2020-11-16 01:09:57 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var completionCmd = &cobra.Command{
|
2024-04-22 04:15:36 +00:00
|
|
|
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:
|
|
|
|
|
2024-04-22 04:15:36 +00:00
|
|
|
$ source <(yq completion bash)
|
2020-11-16 01:09:57 +00:00
|
|
|
|
|
|
|
# To load completions for each session, execute once:
|
|
|
|
Linux:
|
2024-04-22 04:15:36 +00:00
|
|
|
$ yq completion bash > /etc/bash_completion.d/yq
|
2020-11-16 01:09:57 +00:00
|
|
|
MacOS:
|
2024-04-22 04:15:36 +00:00
|
|
|
$ 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:
|
2024-04-22 04:15:36 +00:00
|
|
|
$ 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:
|
|
|
|
|
2024-04-22 04:15:36 +00:00
|
|
|
$ yq completion fish | source
|
2020-11-16 01:09:57 +00:00
|
|
|
|
|
|
|
# To load completions for each session, execute once:
|
2024-04-22 04:15:36 +00:00
|
|
|
$ yq completion fish > ~/.config/fish/completions/yq.fish
|
2020-11-16 01:09:57 +00:00
|
|
|
`,
|
|
|
|
DisableFlagsInUseLine: true,
|
|
|
|
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
|
2022-10-17 04:15:19 +00:00
|
|
|
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":
|
2023-12-20 07:33:51 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
},
|
|
|
|
}
|