From 51915ab08465d28778dce721288dc444b74c196f Mon Sep 17 00:00:00 2001 From: Chris Hoffman Date: Wed, 12 Jul 2017 08:27:56 -0700 Subject: [PATCH] only check special characters on CLI when not in key=value format --- helper/kv-builder/builder.go | 41 +++++++++++++++++-------------- helper/kv-builder/builder_test.go | 19 ++++++++++++++ 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/helper/kv-builder/builder.go b/helper/kv-builder/builder.go index 7ecf7540dffb..685624a12590 100644 --- a/helper/kv-builder/builder.go +++ b/helper/kv-builder/builder.go @@ -48,33 +48,36 @@ func (b *Builder) add(raw string) error { return nil } + // Split into key/value + parts := strings.SplitN(raw, "=", 2) + // If the arg is exactly "-", then we need to read from stdin // and merge the results into the resulting structure. - if raw == "-" { - if b.Stdin == nil { - return fmt.Errorf("stdin is not supported") - } - if b.stdin { - return fmt.Errorf("stdin already consumed") + if len(parts) == 1 { + if raw == "-" { + if b.Stdin == nil { + return fmt.Errorf("stdin is not supported") + } + if b.stdin { + return fmt.Errorf("stdin already consumed") + } + + b.stdin = true + return b.addReader(b.Stdin) } - b.stdin = true - return b.addReader(b.Stdin) - } + // If the arg begins with "@" then we need to read a file directly + if raw[0] == '@' { + f, err := os.Open(raw[1:]) + if err != nil { + return err + } + defer f.Close() - // If the arg begins with "@" then we need to read a file directly - if raw[0] == '@' { - f, err := os.Open(raw[1:]) - if err != nil { - return err + return b.addReader(f) } - defer f.Close() - - return b.addReader(f) } - // Split into key/value - parts := strings.SplitN(raw, "=", 2) if len(parts) != 2 { return fmt.Errorf("format must be key=value") } diff --git a/helper/kv-builder/builder_test.go b/helper/kv-builder/builder_test.go index 9b0cffbc41ba..aa31784921b9 100644 --- a/helper/kv-builder/builder_test.go +++ b/helper/kv-builder/builder_test.go @@ -118,3 +118,22 @@ func TestBuilder_sameKeyMultipleTimes(t *testing.T) { t.Fatalf("bad: %#v", actual) } } + +func TestBuilder_specialCharacteresInKey(t *testing.T) { + var b Builder + b.Stdin = bytes.NewBufferString("{\"foo\": \"bay\"}") + err := b.Add("@foo=bar", "-foo=baz", "-") + if err != nil { + t.Fatalf("err: %s", err) + } + + expected := map[string]interface{}{ + "@foo": "bar", + "-foo": "baz", + "foo": "bay", + } + actual := b.Map() + if !reflect.DeepEqual(actual, expected) { + t.Fatalf("bad: %#v", actual) + } +}