@@ -22,6 +22,7 @@ import (
22
22
23
23
"github.com/kubernetes-sigs/kustomize/pkg/constants"
24
24
"github.com/kubernetes-sigs/kustomize/pkg/fs"
25
+ "github.com/kubernetes-sigs/kustomize/pkg/types"
25
26
"github.com/kubernetes-sigs/kustomize/pkg/validators"
26
27
"github.com/spf13/cobra"
27
28
)
@@ -56,18 +57,13 @@ func newCmdAddAnnotation(fSys fs.FileSystem, v validators.MapValidatorFunc) *cob
56
57
var o addMetadataOptions
57
58
o .kind = annotation
58
59
o .mapValidator = v
59
-
60
60
cmd := & cobra.Command {
61
61
Use : "annotation" ,
62
62
Short : "Adds one or more commonAnnotations to " + constants .KustomizationFileName ,
63
63
Example : `
64
64
add annotation {annotationKey1:annotationValue1},{annotationKey2:annotationValue2}` ,
65
65
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 )
71
67
},
72
68
}
73
69
return cmd
@@ -78,25 +74,41 @@ func newCmdAddLabel(fSys fs.FileSystem, v validators.MapValidatorFunc) *cobra.Co
78
74
var o addMetadataOptions
79
75
o .kind = label
80
76
o .mapValidator = v
81
-
82
77
cmd := & cobra.Command {
83
78
Use : "label" ,
84
79
Short : "Adds one or more commonLabels to " + constants .KustomizationFileName ,
85
80
Example : `
86
81
add label {labelKey1:labelValue1},{labelKey2:labelValue2}` ,
87
82
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 )
93
84
},
94
85
}
95
86
return cmd
96
87
}
97
88
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 {
100
112
if len (args ) < 1 {
101
113
return fmt .Errorf ("must specify %s" , o .kind )
102
114
}
@@ -120,10 +132,10 @@ func (o *addMetadataOptions) convertToMap(arg string) (map[string]string, error)
120
132
for _ , input := range inputs {
121
133
kv := strings .Split (input , ":" )
122
134
if len (kv [0 ]) < 1 {
123
- return nil , makeError ( o . kind , input , "empty key" )
135
+ return nil , o . makeError ( input , "empty key" )
124
136
}
125
137
if len (kv ) > 2 {
126
- return nil , makeError ( o . kind , input , "too many colons" )
138
+ return nil , o . makeError ( input , "too many colons" )
127
139
}
128
140
if len (kv ) > 1 {
129
141
result [kv [0 ]] = kv [1 ]
@@ -134,54 +146,30 @@ func (o *addMetadataOptions) convertToMap(arg string) (map[string]string, error)
134
146
return result , nil
135
147
}
136
148
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 {
148
150
if m .CommonAnnotations == nil {
149
151
m .CommonAnnotations = make (map [string ]string )
150
152
}
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 )
159
154
}
160
155
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 {
172
157
if m .CommonLabels == nil {
173
158
m .CommonLabels = make (map [string ]string )
174
159
}
160
+ return o .writeToMap (m .CommonLabels , label )
161
+ }
175
162
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 )
179
167
}
180
- m . CommonLabels [ key ] = value
168
+ m [ k ] = v
181
169
}
182
- return mf . write ( m )
170
+ return nil
183
171
}
184
172
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 )
187
175
}
0 commit comments