Skip to content

Commit

Permalink
feat: add shell completion
Browse files Browse the repository at this point in the history
  • Loading branch information
cfabianski committed Oct 20, 2023
1 parent 43c970c commit 9323563
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
63 changes: 63 additions & 0 deletions docs/guides/shell-completion.md
Original file line number Diff line number Diff line change
@@ -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]
```
3 changes: 3 additions & 0 deletions internal/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type VersionInfo struct {
func NewApp(version string, commitSHA string) *cobra.Command {
rootCmd := NewRootCommand()
rootCmd.AddCommand(
NewCompletionCommand(),
NewProcessingWorkerCommand(),
NewInitCommand(),
NewScanCommand(),
Expand All @@ -40,6 +41,7 @@ Scan your source code to discover, filter and prioritize security and privacy ri
Usage: bearer <command> [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
Expand All @@ -65,6 +67,7 @@ Learn More:
`

cmd := &cobra.Command{
Use: "bearer",
Args: cobra.NoArgs,
}
cmd.SetUsageTemplate(usageTemplate)
Expand Down
45 changes: 45 additions & 0 deletions internal/commands/completion.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 9323563

Please sign in to comment.