-
Notifications
You must be signed in to change notification settings - Fork 123
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
fix: change flag parse error handling to return errors instead of exiting #107
Conversation
…ting Having ExitOnError in combination with SetOutput to a buffer instead of stdout/stderr means flags.Parse output is swallowed and kubeconform silently exits directly with exit code 2 instead of returning the error. Setting ContinueOnError instead returns the error, and writes usage help to the buffer, so error handling code in main is reached.
This also happens if a flag is input incorrectly, such as for a typo ( I actually dug to the same code and changed it before I realised a PR was already open, but here you can see the difference in behaviour with the same change I did:
|
Awesome thanks :) I had the same issue in another project of mine a few months ago and didnt port the fix here 🙈 yannh/redis-dump-go@d5bfe02 |
…ting (#107) * fix: change flag parse error handling to return errors instead of exiting Having ExitOnError in combination with SetOutput to a buffer instead of stdout/stderr means flags.Parse output is swallowed and kubeconform silently exits directly with exit code 2 instead of returning the error. Setting ContinueOnError instead returns the error, and writes usage help to the buffer, so error handling code in main is reached. * Add test for parsing incorrect flags Co-authored-by: Yann Hamon <yann@mandragor.org>
…ting (#107) * fix: change flag parse error handling to return errors instead of exiting Having ExitOnError in combination with SetOutput to a buffer instead of stdout/stderr means flags.Parse output is swallowed and kubeconform silently exits directly with exit code 2 instead of returning the error. Setting ContinueOnError instead returns the error, and writes usage help to the buffer, so error handling code in main is reached. * Add test for parsing incorrect flags Co-authored-by: Yann Hamon <yann@mandragor.org>
Currently, if flag parsing fails, kubeconform exits with status 2 but no error message. This is because
ExitOnError
meansflags.Parse
actually callsos.Exit
directly, rather than returning the error to the caller, which seems to have been what the rest of the program assumes. In combination withflags.SetOutput
, the error message is written to a buffer which is never printed to the user.This PR changes to
ContinueOnError
, which instead returns the error, leading to the buffer with the error message to be printed before exiting.As a note, it also means the exit code will be 1 instead of 2 on flag parse errors, since that is what is currently done in
realMain
on errors fromconfig.FromFlags
. Not sure if this is something that would be considered a breaking change?