Exit with status 1 on Cobra problems like a missing required flag - #363
Merged
Conversation
Here, exit with status 1 from the River CLI when a problem intrinsic to Cobra occurred like the user entered an unknown command, or didn't pass required flag. Cobra is a little odd in its behavior in that it already prints such a problem to the terminal, but also returns an error with the same information. This change looks for the presence of an error and exits with status 1 if one occurred, but doesn't double print any additional information about it. Fixes #362.
brandur
force-pushed
the
brandur-exit-1-on-cobra-problems
branch
from
May 19, 2024 06:46
dd6f26f to
107fa33
Compare
brandur
commented
May 19, 2024
| if ./river not-a-command; then | ||
| echo "expected non-zero exit code" && exit 1 | ||
| fi | ||
| working-directory: ./cmd/river |
Contributor
Author
There was a problem hiding this comment.
Tested locally as well:
$ go run . migrate-up
Error: required flag(s) "database-url" not set
Usage:
river migrate-up [flags]
Flags:
--database-url postgres://... URL of the database to migrate (should look like postgres://...
--dry-run print information on migrations, but don't apply them
-h, --help help for migrate-up
--max-steps int maximum number of steps to migrate
--show-sql show SQL of each migration
--target-version int target version to migrate to (final state includes this version, but none after it)
Global Flags:
--debug output maximum logging verbosity (debug level)
-v, --verbose output additional logging verbosity (info level)
exit status 1
$ echo $?
1
bgentry
approved these changes
May 19, 2024
Contributor
Author
|
Thanks! |
Merged
brandur
added a commit
that referenced
this pull request
May 21, 2024
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Here, exit with status 1 from the River CLI when a problem intrinsic to
Cobra occurred like the user entered an unknown command, or didn't pass
required flag.
Cobra is a little odd in its behavior in that it already prints such a
problem to the terminal, but also returns an error with the same
information. This change looks for the presence of an error and exits
with status 1 if one occurred, but doesn't double print any additional
information about it.
Fixes #362.