Skip to content

Commit afac2fb

Browse files
Uses single file for both addLabel and addAnnotation commands, as the code is nearly identical. Tests included.
1 parent 20fd433 commit afac2fb

File tree

4 files changed

+509
-131
lines changed

4 files changed

+509
-131
lines changed

pkg/commands/addannotation.go

Lines changed: 65 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -17,95 +17,96 @@ limitations under the License.
1717
package commands
1818

1919
import (
20-
"errors"
21-
"fmt"
22-
"regexp"
23-
"strings"
20+
// "errors"
21+
// "fmt"
22+
// "regexp"
23+
// "strings"
2424

2525
"github.com/spf13/cobra"
2626

27-
"github.com/kubernetes-sigs/kustomize/pkg/constants"
27+
// "github.com/kubernetes-sigs/kustomize/pkg/constants"
2828
"github.com/kubernetes-sigs/kustomize/pkg/fs"
2929
)
3030

31-
type addAnnotationOptions struct {
32-
annotations string
33-
}
31+
// type addAnnotationOptions struct {
32+
// annotations string
33+
// }
3434

3535
// newCmdAddAnnotation adds one or more commonAnnotations to the kustomization file.
3636
func newCmdAddAnnotation(fsys fs.FileSystem) *cobra.Command {
37-
var o addAnnotationOptions
37+
var o addMetadataOptions
3838

3939
cmd := &cobra.Command{
4040
Use: "annotation",
4141
Short: "Adds one or more commonAnnotations to the kustomization.yaml in current directory",
4242
Example: `
4343
add annotation {annotationKey1:annotationValue1},{annotationKey2:annotationValue2}`,
4444
RunE: func(cmd *cobra.Command, args []string) error {
45-
err := o.Validate(args)
45+
mdKind := "annotation"
46+
err := o.Validate(args, mdKind)
4647
if err != nil {
4748
return err
4849
}
4950
err = o.Complete(cmd, args)
5051
if err != nil {
5152
return err
5253
}
53-
return o.RunAddAnnotation(fsys)
54+
return o.RunAddMetadata(fsys, mdKind)
5455
},
5556
}
5657
return cmd
5758
}
5859

