@@ -63,7 +63,7 @@ func newCmdAddAnnotation(fsys fs.FileSystem) *cobra.Command {
63
63
if err != nil {
64
64
return err
65
65
}
66
- return o .RunAddMetadata (fsys , annotation )
66
+ return o .RunAddAnnotation (fsys , annotation )
67
67
},
68
68
}
69
69
return cmd
@@ -83,7 +83,7 @@ func newCmdAddLabel(fsys fs.FileSystem) *cobra.Command {
83
83
if err != nil {
84
84
return err
85
85
}
86
- return o .RunAddMetadata (fsys , label )
86
+ return o .RunAddLabel (fsys , label )
87
87
},
88
88
}
89
89
return cmd
@@ -118,40 +118,52 @@ func (o *addMetadataOptions) Validate(args []string, k KindOfAdd) error {
118
118
return nil
119
119
}
120
120
121
- // RunAddMetadata runs addLabel and addAnnotation commands (do real work) .
122
- func (o * addMetadataOptions ) RunAddMetadata (fsys fs.FileSystem , k KindOfAdd ) error {
121
+ // RunAddAnnotation runs addAnnotation command, doing the real work.
122
+ func (o * addMetadataOptions ) RunAddAnnotation (fsys fs.FileSystem , k KindOfAdd ) error {
123
123
mf , err := newKustomizationFile (constants .KustomizationFileName , fsys )
124
124
if err != nil {
125
125
return err
126
126
}
127
-
128
127
m , err := mf .read ()
129
128
if err != nil {
130
129
return err
131
130
}
132
131
133
- if k == label && m .CommonLabels == nil {
134
- m .CommonLabels = make (map [string ]string )
135
- }
136
-
137
- if k == annotation && m .CommonAnnotations == nil {
132
+ if m .CommonAnnotations == nil {
138
133
m .CommonAnnotations = make (map [string ]string )
139
134
}
140
135
141
136
for key , value := range o .metadata {
142
- if k == label {
143
- if _ , ok := m .CommonLabels [key ]; ok {
144
- return fmt .Errorf ("%s %s already in kustomization file" , k , key )
145
- }
146
- m .CommonLabels [key ] = value
147
- }
148
137
if k == annotation {
149
138
if _ , ok := m .CommonAnnotations [key ]; ok {
150
139
return fmt .Errorf ("%s %s already in kustomization file" , k , key )
151
140
}
152
141
m .CommonAnnotations [key ] = value
153
142
}
154
143
}
144
+ return mf .write (m )
145
+ }
146
+
147
+ // RunAddLabel runs addLabel command, doing the real work.
148
+ func (o * addMetadataOptions ) RunAddLabel (fsys fs.FileSystem , k KindOfAdd ) error {
149
+ mf , err := newKustomizationFile (constants .KustomizationFileName , fsys )
150
+ if err != nil {
151
+ return err
152
+ }
153
+ m , err := mf .read ()
154
+ if err != nil {
155
+ return err
156
+ }
155
157
158
+ if m .CommonLabels == nil {
159
+ m .CommonLabels = make (map [string ]string )
160
+ }
161
+
162
+ for key , value := range o .metadata {
163
+ if _ , ok := m .CommonLabels [key ]; ok {
164
+ return fmt .Errorf ("%s %s already in kustomization file" , k , key )
165
+ }
166
+ m .CommonLabels [key ] = value
167
+ }
156
168
return mf .write (m )
157
169
}
0 commit comments