|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | +) |
| 9 | + |
| 10 | +func init() { |
| 11 | + RootCmd.AddCommand(completionCmd) |
| 12 | +} |
| 13 | + |
| 14 | +var completionCmd = &cobra.Command{ |
| 15 | + Use: "completion [bash|zsh|fish|powershell]", |
| 16 | + Short: "Generate command line completion script", |
| 17 | + Long: `To load completions: |
| 18 | + Bash: |
| 19 | + $ source <(gopherciser completion bash) |
| 20 | +
|
| 21 | + # To load completions for each session, execute once: |
| 22 | + Linux: |
| 23 | + $ gopherciser completion bash > /etc/bash_completion.d/gopherciser |
| 24 | + MacOS: |
| 25 | + $ gopherciser_osx completion bash > /usr/local/etc/bash_completion.d/gopherciser_osx |
| 26 | +
|
| 27 | + Zsh: |
| 28 | + # If shell completion is not already enabled in your environment you will |
| 29 | + # need to enable it. You can execute the following once: |
| 30 | +
|
| 31 | + $ echo "autoload -U compinit; compinit" >> ~/.zshrc |
| 32 | +
|
| 33 | + # To load completions for each session, execute once: |
| 34 | + $ gopherciser completion zsh > "${fpath[1]}/_gopherciser" |
| 35 | +
|
| 36 | + # You might want to put the completion script (_gopherciser) at another |
| 37 | + # path within the fpath. To view the fpath, execute: |
| 38 | + $ echo $fpath |
| 39 | +
|
| 40 | + # You will need to start a new shell for this setup to take effect. |
| 41 | +
|
| 42 | + Fish: |
| 43 | + $ gopherciser completion fish | source |
| 44 | +
|
| 45 | + # To load completions for each session, execute once: |
| 46 | + $ gopherciser completion fish > ~/.config/fish/completions/gopherciser.fish |
| 47 | +
|
| 48 | + Powershell: |
| 49 | + PS> gopherciser.exe completion powershell | Out-String | Invoke-Expression |
| 50 | +
|
| 51 | + # To load completions for every new session, run: |
| 52 | + PS> gopherciser completion powershell > gopherciser.ps1 |
| 53 | + # and source this file from your powershell profile. |
| 54 | +`, |
| 55 | + DisableFlagsInUseLine: true, |
| 56 | + ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, |
| 57 | + Args: cobra.ExactValidArgs(1), |
| 58 | + Run: func(cmd *cobra.Command, args []string) { |
| 59 | + var err error |
| 60 | + switch args[0] { |
| 61 | + case "bash": |
| 62 | + err = cmd.Root().GenBashCompletion(os.Stdout) |
| 63 | + case "zsh": |
| 64 | + err = cmd.Root().GenZshCompletion(os.Stdout) |
| 65 | + case "fish": |
| 66 | + err = cmd.Root().GenFishCompletion(os.Stdout, true) |
| 67 | + case "powershell": |
| 68 | + err = cmd.Root().GenPowerShellCompletion(os.Stdout) |
| 69 | + } |
| 70 | + if err != nil { |
| 71 | + fmt.Fprintln(os.Stderr, err) |
| 72 | + os.Exit(1) |
| 73 | + } |
| 74 | + }, |
| 75 | +} |
0 commit comments