59-
// Validate validates addAnnotation command.
60-
func (o *addAnnotationOptions) Validate(args []string) error {
61-
if len(args) < 1 {
62-
return errors.New("must specify an annotation")
63-
}
64-
if len(args) > 1 {
65-
return errors.New("annotations must be comma-separated, with no spaces. See help text for example.")
66-
}
67-
inputs := strings.Split(args[0], ",")
68-
for _, input := range inputs {
69-
ok, err := regexp.MatchString(`\A([a-zA-Z0-9_.-]+):([a-zA-Z0-9_.-]+)\z`, input)
70-
if err != nil {
71-
return err
72-
}
73-
if !ok {
74-
return fmt.Errorf("invalid annotation format: %s", input)
75-
}
76-
}
77-
o.annotations = args[0]
78-
return nil
79-
}
80-
81-
// Complete completes addAnnotation command.
82-
func (o *addAnnotationOptions) Complete(cmd *cobra.Command, args []string) error {
83-
return nil
84-
}
85-
86-
// RunAddAnnotation runs addAnnotation command (do real work).
87-
func (o *addAnnotationOptions) RunAddAnnotation(fsys fs.FileSystem) error {
88-
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
89-
if err != nil {
90-
return err
91-
}
92-
93-
m, err := mf.read()
94-
if err != nil {
95-
return err
96-
}
97-
98-
if m.CommonAnnotations == nil {
99-
m.CommonAnnotations = make(map[string]string)
100-
}
101-
annotations := strings.Split(o.annotations, ",")
102-
for _, ann := range annotations {
103-
kv := strings.Split(ann, ":")
104-
if _, ok := m.CommonAnnotations[kv[0]]; ok {
105-
return fmt.Errorf("annotation %s already in kustomization file", kv[0])
106-
}
107-
m.CommonAnnotations[kv[0]] = kv[1]
108-
}
109-
110-
return mf.write(m)
111-
}
60+
// // Validate validates addAnnotation command.
61+
// func (o *addAnnotationOptions) Validate(args []string) error {
62+
// if len(args) < 1 {
63+
// return errors.New("must specify an annotation")
64+
// }
65+
// if len(args) > 1 {
66+
// return errors.New("annotations must be comma-separated, with no spaces. See help text for example.")
67+
// }
68+
// inputs := strings.Split(args[0], ",")
69+
// for _, input := range inputs {
70+
// ok, err := regexp.MatchString(`\A([a-zA-Z0-9_.-]+):([a-zA-Z0-9_.-]+)\z`, input)
71+
// if err != nil {
72+
// return err
73+
// }
74+
// if !ok {
75+
// return fmt.Errorf("invalid annotation format: %s", input)
76+
// }
77+
// }
78+
// o.annotations = args[0]
79+
// return nil
80+
// }
81+
82+
// // Complete completes addAnnotation command.
83+
// func (o *addAnnotationOptions) Complete(cmd *cobra.Command, args []string) error {
84+
// return nil
85+
// }
86+
87+
// // RunAddAnnotation runs addAnnotation command (do real work).
88+
// func (o *addAnnotationOptions) RunAddAnnotation(fsys fs.FileSystem) error {
89+
// mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
90+
// if err != nil {
91+
// return err
92+
// }
93+
94+
// m, err := mf.read()
95+
// if err != nil {
96+
// return err
97+
// }
98+
99+
// if m.CommonAnnotations == nil {
100+
// m.CommonAnnotations = make(map[string]string)
101+
// }
102+
// annotations := strings.Split(o.annotations, ",")
103+
// for _, ann := range annotations {
104+
// kv := strings.Split(ann, ":")
105+
// if _, ok := m.CommonAnnotations[kv[0]]; ok {
106+
// return fmt.Errorf("annotation %s already in kustomization file", kv[0])
107+
// }
108+
// m.CommonAnnotations[kv[0]] = kv[1]
109+
// }
110+
111+
// return mf.write(m)
112+
// }

pkg/commands/addlabel.go

Lines changed: 69 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -17,98 +17,100 @@ limitations under the License.
1717
package commands
1818

1919
import (
20-
"errors"
21-
"fmt"
22-
"regexp"
23-
"strings"
20+
// "errors"
21+
// "fmt"
22+
// "regexp"
23+
// "strings"
2424

2525
"github.com/spf13/cobra"
2626

27-
"github.com/kubernetes-sigs/kustomize/pkg/constants"
27+
// "github.com/kubernetes-sigs/kustomize/pkg/constants"
2828
"github.com/kubernetes-sigs/kustomize/pkg/fs"
2929
)
3030

31-
type addLabelOptions struct {
32-
labels string
33-
}
31+
32+
// type addLabelOptions struct {
33+
// labels string
34+
// }
3435

3536
// newCmdAddLabel adds one or more commonLabels to the kustomization file.
3637
func newCmdAddLabel(fsys fs.FileSystem) *cobra.Command {
37-
var o addLabelOptions
38+
var o addMetadataOptions
3839

3940
cmd := &cobra.Command{
4041
Use: "label",
4142
Short: "Adds one or more commonLabels to the kustomization.yaml in current directory",
4243
Example: `
4344
add label {labelKey1:labelValue1},{labelKey2:labelValue2}`,
4445
RunE: func(cmd *cobra.Command, args []string) error {
45-
err := o.Validate(args)
46+
mdKind := "label"
47+
err := o.Validate(args, mdKind)
4648
if err != nil {
4749
return err
4850
}
4951
err = o.Complete(cmd, args)
5052
if err != nil {
5153
return err
5254
}
53-
return o.RunAddLabel(fsys)
55+
return o.RunAddMetadata(fsys, mdKind)
5456
},
5557
}
5658
return cmd
5759
}
5860

