Skip to content

Commit aa2ef29

Browse files
committed
feat(influx): add completion command for users to generate the completions for the influx cli
1 parent 5211e55 commit aa2ef29

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

cmd/influx/completion.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
import (
4+
"io"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
func completionCmd(rootCmd *cobra.Command) *cobra.Command {
10+
writeZSH := func(w io.Writer) error {
11+
if err := rootCmd.GenZshCompletion(w); err != nil {
12+
return err
13+
}
14+
_, err := io.WriteString(w, "\ncompdef _influx influx\n")
15+
return err
16+
}
17+
18+
return &cobra.Command{
19+
Use: "completion [bash|zsh]",
20+
Short: "Generates completion scripts",
21+
Args: cobra.ExactValidArgs(1),
22+
ValidArgs: []string{"bash", "zsh"},
23+
Long: `
24+
Outputs shell completion for the given shell (bash or zsh)
25+
26+
OS X:
27+
$ source $(brew --prefix)/etc/bash_completion
28+
$ influx completion bash > ~/.influx-completion # for bash users
29+
$ influx completion zsh > ~/.influx-completion # for zsh users
30+
$ source ~/.influx-completion
31+
32+
Ubuntu:
33+
$ source /etc/bash-completion
34+
$ source <(influx completion bash) # for bash users
35+
$ source <(influx completion zsh) # for zsh users
36+
37+
Additionally, you may want to output the completion to a file and source in your .bashrc/.zshrc
38+
`,
39+
RunE: func(cmd *cobra.Command, args []string) error {
40+
switch args[0] {
41+
case "bash":
42+
return rootCmd.GenBashCompletion(rootCmd.OutOrStdout())
43+
case "zsh":
44+
return writeZSH(rootCmd.OutOrStdout())
45+
}
46+
return nil
47+
},
48+
}
49+
}

cmd/influx/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ func (b *cmdInfluxBuilder) cmd(childCmdFns ...func(f *globalFlags, opt genericCL
192192
c.Flags().BoolP("help", "h", false, fmt.Sprintf("Help for the %s command ", c.Name()))
193193
})
194194

195+
// completion command goes last, after the walk, so that all
196+
// commands have every flag listed in the bash|zsh completions.
197+
cmd.AddCommand(completionCmd(cmd))
195198
return cmd
196199
}
197200

0 commit comments

Comments
 (0)