1
0
mirror of https://github.com/rancher/os.git synced 2025-07-07 11:58:38 +00:00

Colons can be parsed without quotes

This commit is contained in:
Josh Curl 2016-09-18 16:35:32 -07:00
parent 2781eab500
commit c1582f5a49
No known key found for this signature in database
GPG Key ID: 82B504B9BCCFA677
2 changed files with 23 additions and 5 deletions

View File

@ -122,6 +122,12 @@ func TestParseCmdline(t *testing.T) {
}, },
}, parseCmdline("rancher.key=a\nb")) }, parseCmdline("rancher.key=a\nb"))
assert.Equal(map[interface{}]interface{}{
"rancher": map[interface{}]interface{}{
"key": "a:b",
},
}, parseCmdline("rancher.key=a:b"))
assert.Equal(map[interface{}]interface{}{ assert.Equal(map[interface{}]interface{}{
"rancher": map[interface{}]interface{}{ "rancher": map[interface{}]interface{}{
"key": int64(5), "key": int64(5),
@ -145,6 +151,12 @@ func TestParseCmdline(t *testing.T) {
"strArray": []interface{}{"url:http://192.168.1.100/cloud-config"}, "strArray": []interface{}{"url:http://192.168.1.100/cloud-config"},
}, },
}, parseCmdline("rancher.strArray=[\"url:http://192.168.1.100/cloud-config\"]")) }, parseCmdline("rancher.strArray=[\"url:http://192.168.1.100/cloud-config\"]"))
assert.Equal(map[interface{}]interface{}{
"rancher": map[interface{}]interface{}{
"strArray": []interface{}{"url:http://192.168.1.100/cloud-config"},
},
}, parseCmdline("rancher.strArray=[url:http://192.168.1.100/cloud-config]"))
} }
func TestGet(t *testing.T) { func TestGet(t *testing.T) {

View File

@ -116,9 +116,12 @@ func getOrSetVal(args string, data map[interface{}]interface{}, value interface{
return "", tData return "", tData
} }
// YAML parsers will remove newlines, but we'd like to keep those // Replace newlines and colons with random strings
// replace newlines with magicString, and then undo after unmarshaling // This is done to avoid YAML treating these as special characters
var magicString = "9XsJcx6dR5EERYCC" var (
newlineMagicString = "9XsJcx6dR5EERYCC"
colonMagicString = "V0Rc21pIVknMm2rr"
)
func reverseReplacement(result interface{}) interface{} { func reverseReplacement(result interface{}) interface{} {
switch val := result.(type) { switch val := result.(type) {
@ -133,14 +136,17 @@ func reverseReplacement(result interface{}) interface{} {
} }
return val return val
case string: case string:
return strings.Replace(val, magicString, "\n", -1) val = strings.Replace(val, newlineMagicString, "\n", -1)
val = strings.Replace(val, colonMagicString, ":", -1)
return val
} }
return result return result
} }
func unmarshalOrReturnString(value string) (result interface{}) { func unmarshalOrReturnString(value string) (result interface{}) {
value = strings.Replace(value, "\n", magicString, -1) value = strings.Replace(value, "\n", newlineMagicString, -1)
value = strings.Replace(value, ":", colonMagicString, -1)
if err := yaml.Unmarshal([]byte(value), &result); err != nil { if err := yaml.Unmarshal([]byte(value), &result); err != nil {
result = value result = value
} }