Skip to content

Commit cda909a

Browse files
committed
Remove duplicate code.
1 parent 6f61cad commit cda909a

File tree

2 files changed

+63
-62
lines changed

2 files changed

+63
-62
lines changed

pkg/commands/addmetadata.go

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/kubernetes-sigs/kustomize/pkg/constants"
2424
"github.com/kubernetes-sigs/kustomize/pkg/fs"
25+
"github.com/kubernetes-sigs/kustomize/pkg/types"
2526
"github.com/kubernetes-sigs/kustomize/pkg/validators"
2627
"github.com/spf13/cobra"
2728
)
@@ -56,18 +57,13 @@ func newCmdAddAnnotation(fSys fs.FileSystem, v validators.MapValidatorFunc) *cob
5657
var o addMetadataOptions
5758
o.kind = annotation
5859
o.mapValidator = v
59-
6060
cmd := &cobra.Command{
6161
Use: "annotation",
6262
Short: "Adds one or more commonAnnotations to " + constants.KustomizationFileName,
6363
Example: `
6464
add annotation {annotationKey1:annotationValue1},{annotationKey2:annotationValue2}`,
6565
RunE: func(cmd *cobra.Command, args []string) error {
66-
err := o.ValidateAndParse(args)
67-
if err != nil {
68-
return err
69-
}
70-
return o.RunAddAnnotation(fSys)
66+
return o.runE(args, fSys, o.addAnnotations)
7167
},
7268
}
7369
return cmd
@@ -78,25 +74,41 @@ func newCmdAddLabel(fSys fs.FileSystem, v validators.MapValidatorFunc) *cobra.Co
7874
var o addMetadataOptions
7975
o.kind = label
8076
o.mapValidator = v
81-
8277
cmd := &cobra.Command{
8378
Use: "label",
8479
Short: "Adds one or more commonLabels to " + constants.KustomizationFileName,
8580
Example: `
8681
add label {labelKey1:labelValue1},{labelKey2:labelValue2}`,
8782
RunE: func(cmd *cobra.Command, args []string) error {
88-
err := o.ValidateAndParse(args)
89-
if err != nil {
90-
return err
91-
}
92-
return o.RunAddLabel(fSys)
83+
return o.runE(args, fSys, o.addLabels)
9384
},
9485
}
9586
return cmd
9687
}
9788

