Skip to content

Commit

Permalink
feat(influx): add completion command for users to generate the comple…
Browse files Browse the repository at this point in the history
…tions for the influx cli
  • Loading branch information
jsteenb2 committed Mar 16, 2020
1 parent 04b86df commit 7b1efe5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Features

1. [17273](https://github.com/influxdata/influxdb/pull/17273): Add shell completions command for the influx cli

### Bug Fixes

1. [17240](https://github.com/influxdata/influxdb/pull/17240): NodeJS logo displays properly in Firefox
Expand Down
51 changes: 51 additions & 0 deletions cmd/influx/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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", "powershell"},
Long: `
Outputs shell completion for the given shell (bash or zsh)
OS X:
$ source $(brew --prefix)/etc/bash_completion # for bash users
$ source <(influx completion bash) # for bash users
$ source <(influx completion zsh) # for zsh users
Ubuntu:
$ source /etc/bash-completion # for bash users
$ 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 {
writer := rootCmd.OutOrStdout()
switch args[0] {
case "bash":
return rootCmd.GenBashCompletion(writer)
case "powershell":
return rootCmd.GenPowerShellCompletion(writer)
case "zsh":
return writeZSH(writer)
}
return nil
},
}
}
3 changes: 3 additions & 0 deletions cmd/influx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 7b1efe5

Please sign in to comment.