Skip to content

Commit a6f6514

Browse files
authored
Merge pull request #842 from narayanan/annotation-with-colon
Fix for #818 - Added support for quoted values
2 parents 31ee38b + e666630 commit a6f6514

File tree

2 files changed

+91
-25
lines changed

2 files changed

+91
-25
lines changed

pkg/commands/edit/add/addmetadata.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,18 @@ func (o *addMetadataOptions) convertToMap(arg string) (map[string]string, error)
130130
result := make(map[string]string)
131131
inputs := strings.Split(arg, ",")
132132
for _, input := range inputs {
133-
kv := strings.Split(input, ":")
134-
if len(kv[0]) < 1 {
135-
return nil, o.makeError(input, "empty key")
136-
}
137-
if len(kv) > 2 {
138-
return nil, o.makeError(input, "too many colons")
139-
}
140-
if len(kv) > 1 {
141-
result[kv[0]] = kv[1]
133+
c := strings.Index(input, ":")
134+
if c == 0 {
135+
// key is not passed
136+
return nil, o.makeError(input, "need k:v pair where v may be quoted")
137+
} else if c < 0 {
138+
// only key passed
139+
result[input] = ""
142140
} else {
143-
result[kv[0]] = ""
141+
// both key and value passed
142+
key := input[:c]
143+
value := trimQuotes(input[c+1:])
144+
result[key] = value
144145
}
145146
}
146147
return result, nil
@@ -171,5 +172,14 @@ func (o *addMetadataOptions) writeToMap(m map[string]string, kind kindOfAdd) err
171172
}
172173

173174
func (o *addMetadataOptions) makeError(input string, message string) error {
174-
return fmt.Errorf("invalid %s: %s (%s)", o.kind, input, message)
175+
return fmt.Errorf("invalid %s: '%s' (%s)", o.kind, input, message)
176+
}
177+
178+
func trimQuotes(s string) string {
179+
if len(s) >= 2 {
180+
if s[0] == '"' && s[len(s)-1] == '"' {
181+
return s[1 : len(s)-1]
182+
}
183+
}
184+
return s
175185
}

pkg/commands/edit/add/addmetadata_test.go

Lines changed: 70 additions & 14 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)
@@ -113,23 +140,21 @@ func TestAddAnnotationNoKey(t *testing.T) {
113140
if err == nil {
114141
t.Errorf("expected an error")
115142
}
116-
if err.Error() != "invalid annotation: :nokey (empty key)" {
143+
if err.Error() != "invalid annotation: ':nokey' (need k:v pair where v may be quoted)" {
117144
t.Errorf("incorrect error: %v", err.Error())
118145
}
119146
}
120147

121148
func TestAddAnnotationTooManyColons(t *testing.T) {
122149
fakeFS := fs.MakeFakeFS()
150+
fakeFS.WriteTestKustomization()
123151
v := validators.MakeHappyMapValidator(t)
124152
cmd := newCmdAddAnnotation(fakeFS, v.Validator)
125153
args := []string{"key:v1:v2"}
126154
err := cmd.RunE(cmd, args)
127-
v.VerifyNoCall()
128-
if err == nil {
129-
t.Errorf("expected an error")
130-
}
131-
if err.Error() != "invalid annotation: key:v1:v2 (too many colons)" {
132-
t.Errorf("incorrect error: %v", err.Error())
155+
v.VerifyCall()
156+
if err != nil {
157+
t.Errorf("unexpected error: %v", err.Error())
133158
}
134159
}
135160

@@ -223,23 +248,21 @@ func TestAddLabelNoKey(t *testing.T) {
223248
if err == nil {
224249
t.Errorf("expected an error")
225250
}
226-
if err.Error() != "invalid label: :nokey (empty key)" {
251+
if err.Error() != "invalid label: ':nokey' (need k:v pair where v may be quoted)" {
227252
t.Errorf("incorrect error: %v", err.Error())
228253
}
229254
}
230255

231256
func TestAddLabelTooManyColons(t *testing.T) {
232257
fakeFS := fs.MakeFakeFS()
258+
fakeFS.WriteTestKustomization()
233259
v := validators.MakeHappyMapValidator(t)
234260
cmd := newCmdAddLabel(fakeFS, v.Validator)
235261
args := []string{"key:v1:v2"}
236262
err := cmd.RunE(cmd, args)
237-
v.VerifyNoCall()
238-
if err == nil {
239-
t.Errorf("expected an error")
240-
}
241-
if err.Error() != "invalid label: key:v1:v2 (too many colons)" {
242-
t.Errorf("incorrect error: %v", err.Error())
263+
v.VerifyCall()
264+
if err != nil {
265+
t.Errorf("unexpected error: %v", err.Error())
243266
}
244267
}
245268

@@ -271,3 +294,36 @@ func TestAddLabelMultipleArgs(t *testing.T) {
271294
t.Errorf("incorrect error: %v", err.Error())
272295
}
273296
}
297+
298+
func TestConvertToMap(t *testing.T) {
299+
var o addMetadataOptions
300+
args := "a:b,c:\"d\",e:\"f:g\",g:h:k"
301+
expected := make(map[string]string)
302+
expected["a"] = "b"
303+
expected["c"] = "d"
304+
expected["e"] = "f:g"
305+
expected["g"] = "h:k"
306+
307+
result, err := o.convertToMap(args)
308+
if err != nil {
309+
t.Errorf("unexpected error: %v", err.Error())
310+
}
311+
312+
eq := reflect.DeepEqual(expected, result)
313+
if !eq {
314+
t.Errorf("Converted map does not match expected, expected: %v, result: %v\n", expected, result)
315+
}
316+
}
317+
318+
func TestConvertToMapError(t *testing.T) {
319+
var o addMetadataOptions
320+
args := "a:b,c:\"d\",:f:g"
321+
322+
_, err := o.convertToMap(args)
323+
if err == nil {
324+
t.Errorf("expected an error")
325+
}
326+
if err.Error() != "invalid annotation: ':f:g' (need k:v pair where v may be quoted)" {
327+
t.Errorf("incorrect error: %v", err.Error())
328+
}
329+
}

0 commit comments

Comments
 (0)