-
Notifications
You must be signed in to change notification settings - Fork 951
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | ||
} |