2017-09-05 03:50:13 +00:00
|
|
|
package command
|
2015-04-21 18:24:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2017-09-08 02:05:54 +00:00
|
|
|
"io"
|
2017-09-05 03:50:13 +00:00
|
|
|
"os"
|
2015-04-21 18:24:27 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2017-09-08 02:05:54 +00:00
|
|
|
"text/tabwriter"
|
2015-04-21 18:24:27 +00:00
|
|
|
|
2018-03-16 16:31:26 +00:00
|
|
|
"github.com/hashicorp/vault/command/token"
|
2018-04-05 17:28:02 +00:00
|
|
|
colorable "github.com/mattn/go-colorable"
|
2015-04-21 18:24:27 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2018-02-12 23:12:16 +00:00
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
2015-04-21 18:24:27 +00:00
|
|
|
)
|
|
|
|
|
2018-02-12 23:12:16 +00:00
|
|
|
type VaultUI struct {
|
|
|
|
cli.Ui
|
|
|
|
isTerminal bool
|
|
|
|
format string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *VaultUI) Output(m string) {
|
|
|
|
if u.isTerminal {
|
|
|
|
u.Ui.Output(m)
|
|
|
|
} else {
|
2018-02-13 19:46:57 +00:00
|
|
|
writer := getWriterFromUI(u.Ui)
|
|
|
|
writer.Write([]byte(m))
|
|
|
|
writer.Write([]byte("\n"))
|
2018-02-12 23:12:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// setupEnv parses args and may replace them and sets some env vars to known
|
|
|
|
// values based on format options
|
|
|
|
func setupEnv(args []string) []string {
|
|
|
|
var format string
|
|
|
|
var nextArgFormat bool
|
|
|
|
|
2017-09-05 03:50:13 +00:00
|
|
|
for _, arg := range args {
|
2018-02-12 23:12:16 +00:00
|
|
|
if nextArgFormat {
|
|
|
|
nextArgFormat = false
|
|
|
|
format = arg
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-09-05 03:50:13 +00:00
|
|
|
if arg == "--" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if arg == "-v" || arg == "-version" || arg == "--version" {
|
|
|
|
args = []string{"version"}
|
|
|
|
break
|
|
|
|
}
|
2018-02-12 23:12:16 +00:00
|
|
|
|
|
|
|
// Parse a given flag here, which overrides the env var
|
|
|
|
if strings.HasPrefix(arg, "--format=") {
|
|
|
|
format = strings.TrimPrefix(arg, "--format=")
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(arg, "-format=") {
|
|
|
|
format = strings.TrimPrefix(arg, "-format=")
|
|
|
|
}
|
|
|
|
// For backwards compat, it could be specified without an equal sign
|
|
|
|
if arg == "-format" || arg == "--format" {
|
|
|
|
nextArgFormat = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
envVaultFormat := os.Getenv(EnvVaultFormat)
|
|
|
|
// If we did not parse a value, fetch the env var
|
|
|
|
if format == "" && envVaultFormat != "" {
|
|
|
|
format = envVaultFormat
|
|
|
|
}
|
|
|
|
// Lowercase for consistency
|
|
|
|
format = strings.ToLower(format)
|
|
|
|
if format == "" {
|
|
|
|
format = "table"
|
|
|
|
}
|
|
|
|
// Put back into the env for later
|
|
|
|
os.Setenv(EnvVaultFormat, format)
|
|
|
|
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
2018-03-16 16:31:26 +00:00
|
|
|
type RunOptions struct {
|
|
|
|
TokenHelper token.TokenHelper
|
|
|
|
Stdout io.Writer
|
|
|
|
Stderr io.Writer
|
2018-03-16 17:14:32 +00:00
|
|
|
Address string
|
2018-03-16 16:31:26 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 23:12:16 +00:00
|
|
|
func Run(args []string) int {
|
2018-03-16 15:31:00 +00:00
|
|
|
return RunCustom(args, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunCustom allows passing in a base command template to pass to other
|
|
|
|
// commands. Currenty, this is only used for setting a custom token helper.
|
2018-03-16 16:31:26 +00:00
|
|
|
func RunCustom(args []string, runOpts *RunOptions) int {
|
|
|
|
if runOpts == nil {
|
|
|
|
runOpts = &RunOptions{}
|
|
|
|
}
|
|
|
|
if runOpts.Stdout == nil {
|
2018-04-05 17:28:02 +00:00
|
|
|
runOpts.Stdout = colorable.NewColorable(os.Stdout)
|
2018-03-16 16:31:26 +00:00
|
|
|
}
|
|
|
|
if runOpts.Stderr == nil {
|
2018-04-05 17:28:02 +00:00
|
|
|
runOpts.Stderr = colorable.NewColorable(os.Stderr)
|
2018-03-16 15:31:00 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 23:12:16 +00:00
|
|
|
args = setupEnv(args)
|
|
|
|
|
|
|
|
// Don't use color if disabled
|
|
|
|
color := true
|
|
|
|
if os.Getenv(EnvVaultCLINoColor) != "" {
|
|
|
|
color = false
|
|
|
|
}
|
|
|
|
|
|
|
|
format := format()
|
|
|
|
|
|
|
|
isTerminal := terminal.IsTerminal(int(os.Stdout.Fd()))
|
|
|
|
|
|
|
|
ui := &VaultUI{
|
|
|
|
Ui: &cli.BasicUi{
|
2018-03-16 16:31:26 +00:00
|
|
|
Writer: runOpts.Stdout,
|
|
|
|
ErrorWriter: runOpts.Stderr,
|
2018-02-12 23:12:16 +00:00
|
|
|
},
|
|
|
|
isTerminal: isTerminal,
|
|
|
|
format: format,
|
|
|
|
}
|
|
|
|
serverCmdUi := &VaultUI{
|
|
|
|
Ui: &cli.BasicUi{
|
2018-03-16 16:31:26 +00:00
|
|
|
Writer: runOpts.Stdout,
|
2018-02-12 23:12:16 +00:00
|
|
|
},
|
|
|
|
isTerminal: isTerminal,
|
|
|
|
format: format,
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := Formatters[format]; !ok {
|
|
|
|
ui.Error(fmt.Sprintf("Invalid output format: %s", format))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:54:10 +00:00
|
|
|
// Only use colored UI if stdout is a tty, and not disabled
|
2018-02-12 23:12:16 +00:00
|
|
|
if isTerminal && color && format == "table" {
|
|
|
|
ui.Ui = &cli.ColoredUi{
|
|
|
|
ErrorColor: cli.UiColorRed,
|
|
|
|
WarnColor: cli.UiColorYellow,
|
|
|
|
Ui: ui.Ui,
|
|
|
|
}
|
|
|
|
|
|
|
|
serverCmdUi.Ui = &cli.ColoredUi{
|
|
|
|
ErrorColor: cli.UiColorRed,
|
|
|
|
WarnColor: cli.UiColorYellow,
|
|
|
|
Ui: serverCmdUi.Ui,
|
|
|
|
}
|
2017-09-05 03:50:13 +00:00
|
|
|
}
|
|
|
|
|
2018-03-16 16:31:26 +00:00
|
|
|
initCommands(ui, serverCmdUi, runOpts)
|
2018-02-12 23:12:16 +00:00
|
|
|
|
2017-09-22 00:51:25 +00:00
|
|
|
// Calculate hidden commands from the deprecated ones
|
|
|
|
hiddenCommands := make([]string, 0, len(DeprecatedCommands)+1)
|
|
|
|
for k := range DeprecatedCommands {
|
|
|
|
hiddenCommands = append(hiddenCommands, k)
|
|
|
|
}
|
|
|
|
hiddenCommands = append(hiddenCommands, "version")
|
|
|
|
|
2017-09-05 03:50:13 +00:00
|
|
|
cli := &cli.CLI{
|
|
|
|
Name: "vault",
|
|
|
|
Args: args,
|
|
|
|
Commands: Commands,
|
2017-09-22 00:51:25 +00:00
|
|
|
HelpFunc: groupedHelpFunc(
|
|
|
|
cli.BasicHelpFunc("vault"),
|
2017-09-08 02:05:54 +00:00
|
|
|
),
|
2018-03-16 16:59:52 +00:00
|
|
|
HelpWriter: runOpts.Stderr,
|
2017-09-22 00:51:25 +00:00
|
|
|
HiddenCommands: hiddenCommands,
|
2017-09-05 03:50:13 +00:00
|
|
|
Autocomplete: true,
|
|
|
|
AutocompleteNoDefaultFlags: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
exitCode, err := cli.Run()
|
|
|
|
if err != nil {
|
2018-03-16 16:31:26 +00:00
|
|
|
fmt.Fprintf(runOpts.Stderr, "Error executing CLI: %s\n", err.Error())
|
2017-09-05 03:50:13 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return exitCode
|
|
|
|
}
|
|
|
|
|
2017-09-08 02:05:54 +00:00
|
|
|
var commonCommands = []string{
|
|
|
|
"read",
|
|
|
|
"write",
|
|
|
|
"delete",
|
|
|
|
"list",
|
|
|
|
"login",
|
|
|
|
"server",
|
|
|
|
"status",
|
|
|
|
"unwrap",
|
2015-04-21 18:24:27 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 02:05:54 +00:00
|
|
|
func groupedHelpFunc(f cli.HelpFunc) cli.HelpFunc {
|
|
|
|
return func(commands map[string]cli.CommandFactory) string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
tw := tabwriter.NewWriter(&b, 0, 2, 6, ' ', 0)
|
2015-04-21 18:24:27 +00:00
|
|
|
|
2017-09-08 02:05:54 +00:00
|
|
|
fmt.Fprintf(tw, "Usage: vault <command> [args]\n\n")
|
|
|
|
fmt.Fprintf(tw, "Common commands:\n")
|
|
|
|
for _, v := range commonCommands {
|
|
|
|
printCommand(tw, v, commands[v])
|
2015-04-21 18:24:27 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 02:05:54 +00:00
|
|
|
otherCommands := make([]string, 0, len(commands))
|
|
|
|
for k := range commands {
|
|
|
|
found := false
|
|
|
|
for _, v := range commonCommands {
|
|
|
|
if k == v {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
otherCommands = append(otherCommands, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(otherCommands)
|
|
|
|
|
|
|
|
fmt.Fprintf(tw, "\n")
|
|
|
|
fmt.Fprintf(tw, "Other commands:\n")
|
|
|
|
for _, v := range otherCommands {
|
|
|
|
printCommand(tw, v, commands[v])
|
2015-04-21 18:24:27 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 02:05:54 +00:00
|
|
|
tw.Flush()
|
|
|
|
|
|
|
|
return strings.TrimSpace(b.String())
|
2015-04-21 18:24:27 +00:00
|
|
|
}
|
2017-09-08 02:05:54 +00:00
|
|
|
}
|
2015-04-21 18:24:27 +00:00
|
|
|
|
2017-09-08 02:05:54 +00:00
|
|
|
func printCommand(w io.Writer, name string, cmdFn cli.CommandFactory) {
|
|
|
|
cmd, err := cmdFn()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to load %q command: %s", name, err))
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, " %s\t%s\n", name, cmd.Synopsis())
|
2015-04-21 18:24:27 +00:00
|
|
|
}
|