From 7fa01a2a74ceb9babb5a8696739095d8f9a6c02b Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 28 Jun 2017 11:54:48 +0200 Subject: [PATCH] Add experimental support for bash autocomplete --- cmd/basecli/commands/auto.go | 31 +++++++++++++++++++++++++++++++ cmd/basecli/main.go | 1 + 2 files changed, 32 insertions(+) create mode 100644 cmd/basecli/commands/auto.go diff --git a/cmd/basecli/commands/auto.go b/cmd/basecli/commands/auto.go new file mode 100644 index 0000000000..4eb7810a2b --- /dev/null +++ b/cmd/basecli/commands/auto.go @@ -0,0 +1,31 @@ +package commands + +import ( + "os" + + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +var AutoCompleteCmd = &cobra.Command{ + Use: "complete", + Short: "generate bash autocompletions", + RunE: doAutoComplete, +} + +const ( + FlagOutput = "file" +) + +func init() { + AutoCompleteCmd.Flags().String(FlagOutput, "", "file to output bash autocompletion") + AutoCompleteCmd.MarkFlagFilename(FlagOutput) +} + +func doAutoComplete(cmd *cobra.Command, args []string) error { + output := viper.GetString(FlagOutput) + if output == "" { + return cmd.Root().GenBashCompletion(os.Stdout) + } + return cmd.Root().GenBashCompletionFile(output) +} diff --git a/cmd/basecli/main.go b/cmd/basecli/main.go index 6b335e699d..4f78f792f1 100644 --- a/cmd/basecli/main.go +++ b/cmd/basecli/main.go @@ -56,6 +56,7 @@ func main() { tr, proxy.RootCmd, coincmd.VersionCmd, + bcmd.AutoCompleteCmd, ) cmd := cli.PrepareMainCmd(BaseCli, "BC", os.ExpandEnv("$HOME/.basecli"))