From e904f612f30f31101d9f7e4c8c473857fbede3fb Mon Sep 17 00:00:00 2001 From: Richard Marshall Date: Mon, 29 Jul 2019 21:48:54 -0700 Subject: [PATCH] Move commands/edit utils package up to commands --- pkg/commands/edit/add/addmetadata.go | 38 +------------- pkg/commands/edit/add/addmetadata_test.go | 34 ------------- pkg/commands/edit/add/addpatch.go | 2 +- pkg/commands/edit/add/addresource.go | 2 +- pkg/commands/edit/add/flagsandargs.go | 2 +- pkg/commands/edit/remove/removepatch.go | 2 +- pkg/commands/edit/util/functions.go | 26 ---------- pkg/commands/util/functions.go | 62 +++++++++++++++++++++++ pkg/commands/util/functions_test.go | 37 ++++++++++++++ 9 files changed, 105 insertions(+), 100 deletions(-) delete mode 100644 pkg/commands/edit/util/functions.go create mode 100644 pkg/commands/util/functions.go create mode 100644 pkg/commands/util/functions_test.go diff --git a/pkg/commands/edit/add/addmetadata.go b/pkg/commands/edit/add/addmetadata.go index 87099c16f..85e7e600f 100644 --- a/pkg/commands/edit/add/addmetadata.go +++ b/pkg/commands/edit/add/addmetadata.go @@ -18,10 +18,10 @@ package add import ( "fmt" - "strings" "github.com/spf13/cobra" "sigs.k8s.io/kustomize/v3/pkg/commands/kustfile" + "sigs.k8s.io/kustomize/v3/pkg/commands/util" "sigs.k8s.io/kustomize/v3/pkg/fs" "sigs.k8s.io/kustomize/v3/pkg/pgmconfig" "sigs.k8s.io/kustomize/v3/pkg/types" @@ -122,7 +122,7 @@ func (o *addMetadataOptions) validateAndParse(args []string) error { if len(args) > 1 { return fmt.Errorf("%ss must be comma-separated, with no spaces", o.kind) } - m, err := o.convertToMap(args[0]) + m, err := util.ConvertToMap(args[0], o.kind.String()) if err != nil { return err } @@ -133,27 +133,6 @@ func (o *addMetadataOptions) validateAndParse(args []string) error { return nil } -func (o *addMetadataOptions) convertToMap(arg string) (map[string]string, error) { - result := make(map[string]string) - inputs := strings.Split(arg, ",") - for _, input := range inputs { - 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 { - // both key and value passed - key := input[:c] - value := trimQuotes(input[c+1:]) - result[key] = value - } - } - return result, nil -} - func (o *addMetadataOptions) addAnnotations(m *types.Kustomization) error { if m.CommonAnnotations == nil { m.CommonAnnotations = make(map[string]string) @@ -177,16 +156,3 @@ func (o *addMetadataOptions) writeToMap(m map[string]string, kind kindOfAdd) err } return nil } - -func (o *addMetadataOptions) makeError(input string, message string) error { - return fmt.Errorf("invalid %s: '%s' (%s)", o.kind, input, message) -} - -func trimQuotes(s string) string { - if len(s) >= 2 { - if s[0] == '"' && s[len(s)-1] == '"' { - return s[1 : len(s)-1] - } - } - return s -} diff --git a/pkg/commands/edit/add/addmetadata_test.go b/pkg/commands/edit/add/addmetadata_test.go index cee138687..de26f8c18 100644 --- a/pkg/commands/edit/add/addmetadata_test.go +++ b/pkg/commands/edit/add/addmetadata_test.go @@ -17,7 +17,6 @@ limitations under the License. package add import ( - "reflect" "testing" "sigs.k8s.io/kustomize/v3/pkg/commands/kustfile" @@ -362,36 +361,3 @@ func TestAddLabelForce(t *testing.T) { t.Errorf("unexpected error: %v", err.Error()) } } - -func TestConvertToMap(t *testing.T) { - var o addMetadataOptions - args := "a:b,c:\"d\",e:\"f:g\",g:h:k" - expected := make(map[string]string) - expected["a"] = "b" - expected["c"] = "d" - expected["e"] = "f:g" - expected["g"] = "h:k" - - 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\",:f:g" - - _, err := o.convertToMap(args) - if err == nil { - t.Errorf("expected an error") - } - if err.Error() != "invalid annotation: ':f:g' (need k:v pair where v may be quoted)" { - t.Errorf("incorrect error: %v", err.Error()) - } -} diff --git a/pkg/commands/edit/add/addpatch.go b/pkg/commands/edit/add/addpatch.go index 7ab1e871b..4eb1ae64a 100644 --- a/pkg/commands/edit/add/addpatch.go +++ b/pkg/commands/edit/add/addpatch.go @@ -21,8 +21,8 @@ import ( "log" "github.com/spf13/cobra" - "sigs.k8s.io/kustomize/v3/pkg/commands/edit/util" "sigs.k8s.io/kustomize/v3/pkg/commands/kustfile" + "sigs.k8s.io/kustomize/v3/pkg/commands/util" "sigs.k8s.io/kustomize/v3/pkg/fs" "sigs.k8s.io/kustomize/v3/pkg/patch" ) diff --git a/pkg/commands/edit/add/addresource.go b/pkg/commands/edit/add/addresource.go index 37c63a5ea..28b950eeb 100644 --- a/pkg/commands/edit/add/addresource.go +++ b/pkg/commands/edit/add/addresource.go @@ -21,8 +21,8 @@ import ( "log" "github.com/spf13/cobra" - "sigs.k8s.io/kustomize/v3/pkg/commands/edit/util" "sigs.k8s.io/kustomize/v3/pkg/commands/kustfile" + "sigs.k8s.io/kustomize/v3/pkg/commands/util" "sigs.k8s.io/kustomize/v3/pkg/fs" ) diff --git a/pkg/commands/edit/add/flagsandargs.go b/pkg/commands/edit/add/flagsandargs.go index 85807c2ac..60aab3ae1 100644 --- a/pkg/commands/edit/add/flagsandargs.go +++ b/pkg/commands/edit/add/flagsandargs.go @@ -20,7 +20,7 @@ import ( "fmt" "strings" - "sigs.k8s.io/kustomize/v3/pkg/commands/edit/util" + "sigs.k8s.io/kustomize/v3/pkg/commands/util" "sigs.k8s.io/kustomize/v3/pkg/fs" ) diff --git a/pkg/commands/edit/remove/removepatch.go b/pkg/commands/edit/remove/removepatch.go index f40995332..cdc353e6e 100644 --- a/pkg/commands/edit/remove/removepatch.go +++ b/pkg/commands/edit/remove/removepatch.go @@ -8,8 +8,8 @@ import ( "github.com/pkg/errors" "github.com/spf13/cobra" - "sigs.k8s.io/kustomize/v3/pkg/commands/edit/util" "sigs.k8s.io/kustomize/v3/pkg/commands/kustfile" + "sigs.k8s.io/kustomize/v3/pkg/commands/util" "sigs.k8s.io/kustomize/v3/pkg/fs" "sigs.k8s.io/kustomize/v3/pkg/patch" "sigs.k8s.io/kustomize/v3/pkg/pgmconfig" diff --git a/pkg/commands/edit/util/functions.go b/pkg/commands/edit/util/functions.go deleted file mode 100644 index 407e13027..000000000 --- a/pkg/commands/edit/util/functions.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2019 The Kubernetes Authors. -// SPDX-License-Identifier: Apache-2.0 - -package util - -import ( - "log" - - "sigs.k8s.io/kustomize/v3/pkg/fs" -) - -func GlobPatterns(fsys fs.FileSystem, patterns []string) ([]string, error) { - var result []string - for _, pattern := range patterns { - files, err := fsys.Glob(pattern) - if err != nil { - return nil, err - } - if len(files) == 0 { - log.Printf("%s has no match", pattern) - continue - } - result = append(result, files...) - } - return result, nil -} diff --git a/pkg/commands/util/functions.go b/pkg/commands/util/functions.go new file mode 100644 index 000000000..cc75353b0 --- /dev/null +++ b/pkg/commands/util/functions.go @@ -0,0 +1,62 @@ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + +package util + +import ( + "fmt" + "log" + "strings" + + "sigs.k8s.io/kustomize/v3/pkg/fs" +) + +// GlobPatterns accepts a slice of glob strings and returns the set of +// matching file paths. +func GlobPatterns(fsys fs.FileSystem, patterns []string) ([]string, error) { + var result []string + for _, pattern := range patterns { + files, err := fsys.Glob(pattern) + if err != nil { + return nil, err + } + if len(files) == 0 { + log.Printf("%s has no match", pattern) + continue + } + result = append(result, files...) + } + return result, nil +} + +// ConvertToMap converts a slice of strings in the form of +// `key:value` into a map. +func ConvertToMap(input string, kind string) (map[string]string, error) { + result := make(map[string]string) + inputs := strings.Split(input, ",") + for _, input := range inputs { + c := strings.Index(input, ":") + if c == 0 { + // key is not passed + return nil, fmt.Errorf("invalid %s: '%s' (%s)", kind, input, "need k:v pair where v may be quoted") + } else if c < 0 { + // only key passed + result[input] = "" + } else { + // both key and value passed + key := input[:c] + value := trimQuotes(input[c+1:]) + result[key] = value + } + } + return result, nil +} + +func trimQuotes(s string) string { + if len(s) >= 2 { + if s[0] == '"' && s[len(s)-1] == '"' { + return s[1 : len(s)-1] + } + } + return s +} diff --git a/pkg/commands/util/functions_test.go b/pkg/commands/util/functions_test.go new file mode 100644 index 000000000..5828de090 --- /dev/null +++ b/pkg/commands/util/functions_test.go @@ -0,0 +1,37 @@ +package util + +import ( + "reflect" + "testing" +) + +func TestConvertToMap(t *testing.T) { + args := "a:b,c:\"d\",e:\"f:g\",g:h:k" + expected := make(map[string]string) + expected["a"] = "b" + expected["c"] = "d" + expected["e"] = "f:g" + expected["g"] = "h:k" + + result, err := ConvertToMap(args, "annotation") + 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) { + args := "a:b,c:\"d\",:f:g" + + _, err := ConvertToMap(args, "annotation") + if err == nil { + t.Errorf("expected an error") + } + if err.Error() != "invalid annotation: ':f:g' (need k:v pair where v may be quoted)" { + t.Errorf("incorrect error: %v", err.Error()) + } +}