Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
28492: cli: ensure the exit status is set for unknown sub-commands r=knz a=knz

Fixes cockroachdb#25298.

Prior to this patch, an unknown sub-command would cause an error
message but a success (0) exit status. This patch changes it to
properly report an error status instead.

Release note (cli change): `cockroach` will now report a non-zero exit
status if an attempt is made to use a non-existent sub-command.

Co-authored-by: Raphael 'kena' Poss <knz@cockroachlabs.com>
  • Loading branch information
craig[bot] and knz committed Aug 12, 2018
2 parents 7f34715 + fa66423 commit 9772077
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 18 deletions.
4 changes: 1 addition & 3 deletions pkg/cli/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,7 @@ var certCmds = []*cobra.Command{
var certCmd = &cobra.Command{
Use: "cert",
Short: "create ca, node, and client certs",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Usage()
},
RunE: usageAndErr,
}

func init() {
Expand Down
10 changes: 10 additions & 0 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,13 @@ func Run(args []string) error {
cockroachCmd.SetArgs(args)
return cockroachCmd.Execute()
}

// usageAndErr informs the user about the usage of the command
// and returns an error. This ensures that the top-level command
// has a suitable exit status.
func usageAndErr(cmd *cobra.Command, args []string) error {
if err := cmd.Usage(); err != nil {
return err
}
return fmt.Errorf("unknown sub-command: %s", strings.Join(args, " "))
}
4 changes: 1 addition & 3 deletions pkg/cli/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,5 @@ var debugCmd = &cobra.Command{
These commands are useful for extracting data from the data files of a
process that has failed and cannot restart.
`,
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Usage()
},
RunE: usageAndErr,
}
4 changes: 1 addition & 3 deletions pkg/cli/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,7 @@ var genCmd = &cobra.Command{
Use: "gen [command]",
Short: "generate auxiliary files",
Long: "Generate manpages, example shell settings, example databases, etc.",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Usage()
},
RunE: usageAndErr,
}

var genCmds = []*cobra.Command{
Expand Down
8 changes: 8 additions & 0 deletions pkg/cli/interactive_tests/test_error_handling.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ send "echo \$?\r"
eexpect "1\r\n:/# "
end_test

start_test "Check that unknown sub-commands report a non-zero exit status."
send "$argv node wowowo\r"
eexpect "Error: unknown sub-command"
eexpect ":/# "
send "echo \$?\r"
eexpect "1\r\n:/# "
end_test

send "exit 0\r"
eexpect eof

Expand Down
4 changes: 1 addition & 3 deletions pkg/cli/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,7 @@ var nodeCmd = &cobra.Command{
Use: "node [command]",
Short: "list, inspect or remove nodes",
Long: "List, inspect or remove nodes.",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Usage()
},
RunE: usageAndErr,
}

func init() {
Expand Down
4 changes: 1 addition & 3 deletions pkg/cli/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,7 @@ var userCmds = []*cobra.Command{
var userCmd = &cobra.Command{
Use: "user",
Short: "get, set, list and remove users",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Usage()
},
RunE: usageAndErr,
}

func init() {
Expand Down
4 changes: 1 addition & 3 deletions pkg/cli/zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,7 @@ var zoneCmds = []*cobra.Command{
var zoneCmd = &cobra.Command{
Use: "zone",
Short: "get, set, list and remove zones",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Usage()
},
RunE: usageAndErr,
}

func init() {
Expand Down

0 comments on commit 9772077

Please sign in to comment.