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 (
|
||||
"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
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user