diff --git a/cmd/influx/completion.go b/cmd/influx/completion.go new file mode 100644 index 00000000000..70c9c3af8b6 --- /dev/null +++ b/cmd/influx/completion.go @@ -0,0 +1,49 @@ +package main + +import ( + "io" + + "github.com/spf13/cobra" +) + +func completionCmd(rootCmd *cobra.Command) *cobra.Command { + writeZSH := func(w io.Writer) error { + if err := rootCmd.GenZshCompletion(w); err != nil { + return err + } + _, err := io.WriteString(w, "\ncompdef _influx influx\n") + return err + } + + return &cobra.Command{ + Use: "completion [bash|zsh]", + Short: "Generates completion scripts", + Args: cobra.ExactValidArgs(1), + ValidArgs: []string{"bash", "zsh"}, + Long: ` + Outputs shell completion for the given shell (bash or zsh) + + OS X: + $ source $(brew --prefix)/etc/bash_completion + $ influx completion bash > ~/.influx-completion # for bash users + $ influx completion zsh > ~/.influx-completion # for zsh users + $ source ~/.influx-completion + + Ubuntu: + $ source /etc/bash-completion + $ source <(influx completion bash) # for bash users + $ source <(influx completion zsh) # for zsh users + + Additionally, you may want to output the completion to a file and source in your .bashrc/.zshrc +`, + RunE: func(cmd *cobra.Command, args []string) error { + switch args[0] { + case "bash": + return rootCmd.GenBashCompletion(rootCmd.OutOrStdout()) + case "zsh": + return writeZSH(rootCmd.OutOrStdout()) + } + return nil + }, + } +} diff --git a/cmd/influx/main.go b/cmd/influx/main.go index 2093e8bfa0d..3754692adb9 100644 --- a/cmd/influx/main.go +++ b/cmd/influx/main.go @@ -192,6 +192,9 @@ func (b *cmdInfluxBuilder) cmd(childCmdFns ...func(f *globalFlags, opt genericCL c.Flags().BoolP("help", "h", false, fmt.Sprintf("Help for the %s command ", c.Name())) }) + // completion command goes last, after the walk, so that all + // commands have every flag listed in the bash|zsh completions. + cmd.AddCommand(completionCmd(cmd)) return cmd }