diff --git a/cmd/root.go b/cmd/root.go index 5760724a..cd429a7f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -94,6 +94,7 @@ func init() { serviceCmd, dumpCmd, restoreCmd, + socketCmd, ) // rootCmd.SetHelpCommand(helpCmd) rootCmd.PersistentFlags().StringVarP(&appPath, "app", "a", "", L("Application directory")) diff --git a/cmd/socket.go b/cmd/socket.go new file mode 100644 index 00000000..563c63bc --- /dev/null +++ b/cmd/socket.go @@ -0,0 +1,95 @@ +package cmd + +import ( + "fmt" + "strings" + + "github.com/fatih/color" + jsoniter "github.com/json-iterator/go" + "github.com/spf13/cobra" + "github.com/yaoapp/gou" + "github.com/yaoapp/kun/exception" + "github.com/yaoapp/yao/config" + "github.com/yaoapp/yao/engine" + "github.com/yaoapp/yao/share" +) + +var socketCmd = &cobra.Command{ + Use: "socket", + Short: L("Open socket"), + Long: L("Open socket"), + Run: func(cmd *cobra.Command, args []string) { + defer share.SessionStop() + defer gou.KillPlugins() + 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 + engine.Load(cfg) + if len(args) < 1 { + fmt.Println(color.RedString(L("Not enough arguments"))) + fmt.Println(color.WhiteString(share.BUILDNAME + " help")) + return + } + + name := args[0] + pargs := []interface{}{} + for i, arg := range args { + if i == 0 { + continue + } + + // 解析参数 + 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)) + } + + } + + socket, has := gou.Sockets[name] + if !has { + fmt.Println(color.RedString(L("%s not exists!"), name)) + return + } + + if socket.Mode != "client" { + fmt.Println(color.RedString(L("%s not supported yet!"), socket.Mode)) + return + } + + fmt.Println(color.WhiteString("\n---------------------------------")) + fmt.Println(color.WhiteString(socket.Name)) + fmt.Println(color.WhiteString("---------------------------------")) + fmt.Println(color.GreenString("Mode: %s", socket.Mode)) + fmt.Println(color.GreenString("Host: %s://%s", socket.Protocol, socket.Host)) + fmt.Println(color.GreenString("Port: %s", socket.Port)) + fmt.Println(color.WhiteString("--------------------------------------")) + err := socket.Open(pargs...) + if err != nil { + fmt.Println(color.RedString(L("%s"), err.Error())) + return + } + + }, +} diff --git a/cmd/start.go b/cmd/start.go index 9d4f1255..052164a7 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -107,7 +107,7 @@ var startCmd = &cobra.Command{ if sock.Mode == "server" { go sock.Start() } else if sock.Mode == "client" { - go sock.Connect() + go sock.Open() } } } diff --git a/server/process.go b/server/process.go index 8cd7ce37..d0b02b8b 100644 --- a/server/process.go +++ b/server/process.go @@ -6,8 +6,8 @@ import ( ) func init() { - gou.RegisterProcessHandler("xiang.server.Start", ProcessStart) - gou.RegisterProcessHandler("xiang.server.Connect", ProcessConnect) + // gou.RegisterProcessHandler("xiang.server.Start", ProcessStart) + // gou.RegisterProcessHandler("xiang.server.Open", ProcessOpen) } // ProcessStart xiang.server.Start @@ -33,25 +33,31 @@ func ProcessStart(process *gou.Process) interface{} { return nil } -// ProcessConnect xiang.server.Connect -func ProcessConnect(process *gou.Process) interface{} { - process.ValidateArgNums(1) +// ProcessOpen xiang.server.Open +// func ProcessOpen(process *gou.Process) interface{} { +// process.ValidateArgNums(1) - name := process.ArgsString(0) - serv, has := gou.Sockets[name] - if !has { - exception.New("%s does not load", 400, name).Throw() - } +// name := process.ArgsString(0) +// serv, has := gou.Sockets[name] +// if !has { +// exception.New("%s does not load", 400, name).Throw() +// return nil +// } - args := []interface{}{} - if process.NumOfArgs() > 1 { - args = process.Args[1:] - } +// args := []interface{}{} +// if process.NumOfArgs() > 1 { +// args = process.Args[1:] +// } - if serv.Mode != "client" { - exception.New("%s mode [%s] not server", 400, name, serv.Mode).Throw() - } +// if serv.Mode != "client" { +// exception.New("%s mode [%s] should be client", 400, name, serv.Mode).Throw() +// return nil +// } - serv.Connect(args...) - return nil -} +// err := serv.Open(args...) +// if err != nil { +// exception.New("%s: %s", 500, name, err.Error()).Throw() +// } + +// return nil +// } diff --git a/server/socket_test.go b/server/socket_test.go index 05af1a9d..ecb73c82 100644 --- a/server/socket_test.go +++ b/server/socket_test.go @@ -21,12 +21,12 @@ func TestProcessStart(t *testing.T) { // }) } -func TestProcessConnect(t *testing.T) { - Load(config.Conf) - // assert.NotPanics(t, func() { - // gou.NewProcess("xiang.server.Connect", "rfid_client").Run() - // }) -} +// func TestProcessConnect(t *testing.T) { +// Load(config.Conf) +// // assert.NotPanics(t, func() { +// // gou.NewProcess("xiang.server.Connect", "rfid_client").Run() +// // }) +// } func check(t *testing.T) { keys := []string{}