92 lines
2.3 KiB
Go
92 lines
2.3 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/fatih/color"
|
|
jsoniter "github.com/json-iterator/go"
|
|
"github.com/spf13/cobra"
|
|
"github.com/yaoapp/gou/plugin"
|
|
"github.com/yaoapp/gou/process"
|
|
"github.com/yaoapp/kun/exception"
|
|
"github.com/yaoapp/kun/utils"
|
|
"github.com/yaoapp/yao/config"
|
|
"github.com/yaoapp/yao/engine"
|
|
"github.com/yaoapp/yao/share"
|
|
)
|
|
|
|
var runCmd = &cobra.Command{
|
|
Use: "run",
|
|
Short: L("Execute process"),
|
|
Long: L("Execute process"),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
defer share.SessionStop()
|
|
defer plugin.KillAll()
|
|
defer func() {
|
|
err := exception.Catch(recover())
|
|
if err != nil {
|
|
fmt.Println(color.RedString(L("Fatal: %s"), err.Error()))
|
|
}
|
|
}()
|
|
|
|
Boot()
|
|
cfg := config.Conf
|
|
cfg.Session.IsCLI = true
|
|
if len(args) < 1 {
|
|
fmt.Println(color.RedString(L("Not enough arguments")))
|
|
fmt.Println(color.WhiteString(share.BUILDNAME + " help"))
|
|
return
|
|
}
|
|
|
|
err := engine.Load(cfg)
|
|
if err != nil {
|
|
fmt.Println(color.RedString(L("Engine: %s"), err.Error()))
|
|
}
|
|
|
|
name := args[0]
|
|
fmt.Println(color.GreenString(L("Run: %s"), name))
|
|
pargs := []interface{}{}
|
|
for i, arg := range args {
|
|
if i == 0 {
|
|
continue
|
|
}
|
|
|
|
// Parse the arguments
|
|
if strings.HasPrefix(arg, "::") {
|
|
arg := strings.TrimPrefix(arg, "::")
|
|
var v interface{}
|
|
err := jsoniter.Unmarshal([]byte(arg), &v)
|
|
if err != nil {
|
|
fmt.Println(color.RedString(L("Arguments: %s"), err.Error()))
|
|
return
|
|
}
|
|
pargs = append(pargs, v)
|
|
fmt.Println(color.WhiteString("args[%d]: %s", i-1, arg))
|
|
|
|
} else if strings.HasPrefix(arg, "\\::") {
|
|
arg := "::" + strings.TrimPrefix(arg, "\\::")
|
|
pargs = append(pargs, arg)
|
|
fmt.Println(color.WhiteString("args[%d]: %s", i-1, arg))
|
|
|
|
} else {
|
|
pargs = append(pargs, arg)
|
|
fmt.Println(color.WhiteString("args[%d]: %s", i-1, arg))
|
|
}
|
|
|
|
}
|
|
|
|
process := process.New(name, pargs...)
|
|
res, err := process.Exec()
|
|
if err != nil {
|
|
fmt.Println(color.RedString(L("Process: %s"), err.Error()))
|
|
}
|
|
|
|
fmt.Println(color.WhiteString("--------------------------------------"))
|
|
fmt.Println(color.WhiteString(L("%s Response"), name))
|
|
fmt.Println(color.WhiteString("--------------------------------------"))
|
|
utils.Dump(res)
|
|
fmt.Println(color.WhiteString("--------------------------------------"))
|
|
fmt.Println(color.GreenString(L("✨DONE✨")))
|
|
},
|
|
}
|