diff --git a/pkg/commands/addmetadata.go b/pkg/commands/addmetadata.go index d28c689ea..56e57ba32 100644 --- a/pkg/commands/addmetadata.go +++ b/pkg/commands/addmetadata.go @@ -18,11 +18,11 @@ package commands import ( "fmt" - "regexp" "strings" "github.com/kubernetes-sigs/kustomize/pkg/constants" "github.com/kubernetes-sigs/kustomize/pkg/fs" + "github.com/kubernetes-sigs/kustomize/pkg/validate" "github.com/spf13/cobra" ) @@ -59,7 +59,7 @@ func newCmdAddAnnotation(fsys fs.FileSystem) *cobra.Command { Example: ` add annotation {annotationKey1:annotationValue1},{annotationKey2:annotationValue2}`, RunE: func(cmd *cobra.Command, args []string) error { - err := o.Validate(args, annotation) + err := o.ValidateAndParse(args, annotation) if err != nil { return err } @@ -79,7 +79,7 @@ func newCmdAddLabel(fsys fs.FileSystem) *cobra.Command { Example: ` add label {labelKey1:labelValue1},{labelKey2:labelValue2}`, RunE: func(cmd *cobra.Command, args []string) error { - err := o.Validate(args, label) + err := o.ValidateAndParse(args, label) if err != nil { return err } @@ -89,8 +89,8 @@ func newCmdAddLabel(fsys fs.FileSystem) *cobra.Command { return cmd } -// Validate validates addLabel and addAnnotation commands. -func (o *addMetadataOptions) Validate(args []string, k KindOfAdd) error { +// ValidateAndParse validates addLabel and addAnnotation commands and parses them into o.metadata +func (o *addMetadataOptions) ValidateAndParse(args []string, k KindOfAdd) error { o.metadata = make(map[string]string) if len(args) < 1 { return fmt.Errorf("must specify %s", k) @@ -100,18 +100,22 @@ func (o *addMetadataOptions) Validate(args []string, k KindOfAdd) error { } inputs := strings.Split(args[0], ",") for _, input := range inputs { - ok, err := regexp.MatchString(`\A([a-zA-Z0-9_.-]+):([a-zA-Z0-9_.-]+)\z`, input) - if err != nil { - return err + switch k { + case label: + valid, err := validate.IsValidLabel(input) + if !valid { + return err + } + case annotation: + valid, err := validate.IsValidAnnotation(input) + if !valid { + return err + } + default: + return fmt.Errorf("unknown metadata kind %s", k) } - if !ok { - return fmt.Errorf("invalid %s format: %s", k, input) - } - } - //parse annotation keys and values into metadata - entries := strings.Split(args[0], ",") - for _, entry := range entries { - kv := strings.Split(entry, ":") + //parse annotation keys and values into metadata + kv := strings.Split(input, ":") o.metadata[kv[0]] = kv[1] } return nil diff --git a/pkg/commands/addmetadata_test.go b/pkg/commands/addmetadata_test.go index 2f195a4a9..e796ff04b 100644 --- a/pkg/commands/addmetadata_test.go +++ b/pkg/commands/addmetadata_test.go @@ -17,7 +17,6 @@ limitations under the License. package commands import ( - "fmt" "reflect" "testing" @@ -25,12 +24,13 @@ import ( "github.com/kubernetes-sigs/kustomize/pkg/fs" ) -func TestInputValid(t *testing.T) { +func TestParseValidateInput(t *testing.T) { var testcases = []struct { input string valid bool name string expectedData map[string]string + kind KindOfAdd }{ { input: "otters:cute", @@ -39,6 +39,7 @@ func TestInputValid(t *testing.T) { expectedData: map[string]string{ "otters": "cute", }, + kind: label, }, { input: "owls:great,unicorns:magical", @@ -48,26 +49,7 @@ func TestInputValid(t *testing.T) { "owls": "great", "unicorns": "magical", }, - }, - { - input: "dogs,cats", - valid: false, - name: "Does not contain colon", - }, - { - input: ":noKey", - valid: false, - name: "Missing key", - }, - { - input: "noValue:", - valid: false, - name: "Missing value", - }, - { - input: "exclamation!:point", - valid: false, - name: "Non-alphanumeric input", + kind: label, }, { input: "123:45", @@ -76,30 +58,26 @@ func TestInputValid(t *testing.T) { expectedData: map[string]string{ "123": "45", }, + kind: annotation, + }, + { + input: " ", + valid: false, + name: "Empty space input", + expectedData: nil, + kind: annotation, }, } var o addMetadataOptions for _, tc := range testcases { - args := []string{tc.input} - err := o.Validate(args, label) //use label since in Validate kindofAdd is only used for error messages - + err := o.ValidateAndParse(args, tc.kind) if err != nil && tc.valid { t.Errorf("for test case %s, unexpected cmd error: %v", tc.name, err) } if err == nil && !tc.valid { t.Errorf("unexpected error: expected invalid format error for test case %v", tc.name) } - if err == nil && (tc.name == "Metadata keys must be unique") { - t.Errorf("unexpected error: for test case %s, expected already there problem", tc.name) - } - if err != nil && (tc.name == "Multiple args") { - fmt.Println(err.Error()) - if err.Error() != "must specify label" { - t.Errorf("unexpected error: for test case %s, expected must specify label error", tc.name) - } - } - //o.metadata should be the same as expectedData if tc.valid { if !reflect.DeepEqual(o.metadata, tc.expectedData) {