From da6ac24accc6a0f984893ae0479f5c57e0faca2f Mon Sep 17 00:00:00 2001 From: David Gillies Date: Fri, 22 Mar 2019 14:45:38 -0700 Subject: [PATCH] Add bash & zsh completion --- cmd/completion.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 cmd/completion.go diff --git a/cmd/completion.go b/cmd/completion.go new file mode 100644 index 00000000000..229d0f98b14 --- /dev/null +++ b/cmd/completion.go @@ -0,0 +1,32 @@ +package cmd + +import ( + "github.com/spf13/cobra" + + "errors" + "os" +) + +// bashCompletionCmd represents the completion command +var completionCmd = &cobra.Command{ + Use: "completion [shell]", + Short: "Output shell completion code for the specified shell (bash or zsh)", + Long: "Output shell completion code for the specified shell (bash or zsh)", + ValidArgs: []string{"bash"}, + RunE: func(cmd *cobra.Command, args []string) error { + if len(args) != 1 { + return cmd.Usage() + } + switch args[0] { + case "bash": + return rootCmd.GenBashCompletion(os.Stdout) + case "zsh": + return rootCmd.GenZshCompletion(os.Stdout) + } + return errors.New("Unsupported shell") + }, +} + +func init() { + rootCmd.AddCommand(completionCmd) +}