-
Notifications
You must be signed in to change notification settings - Fork 587
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 shell completion by adding missing usage message required by spf13/cobra #1688
Conversation
I'm having a bit of a hard time getting this working. If I try this on my mac with
Is there a particular container I could use to see this work properly? Also: you will need to sign-off your commits. |
cmd/syft/cli/commands.go
Outdated
@@ -55,6 +55,7 @@ func New() (*cobra.Command, error) { | |||
|
|||
// rootCmd is currently an alias for the packages command | |||
rootCmd := &cobra.Command{ | |||
Use: fmt.Sprintf("%s package [SOURCE]", internal.ApplicationName), |
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.
package
is not a valid command, and this is the root command, so this should probably be:
fmt.Sprintf("%s [SOURCE]", internal.ApplicationName)
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.
That should have been fmt.Sprintf("%s packages [SOURCE]", ...
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.
Even so this is the root command, not the packages command (even though they are duplicates), so it should display usage text as the root command.
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.
Using fmt.Sprintf("%s packages [SOURCE]",...
causes the following to be output for the 'Usage'
$ syft
Generate a packaged-based Software Bill Of Materials (SBOM) from container images and filesystems
Usage:
syft packages [SOURCE] [flags]
syft [command]
Examples:
syft packages alpin...
Happy to change that to what ever you prefer. The important bit (for our fix) is that we have syft
at the start of the command. That is what the spf13/cobra
library needs to generate a working shell completion script (for bash at least!!)
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.
Right, but since a user can run syft without the packages
command, I think we should leave this as "%s [SOURCE]"
, this leaves syft
as the first part of the usage, which is what you need.
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.
Hmmm... So the [SOURCE]
bit of that usage command is only really relevant when running syft packages ...
$ syft packages
Generate a packaged-based Software Bill Of Materials (SBOM) from container images and filesystems
Usage:
syft packages [SOURCE] [flags]
Perhaps we should just have fmt.Sprintf("%s [command] [flags]", ...
as the usage for the main syft command?
This would just give:
$ syft
Generate a packaged-based Software Bill Of Materials (SBOM) from container images and filesystems
Usage:
syft [command] [flags]
syft [command]
Examples:
syft packages alpine:latest a summary of discovered packages
syft packages alpine:la...
...which seems to make a bit more sense. Are you OK with that?
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.
There's a bit of a mismatch between the help text and the command itself (this should be fixed, but I would not ask you to change this as part of this PR).
You can run Syft like:
syft alpine:latest
without the packages
command keyword, because the root command duplicates the functionality of the packages command.
For this reason, I would prefer that the root help text indicates how to run the root command, rather than indicating packages
or some sub-command is necessary. It's a very small detail.
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.
Ahh... OK. That makes sense now. The penny finally dropped!! 😄
Hi Keith
The original issue (#962) was with shell completion for bash. I don't use zsh so I've never tried the completions that are generated for that particular shell... EDIT: FWIW I've just tried to get the completions working for zsh and haven't had much luck either (note that I'm a complete noob with zsh). Perhaps the zsh completions need fixing as well?
I can see this working using the
|
Shall I fix that typo (package -> packages) and force push with a signed commit? |
Again, I think this should be removed. And force-push a signed-off commit, yes 👍 |
Ahh, this was the magic sauce:
I think we can move forward after the aforementioned changes are done, thanks @DanHam |
Thanks Keith, I've now pushed up a signed commit with changes. I've set the usage message to Please let me know if you require any additional changes. |
@DanHam I hope this explanation makes sense why I still feel this should be |
…3/cobra The spf13/cobra library requires and uses the first command given in the usage to correctly generate the shell completion script. Signed-off-by: DanHam <DanHam@users.noreply.github.com>
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.
👍 thanks for sticking through this, @DanHam !
It does! Thank you for your patience!!! Hopefully, that last push should be OK. |
FYI -- I tested this out with bash based on the instructions @DanHam provided:
|
…13/cobra (anchore#1688) Signed-off-by: DanHam <DanHam@users.noreply.github.com>
Attempts a fix for #962.
All credit to @cschug - see this comment
As pointed out in that comment, the spf13/cobra library writes out the shell completion script using a
fmt.Sprintf
here.The
%[1]s
in that string seem to be replaced with the first command in theUse:
field of thecobra.Command
struct. Currently this field is not set so the%[1]s
expand to an empty string resulting in a broken shell completion script.Adding the field (following the same method used by grype - here) gives the
spf13/cobra
library the info it needs to correctly generate the shell completion script.Testing locally seems to show everything is working OK. The shell completion now works as expected after running
source <(syft completion bash)
Fixes: #962