|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | +) |
| 9 | + |
| 10 | +var completionCmd = &cobra.Command{ |
| 11 | + Use: "completion [bash|zsh|fish|powershell]", |
| 12 | + Short: "Generate shell completion scripts", |
| 13 | + Long: `Generate shell completion scripts for the Distill CLI. |
| 14 | +
|
| 15 | +Bash: |
| 16 | + # One-time setup (requires bash-completion package): |
| 17 | + $ distill completion bash > /etc/bash_completion.d/distill |
| 18 | +
|
| 19 | + # Per-session: |
| 20 | + $ source <(distill completion bash) |
| 21 | +
|
| 22 | +Zsh: |
| 23 | + # If shell completion is not already enabled, run: |
| 24 | + $ echo "autoload -U compinit; compinit" >> ~/.zshrc |
| 25 | +
|
| 26 | + $ distill completion zsh > "${fpath[1]}/_distill" |
| 27 | +
|
| 28 | +Fish: |
| 29 | + $ distill completion fish > ~/.config/fish/completions/distill.fish |
| 30 | +
|
| 31 | +PowerShell: |
| 32 | + PS> distill completion powershell | Out-String | Invoke-Expression |
| 33 | +
|
| 34 | + # To load completions for every new session, run: |
| 35 | + PS> distill completion powershell > distill.ps1 |
| 36 | + # and source this file from your PowerShell profile. |
| 37 | +`, |
| 38 | + ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, |
| 39 | + Args: cobra.ExactArgs(1), |
| 40 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 41 | + switch args[0] { |
| 42 | + case "bash": |
| 43 | + return rootCmd.GenBashCompletion(os.Stdout) |
| 44 | + case "zsh": |
| 45 | + return rootCmd.GenZshCompletion(os.Stdout) |
| 46 | + case "fish": |
| 47 | + return rootCmd.GenFishCompletion(os.Stdout, true) |
| 48 | + case "powershell": |
| 49 | + return rootCmd.GenPowerShellCompletionWithDesc(os.Stdout) |
| 50 | + default: |
| 51 | + return fmt.Errorf("unsupported shell %q: choose bash, zsh, fish, or powershell", args[0]) |
| 52 | + } |
| 53 | + }, |
| 54 | +} |
| 55 | + |
| 56 | +func init() { |
| 57 | + rootCmd.AddCommand(completionCmd) |
| 58 | +} |
0 commit comments