|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + |
| 6 | + "github.com/spf13/cobra" |
| 7 | +) |
| 8 | + |
| 9 | +// Completion returns `completion` command |
| 10 | +func Completion(rootCmd *cobra.Command) *cobra.Command { |
| 11 | + return &cobra.Command{ |
| 12 | + Use: "completion [bash|zsh|fish|powershell]", |
| 13 | + Short: "Generate completion script", |
| 14 | + Long: `Generate shell completion script for Git Profile. |
| 15 | +
|
| 16 | +To load completions: |
| 17 | +
|
| 18 | +Bash: |
| 19 | +
|
| 20 | + $ source <(git-profile completion bash) |
| 21 | +
|
| 22 | + # To load completions for each session, execute once: |
| 23 | + # Linux: |
| 24 | + $ git-profile completion bash > /etc/bash_completion.d/git-profile |
| 25 | + # macOS: |
| 26 | + $ git-profile completion bash > $(brew --prefix)/etc/bash_completion.d/git-profile |
| 27 | +
|
| 28 | +Zsh: |
| 29 | +
|
| 30 | + # Create completion directory and generate file: |
| 31 | + $ mkdir -p ~/.zsh/completions |
| 32 | + $ git-profile completion zsh > ~/.zsh/completions/_git-profile |
| 33 | +
|
| 34 | + # Add to ~/.zshrc if not already present: |
| 35 | + $ echo 'fpath=(~/.zsh/completions $fpath)' >> ~/.zshrc |
| 36 | + $ echo 'autoload -U compinit && compinit' >> ~/.zshrc |
| 37 | +
|
| 38 | + # Reload shell or run: source ~/.zshrc |
| 39 | +
|
| 40 | +Fish: |
| 41 | +
|
| 42 | + $ git-profile completion fish | source |
| 43 | +
|
| 44 | + # To load completions for each session, execute once: |
| 45 | + $ git-profile completion fish > ~/.config/fish/completions/git-profile.fish |
| 46 | +`, |
| 47 | + ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, |
| 48 | + Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), |
| 49 | + DisableFlagsInUseLine: true, |
| 50 | + Run: func(cmd *cobra.Command, args []string) { |
| 51 | + switch args[0] { |
| 52 | + case "bash": |
| 53 | + _ = rootCmd.GenBashCompletionV2(os.Stdout, true) |
| 54 | + case "zsh": |
| 55 | + _ = rootCmd.GenZshCompletion(os.Stdout) |
| 56 | + case "fish": |
| 57 | + _ = rootCmd.GenFishCompletion(os.Stdout, true) |
| 58 | + case "powershell": |
| 59 | + _ = rootCmd.GenPowerShellCompletionWithDesc(os.Stdout) |
| 60 | + } |
| 61 | + }, |
| 62 | + } |
| 63 | +} |
0 commit comments