-
Notifications
You must be signed in to change notification settings - Fork 302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return non-zero exit code on invalid commands #4135
Conversation
cmd/ttn-lw-cli/commands/root.go
Outdated
@@ -59,6 +59,9 @@ var ( | |||
SilenceUsage: true, | |||
Short: "The Things Network Command-line Interface", | |||
PersistentPreRunE: preRun(checkAuth, refreshToken, requireAuth), | |||
RunE: func(cmd *cobra.Command, args []string) error { | |||
return nil | |||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@htdvisser Added this otherwise running ttn-lw-cli --help
returns a non-zero exit status code as well (which makes the CI complain).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that's the right solution, because now we would have inconsistent error codes when requesting help on different commands.
How about putting this in main.go
:
help := commands.Root.HelpFunc()
commands.Root.SetHelpFunc(func(cmd *cobra.Command, a []string) {
help(cmd, a)
os.Exit(0)
})
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will not work as expected, since the help function will be called for invalid commands as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps something slightly more elaborate, like:
help := commands.Root.HelpFunc()
commands.Root.SetHelpFunc(func(cmd *cobra.Command, a []string) {
help(cmd, a)
for _, arg ;= range a {
if a == "--help" {
os.Exit(0)
}
}
})
But this is also getting a bit messy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay so maybe we should just accept an exit code 2 in CI, or use a different command for testing. Perhaps have CI call ttn-lw-* config
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my comment from last week.
80af8d7
to
6f2aa88
Compare
6f2aa88
to
f619d8d
Compare
Summary
Closes #4091
Replaces #4129, thanks @htdvisser for suggesting a much more elegant solution.
Changes
Run
andRunE
are empty, no sub-command has been matched, so return a non-zero exit status code.Testing
Locally
Regressions
There should not be any
Checklist
README.md
for the chosen target branch.CHANGELOG.md
.CONTRIBUTING.md
, there are no fixup commits left.