diff --git a/command/kv/get/kv_get.go b/command/kv/get/kv_get.go index 93faf94e7c..720f84f39f 100644 --- a/command/kv/get/kv_get.go +++ b/command/kv/get/kv_get.go @@ -169,7 +169,11 @@ func (c *cmd) Run(args []string) int { return 0 } - c.UI.Info(string(pair.Value)) + if c.base64encode { + c.UI.Info(base64.StdEncoding.EncodeToString(pair.Value)) + } else { + c.UI.Info(string(pair.Value)) + } return 0 } } diff --git a/command/kv/get/kv_get_test.go b/command/kv/get/kv_get_test.go index d072c00f27..9151cb518b 100644 --- a/command/kv/get/kv_get_test.go +++ b/command/kv/get/kv_get_test.go @@ -91,6 +91,41 @@ func TestKVGetCommand(t *testing.T) { } } +func TestKVGetCommand_Base64(t *testing.T) { + t.Parallel() + a := agent.NewTestAgent(t.Name(), ``) + defer a.Shutdown() + client := a.Client() + + ui := cli.NewMockUi() + c := New(ui) + + pair := &api.KVPair{ + Key: "foo", + Value: []byte("bar"), + } + _, err := client.KV().Put(pair, nil) + if err != nil { + t.Fatalf("err: %#v", err) + } + + args := []string{ + "-http-addr=" + a.HTTPAddr(), + "-base64", + "foo", + } + + code := c.Run(args) + if code != 0 { + t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) + } + + output := ui.OutputWriter.String() + if !strings.Contains(output, base64.StdEncoding.EncodeToString(pair.Value)) { + t.Errorf("bad: %#v", output) + } +} + func TestKVGetCommand_Missing(t *testing.T) { t.Parallel() a := agent.NewTestAgent(t.Name(), ``)