yq/cmd/shell_completion.go

43 lines
1.1 KiB
Go
Raw Normal View History

2020-06-11 08:27:01 +00:00
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
var shellVariant = "bash"
func createBashCompletionCmd(rootCmd *cobra.Command) *cobra.Command {
var completionCmd = &cobra.Command{
Use: "shell-completion",
Short: "Generates shell completion scripts",
Long: `Example usage (for bash): to load completion run
. <(yq bash-completion)
To configure your bash shell to load completions for each session add to your bashrc
# ~/.bashrc or ~/.profile
. <(yq bash-completion)
`,
RunE: func(cmd *cobra.Command, args []string) error {
switch shellVariant {
case "bash", "":
2020-06-11 08:30:45 +00:00
return rootCmd.GenBashCompletion(os.Stdout)
2020-06-11 08:27:01 +00:00
case "zsh":
2020-06-11 08:30:45 +00:00
return rootCmd.GenZshCompletion(os.Stdout)
2020-06-11 08:27:01 +00:00
case "fish":
2020-06-11 08:30:45 +00:00
return rootCmd.GenFishCompletion(os.Stdout, true)
2020-06-11 08:27:01 +00:00
case "powershell":
2020-06-11 08:30:45 +00:00
return rootCmd.GenPowerShellCompletion(os.Stdout)
2020-06-11 08:27:01 +00:00
default:
return fmt.Errorf("Unknown variant %v", shellVariant)
}
},
}
completionCmd.PersistentFlags().StringVarP(&shellVariant, "variation", "V", "", "shell variation: bash (default), zsh, fish, powershell")
return completionCmd
}