@@ -24,13 +24,13 @@ import (
24
24
)
25
25
26
26
var (
27
- iotexIDE string
28
- fileList []string
29
- absPath string
27
+ iotexIDE string
28
+ fileList []string
29
+ givenPath string
30
30
31
31
addr = flag .String ("addr" , "localhost:65520" , "http service address" )
32
32
33
- upgrader = websocket.Upgrader {
33
+ upgrade = websocket.Upgrader {
34
34
CheckOrigin : func (r * http.Request ) bool {
35
35
if iotexIDE == r .Header ["Origin" ][0 ] {
36
36
return true
51
51
config .Chinese : "share 将本地文件夹内容分享到IoTex在线智能合约IDE(默认为https://ide.iotex.io)" ,
52
52
}
53
53
flagIoTexIDEUrlUsage = map [config.Language ]string {
54
- config .English : "set your IoTex IDE url instance" ,
55
- config .Chinese : "设置自定义IoTexIDE Url" ,
54
+ config .English : "set your IoTeX IDE url instance" ,
55
+ config .Chinese : "设置自定义IoTeX IDE Url" ,
56
56
}
57
57
)
58
58
@@ -72,26 +72,33 @@ func init() {
72
72
contractShareCmd .Flags ().StringVar (& iotexIDE , "iotex-ide" , "https://ide.iotex.io" , config .TranslateInLang (flagIoTexIDEUrlUsage , config .UILanguage ))
73
73
}
74
74
75
+ func isDir (path string ) bool {
76
+ s , err := os .Stat (path )
77
+ if err != nil {
78
+
79
+ return false
80
+
81
+ }
82
+ return s .IsDir ()
83
+ }
84
+
75
85
func share (args []string ) error {
76
- absPath = args [0 ]
77
- if len (absPath ) == 0 {
86
+ givenPath = args [0 ]
87
+ if len (givenPath ) == 0 {
78
88
return output .NewError (output .ReadFileError , "failed to get directory" , nil )
79
89
}
80
- if ! filepath .IsAbs (absPath ) {
81
- return output .NewError (output .InputError , "inputed path isn't absolute" , nil )
82
- }
83
90
84
- if ! isDir (absPath ) {
85
- return output .NewError (output .InputError , "input file rather than directory" , nil )
91
+ if ! isDir (givenPath ) {
92
+ return output .NewError (output .InputError , "given file rather than directory" , nil )
86
93
}
87
94
88
95
if len (iotexIDE ) == 0 {
89
- return output .NewError (output .FlagError , "failed to get iotex ide url instance" , nil )
96
+ return output .NewError (output .FlagError , "failed to get IoTeX ide url instance" , nil )
90
97
}
91
98
92
- filepath .Walk (absPath , func (path string , info os.FileInfo , err error ) error {
99
+ filepath .Walk (givenPath , func (path string , info os.FileInfo , err error ) error {
93
100
if ! isDir (path ) {
94
- relPath , err := filepath .Rel (absPath , path )
101
+ relPath , err := filepath .Rel (givenPath , path )
95
102
if err != nil {
96
103
return err
97
104
}
@@ -104,14 +111,15 @@ func share(args []string) error {
104
111
return nil
105
112
})
106
113
114
+ log .Println ("Listening on 127.0.0.1:65520, Please open your IDE ( " + iotexIDE + " ) to connect to local files" )
115
+
107
116
http .HandleFunc ("/" , func (writer http.ResponseWriter , request * http.Request ) {
108
- conn , err := upgrader .Upgrade (writer , request , nil )
117
+ conn , err := upgrade .Upgrade (writer , request , nil )
109
118
110
119
if err != nil {
111
120
log .Println ("websocket error:" , err )
112
121
return
113
122
}
114
- log .Println ("contract share client is listening on 127.0.0.1:65520" )
115
123
116
124
for {
117
125
requestInfo := make (map [string ]interface {})
@@ -122,8 +130,6 @@ func share(args []string) error {
122
130
return
123
131
}
124
132
125
- log .Println (requestInfo )
126
-
127
133
response ["action" ] = "response"
128
134
response ["id" ] = requestInfo ["id" ]
129
135
@@ -134,7 +140,6 @@ func share(args []string) error {
134
140
log .Println ("send handshake response" , err )
135
141
break
136
142
}
137
-
138
143
case "list" :
139
144
response ["key" ] = "list"
140
145
payload := make (map [string ]bool )
@@ -153,7 +158,8 @@ func share(args []string) error {
153
158
for i := 0 ; i < s .Len (); i ++ {
154
159
p , _ := s .Index (i ).Interface ().(map [string ]interface {})
155
160
for _ , v := range p {
156
- upload , err := ioutil .ReadFile (absPath + "/" + v .(string ))
161
+ upload , err := ioutil .ReadFile (givenPath + "/" + v .(string ))
162
+ log .Println ("share :" + givenPath + "/" + v .(string ))
157
163
if err != nil {
158
164
log .Println ("read file failed" , err )
159
165
break
@@ -168,7 +174,8 @@ func share(args []string) error {
168
174
}
169
175
}
170
176
}
171
-
177
+ default :
178
+ log .Println ("Don't support this IDE yet. Can not handle websocket method: " + requestInfo ["key" ].(string ))
172
179
}
173
180
}
174
181
})
@@ -177,17 +184,3 @@ func share(args []string) error {
177
184
return nil
178
185
179
186
}
180
-
181
- func isDir (path string ) bool {
182
-
183
- s , err := os .Stat (path )
184
-
185
- if err != nil {
186
-
187
- return false
188
-
189
- }
190
-
191
- return s .IsDir ()
192
-
193
- }
0 commit comments