-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
43c970c
commit 9323563
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |