trivy
trivy copied to clipboard
feat(cli): add command completion
Description
Add autocompletion to trivy command by adding a new command completion
.
Before:
Available Commands:
config Scan config files for misconfigurations
filesystem Scan local filesystem
help Help about any command
image Scan a container image
kubernetes scan kubernetes cluster
module Manage modules
plugin Manage plugins
repository Scan a remote repository
rootfs Scan rootfs
sbom Scan SBOM for vulnerabilities
server Server mode
version Print the version
After:
completion
command is added to the list of commands.
Available Commands:
completion Generate completion script
config Scan config files for misconfigurations
filesystem Scan local filesystem
help Help about any command
image Scan a container image
kubernetes scan kubernetes cluster
module Manage modules
plugin Manage plugins
repository Scan a remote repository
rootfs Scan rootfs
sbom Scan SBOM for vulnerabilities
server Server mode
version Print the version
Usage:
First generate shell with completion command and set up your environment to use it (trivy completion -h
for more information).
Then use trivy [tab] [tab]
to auto complete and get suggestion.
In case of error using completion command, help is displayed. eg:
> trivy completion foo
Usage:
trivy completion [bash|zsh|fish|powershell]
Flags:
-h, --help help for completion
Global Flags:
--cache-dir string cache directory (default "/Users/didier/Library/Caches/trivy")
-c, --config string config path (default "trivy.yaml")
-d, --debug debug mode
--generate-default-config write the default config to trivy-default.yaml
--insecure allow insecure server connections when using TLS
-q, --quiet suppress progress bar and log output
--timeout duration timeout (default 5m0s)
-v, --version show version
2022-10-20T23:43:39.893+0200 FATAL invalid argument "foo" for "trivy completion"
Related issues
- Close #2896
Remove this section if you don't have related PRs.
Checklist
- [x] I've read the guidelines for contributing to this repository.
- [x] I've followed the conventions in the PR title.
- [x] I've added tests that prove my fix is effective or that my feature works.
- [x] I've updated the documentation with the relevant information (if needed).
- [x] I've added usage information (if the PR introduces new options)
- [x] I've included a "before" and "after" example to the description (if the PR is a user interface change).
Hello @knqyf263 , This PR is ready for review. Notice two automated tests are waiting to be executed.
Sorry for the error @knqyf263 . I did pass test and integration test but forgot the linter. I fixed the linter issue and pushed again.
@knqyf263 CI seem waiting again. Maybe because I forced push to have clean commits.
Hi @knqyf263, you seem to be really busy. Could you add the label 'hacktoberfest-accepted' if you think it is worth it. This way you can proceed to nex steps when the time is right for you.
Thanks for your PR. I found the following in the doc.
Cobra will automatically provide your program with a fully functional completion command, similarly to how it provides the help command.
https://github.com/spf13/cobra/blob/main/shell_completions.md
What if enabling the default command? Do we need any customize? https://github.com/aquasecurity/trivy/blob/a377c8d04f7a6f67bc537d19fb95478a61eb64d6/pkg/commands/app.go#L153
Could you add the label 'hacktoberfest-accepted' if you think it is worth it
I don't think we're in Hacktoberfest this year. Am I correct? @itaysk
So I didn't notice that completion was actually deactivated at root command creation. So removing this section actually end up adding completion command too.
...
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
...
This is far more easier than a custom solution.
I can go with this simple solution. Thank you for pointing that out.
Regarding Hacktoberfest, I think that trivy's repo having the label hacktoberfest
makes it part of it by default.
@didiermichel thanks for your time and efforts! it's really nice.
CompletionOptions
doesn't add auto-completion at once.
completion
subcommand generates completion scripts for bash/fish/zsh/powershell. right?
maybe should we add some instructions how to use it? something like this: https://github.com/spf13/cobra/blob/main/shell_completions.md#creating-your-own-completion-command
what do you think about it? thanks again
hi @didiermichel let me know, if you don't have enough time or something else, I can write a sample.
thanks for your effort!
@didiermichel can i help you?
Hi guys, I am quite interested in this issue. If you guys dont mind, I can continue this PR. @didiermichel @afdesk .
Cheers,
@congbang-le sure, i think it'll be nice
Hi, sorry for the delay, thank you @congbang-le to help on this subject.
@didiermichel Can you please grant my permission to update your branch? @afdesk Im gonna add a tutorial for this, something likes https://gist.github.com/congbang-le/93f8651b0479ec86c7cc97ae9e520516 . What do you think about this? And what is the right place to put this file? Thanks
@congbang-le cool! LGTM as for me it's a good idea to add such tutorial. in any case the maintainers will be able to change it.
i'd try to add it into the documentation about CLI: https://aquasecurity.github.io/trivy/v0.37/docs/references/cli/
@didiermichel thanks for granting my permission.
@afdesk I put the tutorial in this path docs/tutorials/additional-resources/shell-completion.md
. Is that okay to you? Is there anything else I can help?
@afdesk I put the tutorial in this path
docs/tutorials/additional-resources/shell-completion.md
. Is that okay to you? Is there anything else I can help?
LGTM. I approved the PR. Let's wait for the maintainers decision thanks
Thank you all!