Skip to content

Commit

Permalink
add cli command server status
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny0826 authored and xcaspar committed Jan 16, 2020
1 parent e08cc07 commit eae5c34
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions cli/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func CmdInit() *baseCommand {
baseCmd.AddCommand(serverCommand)
serverCommand.AddCommand(&StartServerCommand{})
serverCommand.AddCommand(&StopServerCommand{})
serverCommand.AddCommand(&StatusServerCommand{})

return baseCmd
}
58 changes: 58 additions & 0 deletions cli/cmd/server_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cmd

import (
"context"
"fmt"
"strings"

"github.com/chaosblade-io/chaosblade-spec-go/channel"
"github.com/chaosblade-io/chaosblade-spec-go/spec"
"github.com/spf13/cobra"
)

type StatusServerCommand struct {
baseCommand
}

func (ssc *StatusServerCommand) Init() {
ssc.command = &cobra.Command{
Use: "status",
Short: "Prints out the status of blade server",
Long: "Prints out the status of blade server",
Aliases: []string{"s"},
RunE: func(cmd *cobra.Command, args []string) error {
return ssc.run(cmd, args)
},
Example: statusServerExample(),
}
}

func (ssc *StatusServerCommand) run(cmd *cobra.Command, args []string) error {
// check if the process named `blade server --start` exists or not
pids, err := channel.GetPidsByProcessName(startServerKey, context.TODO())
if err != nil {
return spec.ReturnFail(spec.Code[spec.ServerError], err.Error())
}
if len(pids) != 0 {
data := map[string]string{
"status": "up",
"port": "",
}
response := channel.NewLocalChannel().Run(context.TODO(), "ps", fmt.Sprintf("-p %s | grep port", strings.Join(pids, " ")))
fmtStrs := strings.Split(strings.Replace(fmt.Sprintf("%v", response.Result), "\n", "", -1), " ")
for i, p := range fmtStrs {
if p == "--port" {
data["port"] = fmtStrs[i+1]
}
}
response = spec.ReturnSuccess(data)
ssc.command.Println(response.Print())
} else {
return spec.ReturnFail(spec.Code[spec.ServerError], "down")
}
return nil
}

func statusServerExample() string {
return `blade server status`
}

0 comments on commit eae5c34

Please sign in to comment.