-
Notifications
You must be signed in to change notification settings - Fork 101
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
silence usage when kudoctl run commands fail #900
Conversation
@@ -29,6 +29,7 @@ func NewKudoctlCmd() *cobra.Command { | |||
Long: `KUDO CLI and future sub-commands can be used to manipulate, inspect and troubleshoot KUDO-specific CRDs | |||
and serves as an API aggregation layer. | |||
`, | |||
SilenceUsage: true, |
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.
So this means we'll silence it for all errors, not just syntax errors. I wonder if there's a way to selectively prevent from printing out usage just for some errors 🤔
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.
FWIK cobra will not silence usage for all errors, only those related to RunE
functions (this could be a validation error), for example this command
Cobra will still print usage if requested (-h
flag is passed) or there is a syntax error.
I don't know/think it is possible to configure Cobra to print usage for only a certain type of errors, i'll try to look for other possible ways.
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.
oh I see, I think we're still returning error through runE for the non-syntax related errors, should we maybe stop doing that? 🤔
I need to think about this more, I think your solution is correct, I am just wondering if we're not suppressing the usage "too much" now as I think printing it automatically after syntax error is very useful.
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.
Yep, but I'd be ok with that. I like my "usage help" when I ask for it ;)
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.
@zen-dog yep "usage help" will be printed only if you pass and unknown flag or call it using "-h"
@alenkacz exactly ! actually in kudoctl, RunE functions return errors for syntax and non-syntax related errors.
should we maybe stop doing that? 🤔
In my opinion the best thing to do here is use RunE
for handling execution errors and Args
for handling syntax/args validation errors. command.Args
is used to customise argument validation in cobra ( docs here )
for example kubectl init
command can be rewritten like this
cmd := &cobra.Command{
Use: "init",
Short: "Initialize KUDO on both the client and server",
Long: initDesc,
Example: initExample,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return errors.New("this command does not accept arguments")
}
return i.validate(cmd.Flags());
},
RunE: func(cmd *cobra.Command, args []string) error {
i.home = Settings.Home
clog.V(8).Printf("init cmd %v", i)
return i.run()
},
}
But i'm still not sure if flags validation should be handled in the Args
function in this example
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.
It's worth experimenting with flags validation in the Args fn! TIL. Let's merge this and we can open an issue to try this out on one command and then issues to expand this out to every other command, wdyt @a-hilaly?
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.
@gerred LGTM 👍 i would be glad to help in this !
Thanks for the PR @a-hilaly ! |
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.
🚢
merging, tyvm @a-hilaly, looks like there's some follow-up work to do here as well! |
What this PR does / why we need it:
Fixes #896
This will prevent
kudoctl
from printing usage when it subcommands RunE functions fails