only check special characters on CLI when not in key=value format (#2998)

This commit is contained in:
Chris Hoffman 2017-07-12 13:28:57 -07:00 committed by GitHub
parent 6651f3aa33
commit a449424bde
2 changed files with 47 additions and 25 deletions

View File

@ -48,33 +48,36 @@ func (b *Builder) add(raw string) error {
return nil
}
// 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")
}
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()
return b.addReader(f)
}
// 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 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)
}
// 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()
return b.addReader(f)
}
}
if len(parts) != 2 {
return fmt.Errorf("format must be key=value")
}

View File

@ -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)
}
}