mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Move commands/edit utils package up to commands
This commit is contained in:
@@ -18,10 +18,10 @@ package add
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/commands/kustfile"
|
"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/fs"
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/pgmconfig"
|
"sigs.k8s.io/kustomize/v3/pkg/pgmconfig"
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/types"
|
"sigs.k8s.io/kustomize/v3/pkg/types"
|
||||||
@@ -122,7 +122,7 @@ func (o *addMetadataOptions) validateAndParse(args []string) error {
|
|||||||
if len(args) > 1 {
|
if len(args) > 1 {
|
||||||
return fmt.Errorf("%ss must be comma-separated, with no spaces", o.kind)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -133,27 +133,6 @@ func (o *addMetadataOptions) validateAndParse(args []string) error {
|
|||||||
return nil
|
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 {
|
func (o *addMetadataOptions) addAnnotations(m *types.Kustomization) error {
|
||||||
if m.CommonAnnotations == nil {
|
if m.CommonAnnotations == nil {
|
||||||
m.CommonAnnotations = make(map[string]string)
|
m.CommonAnnotations = make(map[string]string)
|
||||||
@@ -177,16 +156,3 @@ func (o *addMetadataOptions) writeToMap(m map[string]string, kind kindOfAdd) err
|
|||||||
}
|
}
|
||||||
return nil
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ limitations under the License.
|
|||||||
package add
|
package add
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/commands/kustfile"
|
"sigs.k8s.io/kustomize/v3/pkg/commands/kustfile"
|
||||||
@@ -362,36 +361,3 @@ func TestAddLabelForce(t *testing.T) {
|
|||||||
t.Errorf("unexpected error: %v", err.Error())
|
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())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -21,8 +21,8 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"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/kustfile"
|
||||||
|
"sigs.k8s.io/kustomize/v3/pkg/commands/util"
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/fs"
|
"sigs.k8s.io/kustomize/v3/pkg/fs"
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/patch"
|
"sigs.k8s.io/kustomize/v3/pkg/patch"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -21,8 +21,8 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"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/kustfile"
|
||||||
|
"sigs.k8s.io/kustomize/v3/pkg/commands/util"
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/fs"
|
"sigs.k8s.io/kustomize/v3/pkg/fs"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"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"
|
"sigs.k8s.io/kustomize/v3/pkg/fs"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ import (
|
|||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/spf13/cobra"
|
"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/kustfile"
|
||||||
|
"sigs.k8s.io/kustomize/v3/pkg/commands/util"
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/fs"
|
"sigs.k8s.io/kustomize/v3/pkg/fs"
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/patch"
|
"sigs.k8s.io/kustomize/v3/pkg/patch"
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/pgmconfig"
|
"sigs.k8s.io/kustomize/v3/pkg/pgmconfig"
|
||||||
|
|||||||
@@ -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
|
|
||||||
}
|
|
||||||
62
pkg/commands/util/functions.go
Normal file
62
pkg/commands/util/functions.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
37
pkg/commands/util/functions_test.go
Normal file
37
pkg/commands/util/functions_test.go
Normal file
@@ -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())
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user