trivy icon indicating copy to clipboard operation
trivy copied to clipboard

feat(cli): add command completion

Open didiermichel opened this issue 2 years ago • 18 comments

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).

didiermichel avatar Oct 20 '22 21:10 didiermichel

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Oct 20 '22 21:10 CLAassistant

Hello @knqyf263 , This PR is ready for review. Notice two automated tests are waiting to be executed.

didiermichel avatar Oct 21 '22 13:10 didiermichel

Sorry for the error @knqyf263 . I did pass test and integration test but forgot the linter. I fixed the linter issue and pushed again.

didiermichel avatar Oct 21 '22 16:10 didiermichel

@knqyf263 CI seem waiting again. Maybe because I forced push to have clean commits.

didiermichel avatar Oct 24 '22 07:10 didiermichel

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.

didiermichel avatar Oct 25 '22 05:10 didiermichel

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

knqyf263 avatar Oct 26 '22 07:10 knqyf263

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

knqyf263 avatar Oct 26 '22 07:10 knqyf263

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 avatar Oct 26 '22 21:10 didiermichel

@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

afdesk avatar Nov 13 '22 19:11 afdesk

hi @didiermichel let me know, if you don't have enough time or something else, I can write a sample.

thanks for your effort!

afdesk avatar Nov 21 '22 06:11 afdesk

@didiermichel can i help you?

afdesk avatar Dec 04 '22 15:12 afdesk

Hi guys, I am quite interested in this issue. If you guys dont mind, I can continue this PR. @didiermichel @afdesk .

Cheers,

congbang-le avatar Jan 30 '23 12:01 congbang-le

@congbang-le sure, i think it'll be nice

afdesk avatar Jan 30 '23 13:01 afdesk

Hi, sorry for the delay, thank you @congbang-le to help on this subject.

didiermichel avatar Jan 30 '23 14:01 didiermichel

@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 avatar Feb 07 '23 03:02 congbang-le

@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/

afdesk avatar Feb 07 '23 05:02 afdesk

@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?

congbang-le avatar Feb 09 '23 08:02 congbang-le

@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

afdesk avatar Feb 09 '23 12:02 afdesk

Thank you all!

knqyf263 avatar Feb 15 '23 11:02 knqyf263