yq/cmd/completion.go

63 lines
1.6 KiB
Go
Raw Normal View History

2020-11-16 01:09:57 +00:00
package cmd
import (
"os"
"github.com/spf13/cobra"
)
var completionCmd = &cobra.Command{
Use: "completion [bash|zsh|fish|powershell]",
Aliases: []string{"shell-completion"},
Short: "Generate the autocompletion script for the specified shell",
2020-11-16 01:09:57 +00:00
Long: `To load completions:
Bash:
$ source <(yq completion bash)
2020-11-16 01:09:57 +00:00
# To load completions for each session, execute once:
Linux:
$ yq completion bash > /etc/bash_completion.d/yq
2020-11-16 01:09:57 +00:00
MacOS:
$ yq completion bash > /usr/local/etc/bash_completion.d/yq
2020-11-16 01:09:57 +00:00
Zsh:
# 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
# To load completions for each session, execute once:
$ yq completion zsh > "${fpath[1]}/_yq"
2020-11-16 01:09:57 +00:00
# You will need to start a new shell for this setup to take effect.
Fish:
$ yq completion fish | source
2020-11-16 01:09:57 +00:00
# To load completions for each session, execute once:
$ yq completion fish > ~/.config/fish/completions/yq.fish
2020-11-16 01:09:57 +00:00
`,
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
2020-11-16 01:09:57 +00:00
RunE: func(cmd *cobra.Command, args []string) error {
var err error = nil
switch args[0] {
case "bash":
err = cmd.Root().GenBashCompletionV2(os.Stdout, true)
2020-11-16 01:09:57 +00:00
case "zsh":
err = cmd.Root().GenZshCompletion(os.Stdout)
case "fish":
err = cmd.Root().GenFishCompletion(os.Stdout, true)
case "powershell":
err = cmd.Root().GenPowerShellCompletion(os.Stdout)
}
return err
},
}