Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix for #818 - Added support for quoted values #842

Merged
merged 3 commits into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions pkg/commands/edit/add/addmetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,28 @@ func (o *addMetadataOptions) convertToMap(arg string) (map[string]string, error)
return nil, o.makeError(input, "empty key")
}
if len(kv) > 2 {
return nil, o.makeError(input, "too many colons")
}
if len(kv) > 1 {
// 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]
} else {
result[kv[0]] = ""
}

// remove quotes if value is quoted
if len(result[kv[0]]) > 0 &&
result[kv[0]][:1] == "\"" &&
result[kv[0]][len(result[kv[0]])-1:] == "\"" {
result[kv[0]] = result[kv[0]][1 : len(result[kv[0]])-1]
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for writing tests!

Suggest that you simplify this code by taking the form:

c := strings.Index(input, ":")
if c < 0 {
  return nil, fmt.Error("need k:v pair where v may be quoted, but got '%s'", input)
}
k := input[:c]
v := trimQuotes(input[c+1:])
...

where trimQuotes is as done at https://stackoverflow.com/a/44222891

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplified the logic & moved the trim quotes logic to separate function.

}
return result, nil
}
Expand Down
63 changes: 61 additions & 2 deletions pkg/commands/edit/add/addmetadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package add

import (
"reflect"
"testing"

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

func TestAddAnnotationValueQuoted(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteTestKustomization()
v := validators.MakeHappyMapValidator(t)
cmd := newCmdAddAnnotation(fakeFS, v.Validator)
args := []string{"k1:\"v1\""}
err := cmd.RunE(cmd, args)
v.VerifyCall()
if err != nil {
t.Errorf("unexpected error: %v", err.Error())
}
}

func TestAddAnnotationValueWithColon(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteTestKustomization()
v := validators.MakeHappyMapValidator(t)
cmd := newCmdAddAnnotation(fakeFS, v.Validator)
args := []string{"k1:\"v1:v2\""}
err := cmd.RunE(cmd, args)
v.VerifyCall()
if err != nil {
t.Errorf("unexpected error: %v", err.Error())
}
}

func TestAddAnnotationNoKey(t *testing.T) {
fakeFS := fs.MakeFakeFS()
v := validators.MakeHappyMapValidator(t)
Expand All @@ -128,7 +155,7 @@ func TestAddAnnotationTooManyColons(t *testing.T) {
if err == nil {
t.Errorf("expected an error")
}
if err.Error() != "invalid annotation: key:v1:v2 (too many colons)" {
if err.Error() != "invalid annotation: key:v1:v2 (too many colons, quote the values)" {
t.Errorf("incorrect error: %v", err.Error())
}
}
Expand Down Expand Up @@ -238,7 +265,7 @@ func TestAddLabelTooManyColons(t *testing.T) {
if err == nil {
t.Errorf("expected an error")
}
if err.Error() != "invalid label: key:v1:v2 (too many colons)" {
if err.Error() != "invalid label: key:v1:v2 (too many colons, quote the values)" {
t.Errorf("incorrect error: %v", err.Error())
}
}
Expand Down Expand Up @@ -271,3 +298,35 @@ func TestAddLabelMultipleArgs(t *testing.T) {
t.Errorf("incorrect error: %v", err.Error())
}
}

func TestConvertToMap(t *testing.T) {
var o addMetadataOptions
args := "a:b,c:\"d\",e:\"f:g\""
expected := make(map[string]string)
expected["a"] = "b"
expected["c"] = "d"
expected["e"] = "f:g"

result, err := o.convertToMap(args)
if err != nil {
t.Errorf("unexpected error: %v", err.Error())
}

eq := reflect.DeepEqual(expected, result)
if !eq {
t.Errorf("Converted map does not match expected, expected: %v, result: %v\n", expected, result)
}
}

func TestConvertToMapError(t *testing.T) {
var o addMetadataOptions
args := "a:b,c:\"d\",e:f:g"

_, err := o.convertToMap(args)
if err == nil {
t.Errorf("expected an error")
}
if err.Error() != "invalid annotation: e:f:g (too many colons, quote the values)" {
t.Errorf("incorrect error: %v", err.Error())
}
}