Skip to content

Commit 41f7164

Browse files
committed
fix cli with changes from previous commit
1 parent 5643b6c commit 41f7164

File tree

2 files changed

+35
-61
lines changed

2 files changed

+35
-61
lines changed

cmd/cli/cmd_dev.go

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ func devCmdRun(cmd *cobra.Command, args []string) {
105105
"lua_filename": args[0],
106106
}
107107

108-
stopChan := make(chan struct{}, 1)
109108
log.WithFields(fields).Debug("running the function")
110109

111110
m := &executor.Message{
@@ -114,30 +113,19 @@ func devCmdRun(cmd *cobra.Command, args []string) {
114113
Executor: cmd.Flag("executor").Value.String(),
115114
}
116115

117-
replyFunc := func(r *executor.Reply) {
118-
fields := log.Fields{"subject": subject}
119-
log.WithFields(fields).Debug("script replied")
120-
121-
j, err := r.JSON()
122-
if err != nil {
123-
log.WithFields(fields).Errorf("failed to Unmarshal reply: %v", err)
124-
}
125-
126-
cmd.Printf("Result: %s\n", string(j))
127-
stopChan <- struct{}{}
128-
}
129-
130116
executors := executor.StartAllExecutors(cmd.Context(), store, plugins, nil)
131117
exec, err := executor.ExecutorByName(m.Executor, executors)
132118
if err != nil {
133119
cmd.PrintErrf("failed to get executor for message: %v", err)
134-
stopChan <- struct{}{}
135120
return
136121
}
137122

138-
exec.HandleMessage(cmd.Context(), m, replyFunc)
139-
140-
<-stopChan
141-
123+
res := exec.HandleMessage(cmd.Context(), m, scr)
124+
if res.Error != "" {
125+
cmd.PrintErrf("Error while running script: %v", err)
126+
return
127+
}
142128
executor.StopAllExecutors(executors)
129+
130+
cmd.Printf("Result: %s\n", string(res.Payload))
143131
}

cmd/cli/cmd_devhttp.go

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ func devHttpCmdRun(cmd *cobra.Command, args []string) {
5353
}
5454
}
5555

56-
5756
executors := executor.StartAllExecutors(cmd.Context(), store, plugins, nil)
5857
exec, err := executor.ExecutorByName(cmd.Flag("executor").Value.String(), executors)
5958
if err != nil {
@@ -180,53 +179,40 @@ func (p *devHttpProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
180179
URL: url,
181180
}
182181

183-
p.executor.HandleMessage(p.context, msg, func(rep *executor.Reply) {
184-
fields := log.Fields{
185-
"subject": msg.Subject,
186-
"url": msg.URL,
187-
"method": msg.Method,
182+
res := p.executor.HandleMessage(p.context, msg, scr)
183+
if res.Error != "" {
184+
if res.Error == (&executor.NoScriptFoundError{}).Error() {
185+
w.WriteHeader(http.StatusNotFound)
186+
} else {
187+
w.WriteHeader(http.StatusInternalServerError)
188188
}
189-
log.WithFields(fields).Debugf("Results: %s", string(msg.Payload))
190-
191-
if rep.Error != "" {
192-
if rep.Error == (&executor.NoScriptFoundError{}).Error() {
193-
w.WriteHeader(http.StatusNotFound)
194-
} else {
195-
w.WriteHeader(http.StatusInternalServerError)
196-
}
197-
198-
_, err = w.Write([]byte("Error: " + rep.Error))
199-
if err != nil {
200-
log.WithFields(fields).Errorf("failed to write error to HTTP response: %v", err)
201-
}
202189

203-
return
190+
_, err = w.Write([]byte("Error: " + res.Error))
191+
if err != nil {
192+
log.WithFields(fields).Errorf("failed to write error to HTTP response: %v", err)
204193
}
205194

206-
rep.Results.Range(func(key, value any) bool {
207-
sr := value.(*executor.ScriptResult)
208-
if sr.IsHTML {
209-
var hasContentType bool
210-
for k, v := range sr.Headers {
211-
if k == "Content-Type" {
212-
hasContentType = true
213-
}
214-
w.Header().Add(k, v)
215-
}
216-
if !hasContentType {
217-
w.Header().Add("Content-Type", "text/html")
218-
}
219-
w.WriteHeader(sr.Code)
220-
221-
_, err = w.Write(sr.Payload)
222-
if err != nil {
223-
log.WithFields(fields).Errorf("failed to write reply back to HTTP response: %v", err)
224-
}
195+
return
196+
}
197+
198+
if res.IsHTML {
199+
var hasContentType bool
200+
for k, v := range res.Headers {
201+
if k == "Content-Type" {
202+
hasContentType = true
225203
}
204+
w.Header().Add(k, v)
205+
}
206+
if !hasContentType {
207+
w.Header().Add("Content-Type", "text/html")
208+
}
209+
w.WriteHeader(res.Code)
226210

227-
return true
228-
})
229-
})
211+
_, err = w.Write(res.Payload)
212+
if err != nil {
213+
log.WithFields(fields).Errorf("failed to write reply back to HTTP response: %v", err)
214+
}
215+
}
230216
}
231217

232218
func emptyStore(s store.ScriptStore, libraryDir string) {

0 commit comments

Comments
 (0)