From c676e6b8c8fc2856375d88d9e14407b2643fe534 Mon Sep 17 00:00:00 2001 From: James Phillips Date: Thu, 14 Dec 2017 17:27:45 -0800 Subject: [PATCH] Adds -base64 support to kv get command. Fixes #3736 --- command/kv/get/kv_get.go | 6 +++++- command/kv/get/kv_get_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) 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(), ``)