2022-01-11 12:24:06 +00:00
|
|
|
package autocomplete
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
const longHelpTemplate = `To load completions:
|
|
|
|
|
|
|
|
Bash:
|
2022-01-11 12:36:38 +00:00
|
|
|
$ source <(%s completion bash)
|
2022-01-11 12:24:06 +00:00
|
|
|
|
2022-01-11 12:36:38 +00:00
|
|
|
To load completions for each session, execute once:
|
|
|
|
Linux:
|
2022-01-11 12:24:06 +00:00
|
|
|
$ %s completion bash > /etc/bash_completion.d/%s
|
2022-01-11 12:36:38 +00:00
|
|
|
MacOS:
|
2022-01-11 12:24:06 +00:00
|
|
|
$ %s completion bash > /usr/local/etc/bash_completion.d/%s
|
|
|
|
|
|
|
|
Zsh:
|
2022-01-11 12:36:38 +00:00
|
|
|
If shell completion is not already enabled in your environment you will need
|
|
|
|
to enable it. You can execute the following once:
|
|
|
|
$ echo "autoload -U compinit; compinit" >> ~/.zshrc
|
2023-03-02 10:56:11 +00:00
|
|
|
|
2022-01-11 12:36:38 +00:00
|
|
|
To load completions for each session, execute once:
|
|
|
|
$ %s completion zsh > "${fpath[1]}/_%s"
|
2023-03-02 10:56:11 +00:00
|
|
|
|
2022-01-11 12:36:38 +00:00
|
|
|
You will need to start a new shell for this setup to take effect.
|
2022-01-11 12:24:06 +00:00
|
|
|
|
|
|
|
Fish:
|
2022-01-11 12:36:38 +00:00
|
|
|
$ %s completion fish | source
|
2023-03-02 10:56:11 +00:00
|
|
|
|
2022-01-11 12:36:38 +00:00
|
|
|
To load completions for each session, execute once:
|
|
|
|
$ %s completion fish > ~/.config/fish/completions/%s.fish
|
2022-01-11 12:24:06 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
// Command returns cobra command structure for autocomplete routine.
|
|
|
|
func Command(name string) *cobra.Command {
|
|
|
|
return &cobra.Command{
|
|
|
|
Use: "completion [bash|zsh|fish|powershell]",
|
|
|
|
Short: "Generate completion script",
|
|
|
|
Long: fmt.Sprintf(longHelpTemplate,
|
|
|
|
name, name, name, name, name, name, name, name, name, name),
|
|
|
|
DisableFlagsInUseLine: true,
|
|
|
|
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
|
2023-03-02 11:25:02 +00:00
|
|
|
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
|
2022-01-11 12:24:06 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
switch args[0] {
|
|
|
|
case "bash":
|
2022-01-11 12:43:24 +00:00
|
|
|
_ = cmd.Root().GenBashCompletion(cmd.OutOrStdout())
|
2022-01-11 12:24:06 +00:00
|
|
|
case "zsh":
|
2022-01-11 12:43:24 +00:00
|
|
|
_ = cmd.Root().GenZshCompletion(cmd.OutOrStdout())
|
2022-01-11 12:24:06 +00:00
|
|
|
case "fish":
|
2022-01-11 12:43:24 +00:00
|
|
|
_ = cmd.Root().GenFishCompletion(cmd.OutOrStdout(), true)
|
2022-01-11 12:24:06 +00:00
|
|
|
case "powershell":
|
2022-01-11 12:43:24 +00:00
|
|
|
_ = cmd.Root().GenPowerShellCompletion(cmd.OutOrStdout())
|
2022-01-11 12:24:06 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|