Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(influx): add completion command for users to generate the completions for the influx cli #17273

Merged
merged 1 commit into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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