cli: make commands more customizable
This commit is contained in:
parent
57be8bcc09
commit
fdf964d17e
|
@ -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
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
19
cli/main.go
19
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()
|
||||
|
|
Loading…
Reference in New Issue