From 91e90eba6bfea44ee06f1413ba943d21659217e4 Mon Sep 17 00:00:00 2001 From: Christoph Blecker Date: Tue, 10 Sep 2019 01:30:02 -0700 Subject: [PATCH] Add support for bash completions (#640) --- pkg/commands/completion.go | 26 ++++++++++++++++++++++++++ pkg/commands/executor.go | 1 + 2 files changed, 27 insertions(+) create mode 100644 pkg/commands/completion.go diff --git a/pkg/commands/completion.go b/pkg/commands/completion.go new file mode 100644 index 000000000000..065ad34e141c --- /dev/null +++ b/pkg/commands/completion.go @@ -0,0 +1,26 @@ +package commands + +import ( + "fmt" + "os" + + "github.com/spf13/cobra" +) + +func (e *Executor) initCompletion() { + completionCmd := &cobra.Command{ + Use: "completion", + Short: "Generates bash completion scripts", + RunE: e.executeCompletion, + } + e.rootCmd.AddCommand(completionCmd) +} + +func (e *Executor) executeCompletion(cmd *cobra.Command, args []string) error { + err := cmd.Root().GenBashCompletion(os.Stdout) + if err != nil { + return fmt.Errorf("unable to generate bash completions: %v", err) + } + + return nil +} diff --git a/pkg/commands/executor.go b/pkg/commands/executor.go index 1f6231e956ee..718456919af8 100644 --- a/pkg/commands/executor.go +++ b/pkg/commands/executor.go @@ -72,6 +72,7 @@ func NewExecutor(version, commit, date string) *Executor { e.initHelp() e.initLinters() e.initConfig() + e.initCompletion() // init e.cfg by values from config: flags parse will see these values // like the default ones. It will overwrite them only if the same option