diff --git a/config/cmdline/cmdline.go b/config/cmdline/cmdline.go index 4dcb49cb..09f448cb 100644 --- a/config/cmdline/cmdline.go +++ b/config/cmdline/cmdline.go @@ -3,6 +3,7 @@ package cmdline import ( "io/ioutil" "strings" + "unicode" "github.com/rancher/os/pkg/util" @@ -125,11 +126,34 @@ func UnmarshalOrReturnString(value string) (result interface{}) { return } +//splitCmdLine splits on spaces except when a space is within a quoted or bracketed string. +func splitCmdLine(cmdLine string) []string { + lastRune := rune(0) + f := func(c rune) bool { + switch { + case c == lastRune: + lastRune = rune(0) + return false + case lastRune != rune(0): + return false + case unicode.In(c, unicode.Quotation_Mark): + lastRune = c + return false + case c == '[': + lastRune = ']' + return false + default: + return c == ' ' + } + } + return strings.FieldsFunc(cmdLine, f) +} + func Parse(cmdLine string, parseAll bool) map[interface{}]interface{} { result := map[interface{}]interface{}{} outer: - for _, part := range strings.Split(cmdLine, " ") { + for _, part := range splitCmdLine(cmdLine) { if strings.HasPrefix(part, "cc.") { part = part[3:] } else if !strings.HasPrefix(part, "rancher.") { diff --git a/config/config_test.go b/config/config_test.go index a74f3779..37145ca8 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -123,6 +123,12 @@ func TestCmdlineParse(t *testing.T) { }, }, cmdline.Parse("rancher.key=a\nb", false), false) + assert.Equal(map[interface{}]interface{}{ + "rancher": map[interface{}]interface{}{ + "key": "a b", + }, + }, cmdline.Parse("rancher.key='a b'", false), false) + assert.Equal(map[interface{}]interface{}{ "rancher": map[interface{}]interface{}{ "key": "a:b", @@ -158,6 +164,24 @@ func TestCmdlineParse(t *testing.T) { "strArray": []interface{}{"url:http://192.168.1.100/cloud-config?a=b"}, }, }, cmdline.Parse("rancher.strArray=[url:http://192.168.1.100/cloud-config?a=b]", false), false) + + assert.Equal(map[interface{}]interface{}{ + "rancher": map[interface{}]interface{}{ + "strArray": []interface{}{"part1 part2", "part3"}, + }, + }, cmdline.Parse("rancher.strArray=['part1 part2',part3]", false), false) + + assert.Equal(map[interface{}]interface{}{ + "rancher": map[interface{}]interface{}{ + "strArray": []interface{}{"part1 part2", "part3"}, + }, + }, cmdline.Parse("rancher.strArray=[\"part1 part2\",part3]", false), false) + + assert.Equal(map[interface{}]interface{}{ + "rancher": map[interface{}]interface{}{ + "strArray": []interface{}{"part1 part2", "part3"}, + }, + }, cmdline.Parse("rancher.strArray=[ \"part1 part2\", part3 ]", false), false) } func TestGet(t *testing.T) {