From 932356389bdea120c8509c6bd27978c8c9d590fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Fabianski?= Date: Fri, 20 Oct 2023 23:07:54 +0200 Subject: [PATCH] feat: add shell completion --- docs/docs.md | 1 + docs/guides/shell-completion.md | 63 +++++++++++++++++++++++++++++++++ internal/commands/app.go | 3 ++ internal/commands/completion.go | 45 +++++++++++++++++++++++ 4 files changed, 112 insertions(+) create mode 100644 docs/guides/shell-completion.md create mode 100644 internal/commands/completion.go diff --git a/docs/docs.md b/docs/docs.md index 6b8261751..9538b57f0 100644 --- a/docs/docs.md +++ b/docs/docs.md @@ -51,6 +51,7 @@ Guides help you make the most of Bearer CLI so you can get up and running quickl - [Run a privacy report](/guides/privacy/) - [Run a data flow report](/guides/dataflow/) - [Using Bearer Cloud](/guides/bearer-cloud/) +- [Enable Completion Script](/guides/shell-completion) ## Explanations diff --git a/docs/guides/shell-completion.md b/docs/guides/shell-completion.md new file mode 100644 index 000000000..9f6c03458 --- /dev/null +++ b/docs/guides/shell-completion.md @@ -0,0 +1,63 @@ +--- +title: Enable Completion Script +--- + +# Enable shell completion + +Below is example steps to enable shell completion feature for `bearer` cli: + +## 1. Know your current shell + +```bash +$ echo $SHELL +/bin/zsh # For this example it is zsh, but will be vary depend on your $SHELL, maybe /bin/bash or /bin/fish +``` + +## 2. Run `completion` command to get sub-commands + +``` bash +bearer completion -h +``` + +Generate the autocompletion script for the zsh shell. + +If shell completion is not already enabled in your environment you will need +to enable it. You can execute the following once: + +```bash +echo "autoload -U compinit; compinit" >> ~/.zshrc +``` + +To load completions in your current shell session: + +```bash +source <(bearer completion zsh); compdef _bearer bearer +``` + +To load completions for every new session, execute once: + +### Linux + +```bash +bearer completion zsh > "${fpath[1]}/_bearer" +``` + +### MacOS + +```bash +bearer completion zsh > $(brew --prefix)/share/zsh/site-functions/_bearer +``` + +You will need to start a new shell for this setup to take effect. + +## 3. Run the sub-commands following the instruction + +```bash +bearer completion zsh > "${fpath[1]}/_bearer" +``` + +## 4. Start a new shell and you can see the shell completion + +```bash +bearer [tab] +``` diff --git a/internal/commands/app.go b/internal/commands/app.go index 28a9abcf4..0e47938dd 100644 --- a/internal/commands/app.go +++ b/internal/commands/app.go @@ -17,6 +17,7 @@ type VersionInfo struct { func NewApp(version string, commitSHA string) *cobra.Command { rootCmd := NewRootCommand() rootCmd.AddCommand( + NewCompletionCommand(), NewProcessingWorkerCommand(), NewInitCommand(), NewScanCommand(), @@ -40,6 +41,7 @@ Scan your source code to discover, filter and prioritize security and privacy ri Usage: bearer [flags] Available Commands: + completion Generate the autocompletion script for your shell scan Scan a directory or file init Write the default config to bearer.yml ignore Manage ignored fingerprints @@ -65,6 +67,7 @@ Learn More: ` cmd := &cobra.Command{ + Use: "bearer", Args: cobra.NoArgs, } cmd.SetUsageTemplate(usageTemplate) diff --git a/internal/commands/completion.go b/internal/commands/completion.go new file mode 100644 index 000000000..2e5392bba --- /dev/null +++ b/internal/commands/completion.go @@ -0,0 +1,45 @@ +package commands + +import ( + "os" + + "github.com/spf13/cobra" +) + +func NewCompletionCommand() *cobra.Command { + usageTemplate := ` +Usage: bearer completion [command] + +Available Commands: + bash Generate the autocompletion script for bash + fish Generate the autocompletion script for fish + powershell Generate the autocompletion script for powershell + zsh Generate the autocompletion script for zsh +` + + cmd := &cobra.Command{ + Use: "completion [command]", + Short: "Generate the autocompletion script for the your shell.", + SilenceErrors: false, + SilenceUsage: false, + DisableFlagsInUseLine: true, + ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, + Args: cobra.MatchAll(cobra.ExactArgs(1)), + Run: func(cmd *cobra.Command, args []string) { + switch args[0] { + case "bash": + cmd.Root().GenBashCompletion(os.Stdout) //nolint:errcheck + case "zsh": + cmd.Root().GenZshCompletion(os.Stdout) //nolint:errcheck + case "fish": + cmd.Root().GenFishCompletion(os.Stdout, true) //nolint:errcheck + case "powershell": + cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout) //nolint:errcheck + } + }, + } + + cmd.SetUsageTemplate(usageTemplate) + + return cmd +}