mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-07-17 01:39:06 +00:00
Simplify map conversion logic
This commit is contained in:
@@ -130,29 +130,19 @@ func (o *addMetadataOptions) convertToMap(arg string) (map[string]string, error)
|
||||
result := make(map[string]string)
|
||||
inputs := strings.Split(arg, ",")
|
||||
for _, input := range inputs {
|
||||
kv := strings.Split(input, ":")
|
||||
if len(kv[0]) < 1 {
|
||||
return nil, o.makeError(input, "empty key")
|
||||
}
|
||||
if len(kv) > 2 {
|
||||
// more than one colon found
|
||||
// check if value is quoted
|
||||
qc := strings.Index(input, ":\"")
|
||||
if qc >= 1 && input[len(input)-1:] == "\"" {
|
||||
//value is quoted
|
||||
result[kv[0]] = input[qc+1:]
|
||||
} else {
|
||||
// value is not quoted, return error
|
||||
return nil, o.makeError(input, "too many colons, quote the values")
|
||||
}
|
||||
} else if len(kv) == 2 {
|
||||
result[kv[0]] = kv[1]
|
||||
c := strings.Index(input, ":")
|
||||
if c == 0 {
|
||||
// key is not passed
|
||||
return nil, o.makeError(input, "need k:v pair where v may be quoted")
|
||||
} else if c < 0 {
|
||||
// only key passed
|
||||
result[input] = ""
|
||||
} else {
|
||||
result[kv[0]] = ""
|
||||
// both key and value passed
|
||||
key := input[:c]
|
||||
value := trimQuotes(input[c+1:])
|
||||
result[key] = value
|
||||
}
|
||||
|
||||
// remove quotes if value is quoted
|
||||
result[kv[0]] = trimQuotes(result[kv[0]])
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
@@ -182,7 +172,7 @@ func (o *addMetadataOptions) writeToMap(m map[string]string, kind kindOfAdd) err
|
||||
}
|
||||
|
||||
func (o *addMetadataOptions) makeError(input string, message string) error {
|
||||
return fmt.Errorf("invalid %s: %s (%s)", o.kind, input, message)
|
||||
return fmt.Errorf("invalid %s: '%s' (%s)", o.kind, input, message)
|
||||
}
|
||||
|
||||
func trimQuotes(s string) string {
|
||||
|
||||
Reference in New Issue
Block a user