diff --git a/command/commands.go b/command/commands.go index 76831b38c0..a02cdda9b3 100644 --- a/command/commands.go +++ b/command/commands.go @@ -216,6 +216,7 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { UI: ui, tokenHelper: runOpts.TokenHelper, flagAddress: runOpts.Address, + client: runOpts.Client, } } diff --git a/command/format.go b/command/format.go index 4e378c89cd..c4c55e00e9 100644 --- a/command/format.go +++ b/command/format.go @@ -69,20 +69,18 @@ var Formatters = map[string]Formatter{ "yml": YamlFormatter{}, } -func format() string { - format := os.Getenv(EnvVaultFormat) - if format == "" { - format = "table" - } - return format -} - func Format(ui cli.Ui) string { switch ui.(type) { case *VaultUI: return ui.(*VaultUI).format } - return format() + + format := os.Getenv(EnvVaultFormat) + if format == "" { + format = "table" + } + + return format } // An output formatter for json output of an object diff --git a/command/format_test.go b/command/format_test.go index b97ca27315..c9478301fa 100644 --- a/command/format_test.go +++ b/command/format_test.go @@ -1,6 +1,7 @@ package command import ( + "bytes" "os" "strings" "testing" @@ -91,13 +92,13 @@ func Test_Format_Parsing(t *testing.T) { }{ { "format", - []string{"-format", "json"}, + []string{"token", "renew", "-format", "json"}, "{", 0, }, { "format_bad", - []string{"-format", "nope-not-real"}, + []string{"token", "renew", "-format", "nope-not-real"}, "Invalid output format", 1, }, @@ -110,21 +111,24 @@ func Test_Format_Parsing(t *testing.T) { client, closer := testVaultServer(t) defer closer() + stdout := bytes.NewBuffer(nil) + stderr := bytes.NewBuffer(nil) + runOpts := &RunOptions{ + Stdout: stdout, + Stderr: stderr, + Client: client, + } + // Login with the token so we can renew-self. token, _ := testTokenAndAccessor(t, client) client.SetToken(token) - ui, cmd := testTokenRenewCommand(t) - cmd.client = client - - tc.args = setupEnv(tc.args) - - code := cmd.Run(tc.args) + code := RunCustom(tc.args, runOpts) if code != tc.code { t.Errorf("expected %d to be %d", code, tc.code) } - combined := ui.OutputWriter.String() + ui.ErrorWriter.String() + combined := stdout.String() + stderr.String() if !strings.Contains(combined, tc.out) { t.Errorf("expected %q to contain %q", combined, tc.out) } diff --git a/command/main.go b/command/main.go index 3fd4134be4..9b52e622ae 100644 --- a/command/main.go +++ b/command/main.go @@ -10,6 +10,7 @@ import ( "text/tabwriter" "github.com/fatih/color" + "github.com/hashicorp/vault/api" "github.com/hashicorp/vault/command/token" colorable "github.com/mattn/go-colorable" "github.com/mitchellh/cli" @@ -22,8 +23,7 @@ type VaultUI struct { // 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 +func setupEnv(args []string) (retArgs []string, format string) { var nextArgFormat bool for _, arg := range args { @@ -65,10 +65,8 @@ func setupEnv(args []string) []string { if format == "" { format = "table" } - // Put back into the env for later - os.Setenv(EnvVaultFormat, format) - return args + return args, format } type RunOptions struct { @@ -76,6 +74,7 @@ type RunOptions struct { Stdout io.Writer Stderr io.Writer Address string + Client *api.Client } func Run(args []string) int { @@ -89,7 +88,8 @@ func RunCustom(args []string, runOpts *RunOptions) int { runOpts = &RunOptions{} } - args = setupEnv(args) + var format string + args, format = setupEnv(args) // Don't use color if disabled useColor := true @@ -104,8 +104,6 @@ func RunCustom(args []string, runOpts *RunOptions) int { runOpts.Stderr = os.Stderr } - format := format() - // Only use colored UI if stdout is a tty, and not disabled if useColor && format == "table" { if f, ok := runOpts.Stdout.(*os.File); ok { diff --git a/command/operator_unseal_test.go b/command/operator_unseal_test.go index 7078b17cfc..b9b5c658af 100644 --- a/command/operator_unseal_test.go +++ b/command/operator_unseal_test.go @@ -1,6 +1,7 @@ package command import ( + "bytes" "encoding/json" "io/ioutil" "os" @@ -147,7 +148,6 @@ func TestOperatorUnsealCommand_Run(t *testing.T) { func TestOperatorUnsealCommand_Format(t *testing.T) { defer func() { - os.Setenv(EnvVaultFormat, "") os.Setenv(EnvVaultCLINoColor, "") }() @@ -159,21 +159,28 @@ func TestOperatorUnsealCommand_Format(t *testing.T) { t.Fatal(err) } - ui, cmd := testOperatorUnsealCommand(t) - cmd.client = client - cmd.testOutput = ioutil.Discard - - args := setupEnv([]string{"-format", "json"}) - - // Unseal with one key - code := cmd.Run(append(args, []string{ - keys[0], - }...)) - if exp := 0; code != exp { - t.Errorf("expected %d to be %d: %s", code, exp, ui.ErrorWriter.String()) + stdout := bytes.NewBuffer(nil) + stderr := bytes.NewBuffer(nil) + runOpts := &RunOptions{ + Stdout: stdout, + Stderr: stderr, + Client: client, } - if !json.Valid(ui.OutputWriter.Bytes()) { + args, format := setupEnv([]string{"unseal", "-format", "json"}) + if format != "json" { + t.Fatalf("expected %q, got %q", "json", format) + } + + // Unseal with one key + code := RunCustom(append(args, []string{ + keys[0], + }...), runOpts) + if exp := 0; code != exp { + t.Errorf("expected %d to be %d: %s", code, exp, stderr.String()) + } + + if !json.Valid(stdout.Bytes()) { t.Error("expected output to be valid JSON") } }