From fdf964d17eb152b7259e904875613a2839a41642 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 12 Apr 2015 17:19:26 -0700 Subject: [PATCH] cli: make commands more customizable --- cli/commands.go | 41 +++++++++++++++++++---------------------- cli/main.go | 19 +++++++++++++++++-- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/cli/commands.go b/cli/commands.go index 5d7f1ad7d..389ed474a 100644 --- a/cli/commands.go +++ b/cli/commands.go @@ -18,19 +18,22 @@ import ( "github.com/mitchellh/cli" ) -// Commands is the mapping of all the available Vault commands. CommandsInclude -// are the commands to include for help. -var Commands map[string]cli.CommandFactory -var CommandsInclude []string - -func init() { - ui := &cli.BasicUi{ - Writer: os.Stdout, - ErrorWriter: os.Stderr, +// Commands returns the mapping of CLI commands for Vault. The meta +// parameter lets you set meta options for all commands. +func Commands(metaPtr *command.Meta) map[string]cli.CommandFactory { + if metaPtr == nil { + metaPtr = new(command.Meta) } - meta := command.Meta{Ui: ui} - Commands = map[string]cli.CommandFactory{ + meta := *metaPtr + if meta.Ui == nil { + meta.Ui = &cli.BasicUi{ + Writer: os.Stdout, + ErrorWriter: os.Stderr, + } + } + + return map[string]cli.CommandFactory{ "init": func() (cli.Command, error) { return &command.InitCommand{ Meta: meta, @@ -203,19 +206,13 @@ func init() { Revision: GitCommit, Version: ver, VersionPrerelease: rel, - Ui: ui, + Ui: meta.Ui, }, nil }, - } - // Build the commands to include in the help now - CommandsInclude = make([]string, 0, len(Commands)) - for k, _ := range Commands { - CommandsInclude = append(CommandsInclude, k) - } - - // The commands below are hidden from the help output - Commands["token-disk"] = func() (cli.Command, error) { - return &tokenDisk.Command{}, nil + // The commands below are hidden from the help output + "token-disk": func() (cli.Command, error) { + return &tokenDisk.Command{}, nil + }, } } diff --git a/cli/main.go b/cli/main.go index fd2046d7a..00de2c8e3 100644 --- a/cli/main.go +++ b/cli/main.go @@ -8,6 +8,10 @@ import ( ) func Run(args []string) int { + return RunCustom(args, Commands(nil)) +} + +func RunCustom(args []string, commands map[string]cli.CommandFactory) int { // Get the command line args. We shortcut "--version" and "-v" to // just show the version. for _, arg := range args { @@ -20,11 +24,22 @@ func Run(args []string) int { } } + // Build the commands to include in the help now. This is pretty... + // tedious, but we don't have a better way at the moment. + commandsInclude := make([]string, 0, len(commands)) + for k, _ := range commands { + switch k { + case "token-disk": + default: + commandsInclude = append(commandsInclude, k) + } + } + cli := &cli.CLI{ Args: args, - Commands: Commands, + Commands: commands, HelpFunc: cli.FilteredHelpFunc( - CommandsInclude, cli.BasicHelpFunc("vault")), + commandsInclude, cli.BasicHelpFunc("vault")), } exitCode, err := cli.Run()