Skip to content

Commit ea3d5e6

Browse files
committed
Fix for #818 - Added support for quoted values
1 parent 16d1b20 commit ea3d5e6

File tree

2 files changed

+79
-5
lines changed

2 files changed

+79
-5
lines changed

pkg/commands/edit/add/addmetadata.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,28 @@ func (o *addMetadataOptions) convertToMap(arg string) (map[string]string, error)
135135
return nil, o.makeError(input, "empty key")
136136
}
137137
if len(kv) > 2 {
138-
return nil, o.makeError(input, "too many colons")
139-
}
140-
if len(kv) > 1 {
138+
// more than one colon found
139+
// check if value is quoted
140+
qc := strings.Index(input, ":\"")
141+
if qc >= 1 && input[len(input)-1:] == "\"" {
142+
//value is quoted
143+
result[kv[0]] = input[qc+1:]
144+
} else {
145+
// value is not quoted, return error
146+
return nil, o.makeError(input, "too many colons, quote the values")
147+
}
148+
} else if len(kv) == 2 {
141149
result[kv[0]] = kv[1]
142150
} else {
143151
result[kv[0]] = ""
144152
}
153+
154+
// remove quotes if value is quoted
155+
if len(result[kv[0]]) > 0 &&
156+
result[kv[0]][:1] == "\"" &&
157+
result[kv[0]][len(result[kv[0]])-1:] == "\"" {
158+
result[kv[0]] = result[kv[0]][1 : len(result[kv[0]])-1]
159+
}
145160
}
146161
return result, nil
147162
}

pkg/commands/edit/add/addmetadata_test.go

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package add
1818

1919
import (
20+
"reflect"
2021
"testing"
2122

2223
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
@@ -103,6 +104,32 @@ func TestAddAnnotationManyArgs(t *testing.T) {
103104
}
104105
}
105106

107+
func TestAddAnnotationValueQuoted(t *testing.T) {
108+
fakeFS := fs.MakeFakeFS()
109+
fakeFS.WriteTestKustomization()
110+
v := validators.MakeHappyMapValidator(t)
111+
cmd := newCmdAddAnnotation(fakeFS, v.Validator)
112+
args := []string{"k1:\"v1\""}
113+
err := cmd.RunE(cmd, args)
114+
v.VerifyCall()
115+
if err != nil {
116+
t.Errorf("unexpected error: %v", err.Error())
117+
}
118+
}
119+
120+
func TestAddAnnotationValueWithColon(t *testing.T) {
121+
fakeFS := fs.MakeFakeFS()
122+
fakeFS.WriteTestKustomization()
123+
v := validators.MakeHappyMapValidator(t)
124+
cmd := newCmdAddAnnotation(fakeFS, v.Validator)
125+
args := []string{"k1:\"v1:v2\""}
126+
err := cmd.RunE(cmd, args)
127+
v.VerifyCall()
128+
if err != nil {
129+
t.Errorf("unexpected error: %v", err.Error())
130+
}
131+
}
132+
106133
func TestAddAnnotationNoKey(t *testing.T) {
107134
fakeFS := fs.MakeFakeFS()
108135
v := validators.MakeHappyMapValidator(t)
@@ -128,7 +155,7 @@ func TestAddAnnotationTooManyColons(t *testing.T) {
128155
if err == nil {
129156
t.Errorf("expected an error")
130157
}
131-
if err.Error() != "invalid annotation: key:v1:v2 (too many colons)" {
158+
if err.Error() != "invalid annotation: key:v1:v2 (too many colons, quote the values)" {
132159
t.Errorf("incorrect error: %v", err.Error())
133160
}
134161
}
@@ -238,7 +265,7 @@ func TestAddLabelTooManyColons(t *testing.T) {
238265
if err == nil {
239266
t.Errorf("expected an error")
240267
}
241-
if err.Error() != "invalid label: key:v1:v2 (too many colons)" {
268+
if err.Error() != "invalid label: key:v1:v2 (too many colons, quote the values)" {
242269
t.Errorf("incorrect error: %v", err.Error())
243270
}
244271
}
@@ -271,3 +298,35 @@ func TestAddLabelMultipleArgs(t *testing.T) {
271298
t.Errorf("incorrect error: %v", err.Error())
272299
}
273300
}
301+
302+
func TestConvertToMap(t *testing.T) {
303+
var o addMetadataOptions
304+
args := "a:b,c:\"d\",e:\"f:g\""
305+
expected := make(map[string]string)
306+
expected["a"] = "b"
307+
expected["c"] = "d"
308+
expected["e"] = "f:g"
309+
310+
result, err := o.convertToMap(args)
311+
if err != nil {
312+
t.Errorf("unexpected error: %v", err.Error())
313+
}
314+
315+
eq := reflect.DeepEqual(expected, result)
316+
if !eq {
317+
t.Errorf("Converted map does not match expected, expected: %v, result: %v\n", expected, result)
318+
}
319+
}
320+
321+
func TestConvertToMapError(t *testing.T) {
322+
var o addMetadataOptions
323+
args := "a:b,c:\"d\",e:f:g"
324+
325+
_, err := o.convertToMap(args)
326+
if err == nil {
327+
t.Errorf("expected an error")
328+
}
329+
if err.Error() != "invalid annotation: e:f:g (too many colons, quote the values)" {
330+
t.Errorf("incorrect error: %v", err.Error())
331+
}
332+
}

0 commit comments

Comments
 (0)