Skip to content

Commit 9e91b33

Browse files
fix ioctl contract share cmd issue (iotexproject#2415)
* New ioctl command to help iotex-studio link local files * add --iotex-ide flag and print execution stdout of commands in real time * Use golang to implement remixd tool * make command support map dir to IDE * fix codereview Co-authored-by: Raullen Chai <[email protected]>
1 parent 70d5b9f commit 9e91b33

File tree

1 file changed

+30
-37
lines changed

1 file changed

+30
-37
lines changed

ioctl/cmd/contract/contractshare.go

+30-37
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ import (
2424
)
2525

2626
var (
27-
iotexIDE string
28-
fileList []string
29-
absPath string
27+
iotexIDE string
28+
fileList []string
29+
givenPath string
3030

3131
addr = flag.String("addr", "localhost:65520", "http service address")
3232

33-
upgrader = websocket.Upgrader{
33+
upgrade = websocket.Upgrader{
3434
CheckOrigin: func(r *http.Request) bool {
3535
if iotexIDE == r.Header["Origin"][0] {
3636
return true
@@ -51,8 +51,8 @@ var (
5151
config.Chinese: "share 将本地文件夹内容分享到IoTex在线智能合约IDE(默认为https://ide.iotex.io)",
5252
}
5353
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",
5656
}
5757
)
5858

@@ -72,26 +72,33 @@ func init() {
7272
contractShareCmd.Flags().StringVar(&iotexIDE, "iotex-ide", "https://ide.iotex.io", config.TranslateInLang(flagIoTexIDEUrlUsage, config.UILanguage))
7373
}
7474

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+
7585
func share(args []string) error {
76-
absPath = args[0]
77-
if len(absPath) == 0 {
86+
givenPath = args[0]
87+
if len(givenPath) == 0 {
7888
return output.NewError(output.ReadFileError, "failed to get directory", nil)
7989
}
80-
if !filepath.IsAbs(absPath) {
81-
return output.NewError(output.InputError, "inputed path isn't absolute", nil)
82-
}
8390

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)
8693
}
8794

8895
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)
9097
}
9198

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 {
93100
if !isDir(path) {
94-
relPath, err := filepath.Rel(absPath, path)
101+
relPath, err := filepath.Rel(givenPath, path)
95102
if err != nil {
96103
return err
97104
}
@@ -104,14 +111,15 @@ func share(args []string) error {
104111
return nil
105112
})
106113

114+
log.Println("Listening on 127.0.0.1:65520, Please open your IDE ( " + iotexIDE + " ) to connect to local files")
115+
107116
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)
109118

110119
if err != nil {
111120
log.Println("websocket error:", err)
112121
return
113122
}
114-
log.Println("contract share client is listening on 127.0.0.1:65520")
115123

116124
for {
117125
requestInfo := make(map[string]interface{})
@@ -122,8 +130,6 @@ func share(args []string) error {
122130
return
123131
}
124132

125-
log.Println(requestInfo)
126-
127133
response["action"] = "response"
128134
response["id"] = requestInfo["id"]
129135

@@ -134,7 +140,6 @@ func share(args []string) error {
134140
log.Println("send handshake response", err)
135141
break
136142
}
137-
138143
case "list":
139144
response["key"] = "list"
140145
payload := make(map[string]bool)
@@ -153,7 +158,8 @@ func share(args []string) error {
153158
for i := 0; i < s.Len(); i++ {
154159
p, _ := s.Index(i).Interface().(map[string]interface{})
155160
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))
157163
if err != nil {
158164
log.Println("read file failed", err)
159165
break
@@ -168,7 +174,8 @@ func share(args []string) error {
168174
}
169175
}
170176
}
171-
177+
default:
178+
log.Println("Don't support this IDE yet. Can not handle websocket method: " + requestInfo["key"].(string))
172179
}
173180
}
174181
})
@@ -177,17 +184,3 @@ func share(args []string) error {
177184
return nil
178185

179186
}
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

Comments
 (0)