2017-09-05 03:50:13 +00:00
|
|
|
package command
|
2015-04-21 18:24:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2017-09-05 03:50:13 +00:00
|
|
|
"os"
|
2015-04-21 18:24:27 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
2017-09-05 03:50:13 +00:00
|
|
|
func Run(args []string) int {
|
|
|
|
// Handle -v shorthand
|
|
|
|
for _, arg := range args {
|
|
|
|
if arg == "--" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if arg == "-v" || arg == "-version" || arg == "--version" {
|
|
|
|
args = []string{"version"}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cli := &cli.CLI{
|
|
|
|
Name: "vault",
|
|
|
|
Args: args,
|
|
|
|
Commands: Commands,
|
|
|
|
HelpFunc: helpFunc,
|
|
|
|
|
|
|
|
Autocomplete: true,
|
|
|
|
AutocompleteNoDefaultFlags: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
exitCode, err := cli.Run()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Error executing CLI: %s\n", err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return exitCode
|
|
|
|
}
|
|
|
|
|
|
|
|
// helpFunc is a cli.HelpFunc that can is used to output the help for Vault.
|
|
|
|
func helpFunc(commands map[string]cli.CommandFactory) string {
|
2015-04-21 18:24:27 +00:00
|
|
|
commonNames := map[string]struct{}{
|
2017-09-05 03:50:13 +00:00
|
|
|
"delete": struct{}{},
|
|
|
|
"read": struct{}{},
|
|
|
|
"renew": struct{}{},
|
|
|
|
"revoke": struct{}{},
|
|
|
|
"server": struct{}{},
|
|
|
|
"status": struct{}{},
|
|
|
|
"unwrap": struct{}{},
|
|
|
|
"write": struct{}{},
|
2015-04-21 18:24:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the maximum key length, and classify based on type
|
|
|
|
commonCommands := make(map[string]cli.CommandFactory)
|
|
|
|
otherCommands := make(map[string]cli.CommandFactory)
|
|
|
|
|
2017-09-05 03:50:13 +00:00
|
|
|
commonKeyLen, otherKeyLen := 0, 0
|
|
|
|
for key, f := range commands {
|
2015-04-21 18:24:27 +00:00
|
|
|
if _, ok := commonNames[key]; ok {
|
2017-09-05 03:50:13 +00:00
|
|
|
if len(key) > commonKeyLen {
|
|
|
|
commonKeyLen = len(key)
|
|
|
|
}
|
2015-04-21 18:24:27 +00:00
|
|
|
commonCommands[key] = f
|
|
|
|
} else {
|
2017-09-05 03:50:13 +00:00
|
|
|
if len(key) > otherKeyLen {
|
|
|
|
otherKeyLen = len(key)
|
|
|
|
}
|
2015-04-21 18:24:27 +00:00
|
|
|
otherCommands[key] = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
2017-09-05 03:50:13 +00:00
|
|
|
buf.WriteString("Usage: vault <command> [args]\n\n")
|
|
|
|
buf.WriteString("Common commands:\n\n")
|
|
|
|
buf.WriteString(listCommands(commonCommands, commonKeyLen))
|
|
|
|
buf.WriteString("\n")
|
|
|
|
buf.WriteString("Other commands:\n\n")
|
|
|
|
buf.WriteString(listCommands(otherCommands, otherKeyLen))
|
|
|
|
return strings.TrimSpace(buf.String())
|
2015-04-21 18:24:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// listCommands just lists the commands in the map with the
|
|
|
|
// given maximum key length.
|
|
|
|
func listCommands(commands map[string]cli.CommandFactory, maxKeyLen int) string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
// Get the list of keys so we can sort them, and also get the maximum
|
|
|
|
// key length so they can be aligned properly.
|
|
|
|
keys := make([]string, 0, len(commands))
|
|
|
|
for key, _ := range commands {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
commandFunc, ok := commands[key]
|
|
|
|
if !ok {
|
|
|
|
// This should never happen since we JUST built the list of
|
|
|
|
// keys.
|
|
|
|
panic("command not found: " + key)
|
|
|
|
}
|
|
|
|
|
|
|
|
command, err := commandFunc()
|
|
|
|
if err != nil {
|
2016-08-19 20:45:17 +00:00
|
|
|
panic(fmt.Sprintf("command '%s' failed to load: %s", key, err))
|
2015-04-21 18:24:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
key = fmt.Sprintf("%s%s", key, strings.Repeat(" ", maxKeyLen-len(key)))
|
|
|
|
buf.WriteString(fmt.Sprintf(" %s %s\n", key, command.Synopsis()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|