Skip to content
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

Running invalid command causes kind CLI to exit with 0 #2087

Closed
vainu-lauri opened this issue Feb 24, 2021 · 2 comments · Fixed by #2090
Closed

Running invalid command causes kind CLI to exit with 0 #2087

vainu-lauri opened this issue Feb 24, 2021 · 2 comments · Fixed by #2090
Assignees
Labels
kind/bug Categorizes issue or PR as related to a bug. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete.
Milestone

Comments

@vainu-lauri
Copy link
Contributor

What happened:
For example kind load typo will exit with 0

What you expected to happen:
Kind to exit with non-zero (Very useful when used in ci or script etc)

How to reproduce it (as minimally and precisely as possible):

kind load typo
echo $?

Anything else we need to know?:

This is due to cobra (spf13/cobra#1156) and affects all commands that have sub-commands in kind (build, completion, create, delete, export, get, load)

The specified NoArgsor any of the cobra validators don't seem to have any effect unless Run or RunE has been defined for the command. One fix might be to just specify something like:

+               Run: func(cmd *cobra.Command, args []string) {
+                       cmd.Help()
+                       os.Exit(1)
+               },

to every command (or also have them return 0 in case called without arguments).
I implemented this locally and it seemed to work as it should. Although it's very far from an elegant solution since that would need to duplicated to all of the commands that have sub-commands.

If my (probably rather bad fix) seems reasonable I'm happy to create a PR, but I think this need a more elegant solution.

Environment:

  • kind version: (use kind version): kind v0.10.0 go1.15.7 darwin/amd64
  • Kubernetes version: (use kubectl version):
  • Docker version: (use docker info):
  • OS (e.g. from /etc/os-release):
@vainu-lauri vainu-lauri added the kind/bug Categorizes issue or PR as related to a bug. label Feb 24, 2021
@BenTheElder
Copy link
Member

BenTheElder commented Feb 24, 2021

thanks for the report, this is a nice catch.

One fix might be to just specify something like:

I'd prefer we continue to leave exit calls in app.Main as we have users that import the cli cobra commands (and we should return an error instead of exiting their binary), but we should ensure these return a non-nil error, so perhaps the equivalent RunE on all commands instead and return an error.

The general (if not necessarily elegant) idea of putting an explicit cmd.Help run on all commands makes sense.

@BenTheElder BenTheElder added the priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. label Feb 25, 2021
@vainu-lauri
Copy link
Contributor Author

Thanks.

Good point about calling the os.exit() in the subcommand.

I'll open up a PR with the proposed changes and some tests.

Although I'm not sure if the calling of cmd.Help() will cause problems to the users who have imported these commands (that's my main concern).

Then there's the philosophical question whether we should consider incomplete but not wrong command (like kind delete) as failure or not (my current implementation considers it as a failure and exits with 0).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants