Skip to content

Commit

Permalink
only check special characters on CLI when not in key=value format (#2998
Browse files Browse the repository at this point in the history
)
  • Loading branch information
chrishoffman committed Jul 12, 2017
1 parent e317790 commit 5c5b3e6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
41 changes: 22 additions & 19 deletions helper/kv-builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
19 changes: 19 additions & 0 deletions helper/kv-builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 5c5b3e6

Please sign in to comment.