From 40a8f13cd93fc4da4e98ff4ca261ca4a2fdd4c16 Mon Sep 17 00:00:00 2001 From: mckennajones Date: Sun, 27 Nov 2016 12:55:14 -0800 Subject: [PATCH 1/2] Simple check to differentiate between stdin flag or value that contains a - --- command/kv_put.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/command/kv_put.go b/command/kv_put.go index 28c5d98fa1..4c214a6653 100644 --- a/command/kv_put.go +++ b/command/kv_put.go @@ -229,8 +229,12 @@ func (c *KVPutCommand) dataFromArgs(args []string) (string, string, error) { return key, string(data), nil case '-': var b bytes.Buffer - if _, err := io.Copy(&b, stdin); err != nil { - return "", "", fmt.Errorf("Failed to read stdin: %s", err) + if len(data) > 1 { + return key, data, nil + } else { + if _, err := io.Copy(&b, stdin); err != nil { + return "", "", fmt.Errorf("Failed to read stdin: %s", err) + } } return key, b.String(), nil default: From 6bc116482582e0c37debe705c9f640b96b9689b0 Mon Sep 17 00:00:00 2001 From: mckennajones Date: Tue, 29 Nov 2016 22:29:31 -0800 Subject: [PATCH 2/2] Unit test for kv_put with negative values --- command/kv_put.go | 4 ++-- command/kv_put_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/command/kv_put.go b/command/kv_put.go index 4c214a6653..f5e1fa5de2 100644 --- a/command/kv_put.go +++ b/command/kv_put.go @@ -228,15 +228,15 @@ func (c *KVPutCommand) dataFromArgs(args []string) (string, string, error) { } return key, string(data), nil case '-': - var b bytes.Buffer if len(data) > 1 { return key, data, nil } else { + var b bytes.Buffer if _, err := io.Copy(&b, stdin); err != nil { return "", "", fmt.Errorf("Failed to read stdin: %s", err) } + return key, b.String(), nil } - return key, b.String(), nil default: return key, data, nil } diff --git a/command/kv_put_test.go b/command/kv_put_test.go index 8239809cc8..5b84a68b38 100644 --- a/command/kv_put_test.go +++ b/command/kv_put_test.go @@ -194,6 +194,34 @@ func TestKVPutCommand_Stdin(t *testing.T) { } } +func TestKVPutCommand_NegativeVal(t *testing.T) { + srv, client := testAgentWithAPIClient(t) + defer srv.Shutdown() + waitForLeader(t, srv.httpAddr) + + ui := new(cli.MockUi) + c := &KVPutCommand{Ui: ui} + + args := []string{ + "-http-addr=" + srv.httpAddr, + "foo", "-2", + } + + code := c.Run(args) + if code != 0 { + t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) + } + + data, _, err := client.KV().Get("foo", nil) + if err != nil { + t.Fatal(err) + } + + if !bytes.Equal(data.Value, []byte("-2")) { + t.Errorf("bad: %#v", data.Value) + } +} + func TestKVPutCommand_Flags(t *testing.T) { srv, client := testAgentWithAPIClient(t) defer srv.Shutdown()