github.com/crowdsecurity/crowdsec@v1.6.1/cmd/crowdsec-cli/completion.go (about) 1 package main 2 3 import ( 4 "os" 5 6 "github.com/spf13/cobra" 7 ) 8 9 func NewCompletionCmd() *cobra.Command { 10 completionCmd := &cobra.Command{ 11 Use: "completion [bash|zsh|powershell|fish]", 12 Short: "Generate completion script", 13 Long: `To load completions: 14 15 ### Bash: 16 ` + "```shell" + ` 17 $ source <(cscli completion bash) 18 19 # To load completions for each session, execute once: 20 21 22 # Linux: 23 24 $ cscli completion bash | sudo tee /etc/bash_completion.d/cscli 25 $ source ~/.bashrc 26 27 # macOS: 28 29 $ cscli completion bash | sudo tee /usr/local/etc/bash_completion.d/cscli 30 31 # Troubleshoot: 32 If you have this error (bash: _get_comp_words_by_ref: command not found), it seems that you need "bash-completion" dependency : 33 34 * Install bash-completion package 35 $ source /etc/profile 36 $ source <(cscli completion bash) 37 ` + "```" + ` 38 39 ### Zsh: 40 ` + "```shell" + ` 41 # If shell completion is not already enabled in your environment, 42 # you will need to enable it. You can execute the following once: 43 44 $ echo "autoload -U compinit; compinit" >> ~/.zshrc 45 46 # To load completions for each session, execute once: 47 48 $ cscli completion zsh > "${fpath[1]}/_cscli" 49 50 # You will need to start a new shell for this setup to take effect. 51 52 ### fish: 53 ` + "```shell" + ` 54 $ cscli completion fish | source 55 56 # To load completions for each session, execute once: 57 $ cscli completion fish > ~/.config/fish/completions/cscli.fish 58 ` + "```" + ` 59 ### PowerShell: 60 ` + "```powershell" + ` 61 PS> cscli completion powershell | Out-String | Invoke-Expression 62 63 # To load completions for every new session, run: 64 PS> cscli completion powershell > cscli.ps1 65 # and source this file from your PowerShell profile. 66 ` + "```", 67 DisableFlagsInUseLine: true, 68 DisableAutoGenTag: true, 69 ValidArgs: []string{"bash", "zsh", "powershell", "fish"}, 70 Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), 71 Run: func(cmd *cobra.Command, args []string) { 72 switch args[0] { 73 case "bash": 74 cmd.Root().GenBashCompletion(os.Stdout) 75 case "zsh": 76 cmd.Root().GenZshCompletion(os.Stdout) 77 case "powershell": 78 cmd.Root().GenPowerShellCompletion(os.Stdout) 79 case "fish": 80 cmd.Root().GenFishCompletion(os.Stdout, true) 81 } 82 }, 83 } 84 85 return completionCmd 86 }