| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "embed" |
| 6 | "fmt" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | const ( |
| 11 | completionCommandName = "completion" |
| 12 | |
| 13 | // This flag is supposed to only be used by the completion script itself to generate completions on the fly. |
| 14 | completionFlag = "--generate-shell-completion" |
| 15 | ) |
| 16 | |
| 17 | type renderCompletion func(cmd *Command, appName string) (string, error) |
| 18 | |
| 19 | var ( |
| 20 | //go:embed autocomplete |
| 21 | autoCompleteFS embed.FS |
| 22 | |
| 23 | // completionShells defines the order in which the shell completion |
| 24 | // subcommands appear in help output. Iterating shellCompletions directly |
| 25 | // would use Go's randomized map order, making the listing nondeterministic. |
| 26 | // Keep this in sync with shellCompletions. |
| 27 | completionShells = []string{"bash", "zsh", "fish", "pwsh"} |
| 28 | |
| 29 | shellCompletions = map[string]renderCompletion{ |
| 30 | "bash": func(c *Command, appName string) (string, error) { |
| 31 | b, err := autoCompleteFS.ReadFile("autocomplete/bash_autocomplete") |
| 32 | return fmt.Sprintf(string(b), appName), err |
| 33 | }, |
| 34 | "zsh": func(c *Command, appName string) (string, error) { |
| 35 | b, err := autoCompleteFS.ReadFile("autocomplete/zsh_autocomplete") |
| 36 | return fmt.Sprintf(string(b), appName), err |
| 37 | }, |
| 38 | "fish": func(c *Command, appName string) (string, error) { |
| 39 | b, err := autoCompleteFS.ReadFile("autocomplete/fish_autocomplete") |
| 40 | return fmt.Sprintf(string(b), appName), err |
| 41 | }, |
| 42 | "pwsh": func(c *Command, appName string) (string, error) { |
| 43 | b, err := autoCompleteFS.ReadFile("autocomplete/powershell_autocomplete.ps1") |
| 44 | return string(b), err |
| 45 | }, |
| 46 | } |
| 47 | ) |
| 48 | |
| 49 | const completionDescription = `Output shell completion script for bash, zsh, fish, or Powershell. |
| 50 | Source the output to enable completion. |
| 51 | |
| 52 | # .bashrc |
| 53 | source <($COMMAND completion bash) |
| 54 | |
| 55 | # .zshrc |
| 56 | source <($COMMAND completion zsh) |
| 57 | |
| 58 | # fish |
| 59 | $COMMAND completion fish > ~/.config/fish/completions/$COMMAND.fish |
| 60 | |
| 61 | # Powershell |
| 62 | Output the script to path/to/autocomplete/$COMMAND.ps1 an run it. |
| 63 | ` |
| 64 | |
| 65 | func buildCompletionCommand(appName string) *Command { |
| 66 | cmd := &Command{ |
| 67 | Name: completionCommandName, |
| 68 | Hidden: true, |
| 69 | Usage: "Output shell completion script for bash, zsh, fish, or Powershell", |
| 70 | Description: strings.ReplaceAll(completionDescription, "$COMMAND", appName), |
| 71 | isCompletionCommand: true, |
| 72 | } |
| 73 | |
| 74 | for _, shell := range completionShells { |
| 75 | cmd.Commands = append(cmd.Commands, buildShellCompletionSubcommand(shell, shellCompletions[shell], appName)) |
| 76 | } |
| 77 | |
| 78 | return cmd |
| 79 | } |
| 80 | |
| 81 | func buildShellCompletionSubcommand(shell string, render renderCompletion, appName string) *Command { |
| 82 | return &Command{ |
| 83 | Name: shell, |
| 84 | Usage: fmt.Sprintf("Output %s completion script", shell), |
| 85 | isCompletionCommand: true, |
| 86 | Action: func(ctx context.Context, cmd *Command) error { |
| 87 | completionScript, err := render(cmd, appName) |
| 88 | if err != nil { |
| 89 | return Exit(err, 1) |
| 90 | } |
| 91 | _, err = cmd.Root().Writer.Write([]byte(completionScript)) |
| 92 | if err != nil { |
| 93 | return Exit(err, 1) |
| 94 | } |
| 95 | return nil |
| 96 | }, |
| 97 | } |
| 98 | } |