Skip to content

Commit

Permalink
Exit zero if help/version is explicitly requested (#525)
Browse files Browse the repository at this point in the history
We want a non-zero exit when the user invokes `temporal` with an unknown
command/subcommand, or when invoked with no arguments. But if the user
explicitly asks for help or version information, we should print it and
return success.

Closes #495.
  • Loading branch information
josh-berry committed Apr 8, 2024
1 parent 1442673 commit 267e114
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion temporalcli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"os/signal"
"path/filepath"
"slices"
"strings"
"syscall"
"time"
Expand Down Expand Up @@ -321,8 +322,23 @@ func Execute(ctx context.Context, options CommandOptions) {
if err != nil {
cctx.Options.Fail(err)
}
// If no command ever actually got run, exit nonzero

// If no command ever actually got run, exit nonzero with an error. This is
// an ugly hack to make sure that iff the user explicitly asked for help, we
// exit with a zero error code. (The other situation in which help is
// printed is when the user invokes an unknown command--we still want a
// non-zero exit in that case.) We should revisit this if/when the
// following Cobra issues get fixed:
//
// - https://github.com/spf13/cobra/issues/1156
// - https://github.com/spf13/cobra/issues/706
if !cctx.ActuallyRanCommand {
zeroExitArgs := []string{"--help", "-h", "--version", "-v", "help"}
if slices.ContainsFunc(cctx.Options.Args, func(a string) bool {
return slices.Contains(zeroExitArgs, a)
}) {
return
}
cctx.Options.Fail(fmt.Errorf("unknown command"))
}
}
Expand Down

0 comments on commit 267e114

Please sign in to comment.