mirror of
https://github.com/rancher/os.git
synced 2025-07-07 20:08:37 +00:00
Colons can be parsed without quotes
This commit is contained in:
parent
2781eab500
commit
c1582f5a49
@ -122,6 +122,12 @@ func TestParseCmdline(t *testing.T) {
|
||||
},
|
||||
}, 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{}{
|
||||
"rancher": map[interface{}]interface{}{
|
||||
"key": int64(5),
|
||||
@ -145,6 +151,12 @@ func TestParseCmdline(t *testing.T) {
|
||||
"strArray": []interface{}{"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) {
|
||||
|
@ -116,9 +116,12 @@ func getOrSetVal(args string, data map[interface{}]interface{}, value interface{
|
||||
return "", tData
|
||||
}
|
||||
|
||||
// YAML parsers will remove newlines, but we'd like to keep those
|
||||
// replace newlines with magicString, and then undo after unmarshaling
|
||||
var magicString = "9XsJcx6dR5EERYCC"
|
||||
// Replace newlines and colons with random strings
|
||||
// This is done to avoid YAML treating these as special characters
|
||||
var (
|
||||
newlineMagicString = "9XsJcx6dR5EERYCC"
|
||||
colonMagicString = "V0Rc21pIVknMm2rr"
|
||||
)
|
||||
|
||||
func reverseReplacement(result interface{}) interface{} {
|
||||
switch val := result.(type) {
|
||||
@ -133,14 +136,17 @@ func reverseReplacement(result interface{}) interface{} {
|
||||
}
|
||||
return val
|
||||
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
|
||||
}
|
||||
|
||||
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 {
|
||||
result = value
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user