59-
// Validate validates addLabel command.
60-
func (o *addLabelOptions) Validate(args []string) error {
61-
if len(args) < 1 {
62-
return errors.New("must specify a label")
63-
}
64-
if len(args) > 1 {
65-
return errors.New("labels must be comma-separated, with no spaces. See help text for example.")
66-
}
67-
inputs := strings.Split(args[0], ",")
68-
for _, input := range inputs {
69-
ok, err := regexp.MatchString(`\A([a-zA-Z0-9_.-]+):([a-zA-Z0-9_.-]+)\z`, input)
70-
if err != nil {
71-
return err
72-
}
73-
if !ok {
74-
return fmt.Errorf("invalid label format: %s", input)
75-
}
76-
77-
}
78-
79-
o.labels = args[0]
80-
return nil
81-
}
82-
83-
// Complete completes addLabel command.
84-
func (o *addLabelOptions) Complete(cmd *cobra.Command, args []string) error {
85-
return nil
86-
}
87-
88-
// RunAddLabel runs addLabel command (do real work).
89-
func (o *addLabelOptions) RunAddLabel(fsys fs.FileSystem) error {
90-
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
91-
if err != nil {
92-
return err
93-
}
94-
95-
m, err := mf.read()
96-
if err != nil {
97-
return err
98-
}
99-
100-
if m.CommonLabels == nil {
101-
m.CommonLabels = make(map[string]string)
102-
}
103-
104-
labels := strings.Split(o.labels, ",")
105-
for _, label := range labels {
106-
kv := strings.Split(label, ":")
107-
if _, ok := m.CommonLabels[kv[0]]; ok {
108-
return fmt.Errorf("label %s already in kustomization file", kv[0])
109-
}
110-
m.CommonLabels[kv[0]] = kv[1]
111-
}
112-
113-
return mf.write(m)
114-
}
61+
// // Validate validates addLabel command.
62+
// func (o *addLabelOptions) Validate(args []string) error {
63+
// if len(args) < 1 {
64+
// return errors.New("must specify a label")
65+
// }
66+
// if len(args) > 1 {
67+
// return errors.New("labels must be comma-separated, with no spaces. See help text for example.")
68+
// }
69+
// inputs := strings.Split(args[0], ",")
70+
// for _, input := range inputs {
71+
// ok, err := regexp.MatchString(`\A([a-zA-Z0-9_.-]+):([a-zA-Z0-9_.-]+)\z`, input)
72+
// if err != nil {
73+
// return err
74+
// }
75+
// if !ok {
76+
// return fmt.Errorf("invalid label format: %s", input)
77+
// }
78+
79+
// }
80+
81+
// o.labels = args[0]
82+
// return nil
83+
// }
84+
85+
// // Complete completes addLabel command.
86+
// func (o *addLabelOptions) Complete(cmd *cobra.Command, args []string) error {
87+
// return nil
88+
// }
89+
90+
// // RunAddLabel runs addLabel command (do real work).
91+
// func (o *addLabelOptions) RunAddLabel(fsys fs.FileSystem) error {
92+
// mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
93+
// if err != nil {
94+
// return err
95+
// }
96+
97+
// m, err := mf.read()
98+
// if err != nil {
99+
// return err
100+
// }
101+
102+
// if m.CommonLabels == nil {
103+
// m.CommonLabels = make(map[string]string)
104+
// }
105+
106+
// labels := strings.Split(o.labels, ",")
107+
// for _, label := range labels {
108+
// kv := strings.Split(label, ":")
109+
// if _, ok := m.CommonLabels[kv[0]]; ok {
110+
// return fmt.Errorf("label %s already in kustomization file", kv[0])
111+
// }
112+
// m.CommonLabels[kv[0]] = kv[1]
113+
// }
114+
115+
// return mf.write(m)
116+
// }

0 commit comments

Comments
 (0)