cmd/ctrld: use proper exit codes for status command

While at it, disable sort commands, so help output will be in order.

Updates #48
This commit is contained in:
Cuong Manh Le
2023-02-09 01:04:09 +07:00
committed by Cuong Manh Le
parent 6428ac23a0
commit 50b0e5a4b0

View File

@@ -56,6 +56,7 @@ func initCLI() {
// Enable opening via explorer.exe on Windows.
// See: https://github.com/spf13/cobra/issues/844.
cobra.MousetrapHelpText = ""
cobra.EnableCommandSorting = false
rootCmd := &cobra.Command{
Use: "ctrld",
@@ -177,7 +178,7 @@ func initCLI() {
startCmd := &cobra.Command{
PreRun: checkHasElevatedPrivilege,
Use: "start",
Short: "Start the ctrld service",
Short: "Install and start the ctrld service",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
sc := &service.Config{}
@@ -293,15 +294,18 @@ func initCLI() {
status, err := s.Status()
if err != nil {
stderrMsg(err.Error())
return
os.Exit(1)
}
switch status {
case service.StatusUnknown:
stdoutMsg("Unknown status")
os.Exit(2)
case service.StatusRunning:
stdoutMsg("Service is running")
os.Exit(0)
case service.StatusStopped:
stdoutMsg("Service is stopped")
os.Exit(1)
}
},
}
@@ -309,7 +313,7 @@ func initCLI() {
uninstallCmd := &cobra.Command{
PreRun: checkHasElevatedPrivilege,
Use: "uninstall",
Short: "Uninstall the ctrld service",
Short: "Stop and uninstall the ctrld service",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
prog := &prog{}