[add] __neo.ExecCommand service
This commit is contained in:
parent
4530aeab2f
commit
e722144329
5 changed files with 49 additions and 18 deletions
|
|
@ -65,7 +65,7 @@ func LoadSource(data []byte, file, id string) (*Command, error) {
|
|||
},
|
||||
Optional: Optional{
|
||||
Autopilot: false,
|
||||
Confirm: "",
|
||||
Confirm: false,
|
||||
MaxAttempts: 10,
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue