Skip to content

Commit 524d593

Browse files
Separate functions for RunnAddLabel and RunAddAnnotation
1 parent 3b64447 commit 524d593

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

pkg/commands/addmetadata.go

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func newCmdAddAnnotation(fsys fs.FileSystem) *cobra.Command {
6363
if err != nil {
6464
return err
6565
}
66-
return o.RunAddMetadata(fsys, annotation)
66+
return o.RunAddAnnotation(fsys, annotation)
6767
},
6868
}
6969
return cmd
@@ -83,7 +83,7 @@ func newCmdAddLabel(fsys fs.FileSystem) *cobra.Command {
8383
if err != nil {
8484
return err
8585
}
86-
return o.RunAddMetadata(fsys, label)
86+
return o.RunAddLabel(fsys, label)
8787
},
8888
}
8989
return cmd
@@ -118,40 +118,52 @@ func (o *addMetadataOptions) Validate(args []string, k KindOfAdd) error {
118118
return nil
119119
}
120120

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 {
123123
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
124124
if err != nil {
125125
return err
126126
}
127-
128127
m, err := mf.read()
129128
if err != nil {
130129
return err
131130
}
132131

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 {
138133
m.CommonAnnotations = make(map[string]string)
139134
}
140135

141136
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-
}
148137
if k == annotation {
149138
if _, ok := m.CommonAnnotations[key]; ok {
150139
return fmt.Errorf("%s %s already in kustomization file", k, key)
151140
}
152141
m.CommonAnnotations[key] = value
153142
}
154143
}
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+
}
155157

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+
}
156168
return mf.write(m)
157169
}

0 commit comments

Comments
 (0)