98-
// ValidateAndParse validates addLabel and addAnnotation commands and parses them into o.metadata
99-
func (o *addMetadataOptions) ValidateAndParse(args []string) error {
89+
func (o *addMetadataOptions) runE(
90+
args []string, fSys fs.FileSystem, adder func(*types.Kustomization) error) error {
91+
err := o.validateAndParse(args)
92+
if err != nil {
93+
return err
94+
}
95+
kf, err := newKustomizationFile(constants.KustomizationFileName, fSys)
96+
if err != nil {
97+
return err
98+
}
99+
m, err := kf.read()
100+
if err != nil {
101+
return err
102+
}
103+
err = adder(m)
104+
if err != nil {
105+
return err
106+
}
107+
return kf.write(m)
108+
}
109+
110+
// validateAndParse validates `add` commands and parses them into o.metadata
111+
func (o *addMetadataOptions) validateAndParse(args []string) error {
100112
if len(args) < 1 {
101113
return fmt.Errorf("must specify %s", o.kind)
102114
}
@@ -120,10 +132,10 @@ func (o *addMetadataOptions) convertToMap(arg string) (map[string]string, error)
120132
for _, input := range inputs {
121133
kv := strings.Split(input, ":")
122134
if len(kv[0]) < 1 {
123-
return nil, makeError(o.kind, input, "empty key")
135+
return nil, o.makeError(input, "empty key")
124136
}
125137
if len(kv) > 2 {
126-
return nil, makeError(o.kind, input, "too many colons")
138+
return nil, o.makeError(input, "too many colons")
127139
}
128140
if len(kv) > 1 {
129141
result[kv[0]] = kv[1]
@@ -134,54 +146,30 @@ func (o *addMetadataOptions) convertToMap(arg string) (map[string]string, error)
134146
return result, nil
135147
}
136148

137-
// RunAddAnnotation runs addAnnotation command, doing the real work.
138-
func (o *addMetadataOptions) RunAddAnnotation(fsys fs.FileSystem) error {
139-
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
140-
if err != nil {
141-
return err
142-
}
143-
m, err := mf.read()
144-
if err != nil {
145-
return err
146-
}
147-
149+
func (o *addMetadataOptions) addAnnotations(m *types.Kustomization) error {
148150
if m.CommonAnnotations == nil {
149151
m.CommonAnnotations = make(map[string]string)
150152
}
151-
152-
for key, value := range o.metadata {
153-
if _, ok := m.CommonAnnotations[key]; ok {
154-
return fmt.Errorf("%s %s already in kustomization file", o.kind, key)
155-
}
156-
m.CommonAnnotations[key] = value
157-
}
158-
return mf.write(m)
153+
return o.writeToMap(m.CommonAnnotations, annotation)
159154
}
160155

161-
// RunAddLabel runs addLabel command, doing the real work.
162-
func (o *addMetadataOptions) RunAddLabel(fsys fs.FileSystem) error {
163-
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
164-
if err != nil {
165-
return err
166-
}
167-
m, err := mf.read()
168-
if err != nil {
169-
return err
170-
}
171-
156+
func (o *addMetadataOptions) addLabels(m *types.Kustomization) error {
172157
if m.CommonLabels == nil {
173158
m.CommonLabels = make(map[string]string)
174159
}
160+
return o.writeToMap(m.CommonLabels, label)
161+
}
175162

176-
for key, value := range o.metadata {
177-
if _, ok := m.CommonLabels[key]; ok {
178-
return fmt.Errorf("%s %s already in kustomization file", o.kind, key)
163+
func (o *addMetadataOptions) writeToMap(m map[string]string, kind kindOfAdd) error {
164+
for k, v := range o.metadata {
165+
if _, ok := m[k]; ok {
166+
return fmt.Errorf("%s %s already in kustomization file", kind, k)
179167
}
180-
m.CommonLabels[key] = value
168+
m[k] = v
181169
}
182-
return mf.write(m)
170+
return nil
183171
}
184172

185-
func makeError(k kindOfAdd, input string, message string) error {
186-
return fmt.Errorf("invalid %s: %s (%s)", k, input, message)
173+
func (o *addMetadataOptions) makeError(input string, message string) error {
174+
return fmt.Errorf("invalid %s: %s (%s)", o.kind, input, message)
187175
}

pkg/commands/addmetadata_test.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,41 @@ import (
2121

2222
"github.com/kubernetes-sigs/kustomize/pkg/constants"
2323
"github.com/kubernetes-sigs/kustomize/pkg/fs"
24+
"github.com/kubernetes-sigs/kustomize/pkg/types"
2425
"github.com/kubernetes-sigs/kustomize/pkg/validators"
2526
)
2627

27-
func TestRunAddAnnotation(t *testing.T) {
28+
func makeKustomization(t *testing.T) *types.Kustomization {
2829
fakeFS := fs.MakeFakeFS()
2930
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
31+
kf, err := newKustomizationFile(constants.KustomizationFileName, fakeFS)
32+
if err != nil {
33+
t.Errorf("unexpected new error %v", err)
34+
}
35+
m, err := kf.read()
36+
if err != nil {
37+
t.Errorf("unexpected read error %v", err)
38+
}
39+
return m
40+
}
41+
42+
func TestRunAddAnnotation(t *testing.T) {
3043
var o addMetadataOptions
3144
o.metadata = map[string]string{"owls": "cute", "otters": "adorable"}
3245

33-
err := o.RunAddAnnotation(fakeFS)
46+
m := makeKustomization(t)
47+
err := o.addAnnotations(m)
3448
if err != nil {
3549
t.Errorf("unexpected error: could not write to kustomization file")
3650
}
3751
// adding the same test input should not work
38-
err = o.RunAddAnnotation(fakeFS)
52+
err = o.addAnnotations(m)
3953
if err == nil {
4054
t.Errorf("expected already in kustomization file error")
4155
}
4256
// adding new annotations should work
4357
o.metadata = map[string]string{"new": "annotation"}
44-
err = o.RunAddAnnotation(fakeFS)
58+
err = o.addAnnotations(m)
4559
if err != nil {
4660
t.Errorf("unexpected error: could not write to kustomization file")
4761
}
@@ -149,23 +163,22 @@ func TestAddAnnotationMultipleArgs(t *testing.T) {
149163
}
150164

151165
func TestRunAddLabel(t *testing.T) {
152-
fakeFS := fs.MakeFakeFS()
153-
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
154166
var o addMetadataOptions
155167
o.metadata = map[string]string{"owls": "cute", "otters": "adorable"}
156168

157-
err := o.RunAddLabel(fakeFS)
169+
m := makeKustomization(t)
170+
err := o.addLabels(m)
158171
if err != nil {
159172
t.Errorf("unexpected error: could not write to kustomization file")
160173
}
161174
// adding the same test input should not work
162-
err = o.RunAddLabel(fakeFS)
175+
err = o.addLabels(m)
163176
if err == nil {
164177
t.Errorf("expected already in kustomization file error")
165178
}
166179
// adding new labels should work
167180
o.metadata = map[string]string{"new": "label"}
168-
err = o.RunAddLabel(fakeFS)
181+
err = o.addLabels(m)
169182
if err != nil {
170183
t.Errorf("unexpected error: could not write to kustomization file")
171184
}

0 commit comments

Comments
 (0)