From e7221443296cb149521f01e3ec8e2173e2d2e617 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 16 May 2023 20:45:25 +0800 Subject: [PATCH] [add] __neo.ExecCommand service --- neo/command/load.go | 2 +- neo/command/request.go | 19 ++++++++----------- neo/command/types.go | 6 +++--- neo/neo.go | 2 ++ widgets/app/app.go | 38 +++++++++++++++++++++++++++++++++++--- 5 files changed, 49 insertions(+), 18 deletions(-) diff --git a/neo/command/load.go b/neo/command/load.go index c5c23224..c8bf9ace 100644 --- a/neo/command/load.go +++ b/neo/command/load.go @@ -65,7 +65,7 @@ func LoadSource(data []byte, file, id string) (*Command, error) { }, Optional: Optional{ Autopilot: false, - Confirm: "", + Confirm: false, MaxAttempts: 10, }, } diff --git a/neo/command/request.go b/neo/command/request.go index 05881377..8d688fac 100644 --- a/neo/command/request.go +++ b/neo/command/request.go @@ -61,7 +61,7 @@ func (req *Request) Run(messages []map[string]interface{}, cb func(msg *message. } // Send the command to the service - if req.Command.Optional.Confirm != "" { + if req.Command.Optional.Confirm { req.confirm(args, cb) return nil } @@ -117,24 +117,18 @@ func (req *Request) Run(messages []map[string]interface{}, cb func(msg *message. // confirm the command func (req *Request) confirm(args []interface{}, cb func(msg *message.JSON) int) { - service := strings.Split(req.Command.Optional.Confirm, ".") - if len(service) == 0 { - service = []string{"neo", "Exec"} - } else if len(service) == 1 { - service = []string{"neo", service[0]} - } - payload := map[string]interface{}{ - "method": service[1], + "method": "ExecCommand", "args": []interface{}{ req.id, req.Command.Process, args, + map[string]interface{}{"stack": req.ctx.Stack, "path": req.ctx.Path}, }, } msg := req.msg(). - Action("ExecCommand", fmt.Sprintf("Service.%s", service[0]), payload, ""). + Action("ExecCommand", "Service.__neo", payload, ""). Confirm(). Done() @@ -291,7 +285,10 @@ func (req *Request) prepareAfter(content string, cb func(msg *message.JSON) int) } // prepare the args - args := []interface{}{content} + args := []interface{}{ + content, + map[string]interface{}{"stack": req.ctx.Stack, "path": req.ctx.Path}, // context + } return req.runScript(req.Prepare.After, args, cb) } diff --git a/neo/command/types.go b/neo/command/types.go index 05b36c6a..e018ca82 100644 --- a/neo/command/types.go +++ b/neo/command/types.go @@ -62,9 +62,9 @@ type Prompt struct { // Optional optional type Optional struct { - Autopilot bool `json:"autopilot,omitempty"` - Confirm string `json:"confirm,omitempty"` - MaxAttempts int `json:"maxAttempts,omitempty"` // default 10 + Autopilot bool `json:"autopilot,omitempty"` + Confirm bool `json:"confirm,omitempty"` + MaxAttempts int `json:"maxAttempts,omitempty"` // default 10 } // Context the context diff --git a/neo/neo.go b/neo/neo.go index 4ef96edd..f25b5166 100644 --- a/neo/neo.go +++ b/neo/neo.go @@ -423,6 +423,8 @@ func (neo *DSL) crossDomain(router *gin.Engine, path string) { }) router.OPTIONS(path, func(c *gin.Context) { c.AbortWithStatus(204) }) + router.OPTIONS(path+"/commands", func(c *gin.Context) { c.AbortWithStatus(204) }) + router.OPTIONS(path+"/history", func(c *gin.Context) { c.AbortWithStatus(204) }) } func (neo *DSL) setGuard(router *gin.Engine) error { diff --git a/widgets/app/app.go b/widgets/app/app.go index c93fbced..ec1032a7 100644 --- a/widgets/app/app.go +++ b/widgets/app/app.go @@ -1,6 +1,7 @@ package app import ( + "context" "fmt" "os" "path/filepath" @@ -270,22 +271,53 @@ func processService(process *process.Process) interface{} { args = v } + // Foward: Neo Confirm Command + if service == "__yao_service.__neo" && method == "ExecCommand" { + if len(args) < 4 { + exception.New("args is required (%v)", 400, args).Throw() + } + + id := args[0].(string) + ctx := args[3].(map[string]interface{}) + processName := args[1].(string) + processArgs := append(args[2].([]interface{}), ctx) + result := forwardNeoExecCommand(process, processName, processArgs...) + return map[string]interface{}{"id": id, "result": result, "context": ctx} + } + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + script, err := v8.Select(service) if err != nil { exception.New("services.%s not loaded", 404, process.ArgsString(0)).Throw() return nil } - ctx, err := script.NewContext(process.Sid, process.Global) + v8ctx, err := script.NewContext(process.Sid, process.Global) if err != nil { message := fmt.Sprintf("services.%s failed to create context. %s", process.ArgsString(0), err.Error()) log.Error("[V8] process error. %s", message) exception.New(message, 500).Throw() return nil } - defer ctx.Close() + defer v8ctx.Close() - res, err := ctx.Call(method, args...) + res, err := v8ctx.CallWith(ctx, method, args...) + if err != nil { + exception.New(err.Error(), 500).Throw() + } + + return res +} + +func forwardNeoExecCommand(p *process.Process, name string, args ...interface{}) interface{} { + new, err := process.Of(name, args...) + if err != nil { + exception.New(err.Error(), 400).Throw() + } + + res, err := new.WithGlobal(p.Global).WithSID(p.Sid).Exec() if err != nil { exception.New(err.Error(), 500).Throw() }