From 524d593c5c18ce90e73a4d440d651d1a2885b229 Mon Sep 17 00:00:00 2001 From: guineveresaenger Date: Wed, 15 Aug 2018 03:51:56 -0700 Subject: [PATCH] Separate functions for RunnAddLabel and RunAddAnnotation --- pkg/commands/addmetadata.go | 46 +++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/pkg/commands/addmetadata.go b/pkg/commands/addmetadata.go index d4898b5cb..7579ca961 100644 --- a/pkg/commands/addmetadata.go +++ b/pkg/commands/addmetadata.go @@ -63,7 +63,7 @@ func newCmdAddAnnotation(fsys fs.FileSystem) *cobra.Command { if err != nil { return err } - return o.RunAddMetadata(fsys, annotation) + return o.RunAddAnnotation(fsys, annotation) }, } return cmd @@ -83,7 +83,7 @@ func newCmdAddLabel(fsys fs.FileSystem) *cobra.Command { if err != nil { return err } - return o.RunAddMetadata(fsys, label) + return o.RunAddLabel(fsys, label) }, } return cmd @@ -118,33 +118,22 @@ func (o *addMetadataOptions) Validate(args []string, k KindOfAdd) error { return nil } -// RunAddMetadata runs addLabel and addAnnotation commands (do real work). -func (o *addMetadataOptions) RunAddMetadata(fsys fs.FileSystem, k KindOfAdd) error { +// RunAddAnnotation runs addAnnotation command, doing the real work. +func (o *addMetadataOptions) RunAddAnnotation(fsys fs.FileSystem, k KindOfAdd) error { mf, err := newKustomizationFile(constants.KustomizationFileName, fsys) if err != nil { return err } - m, err := mf.read() if err != nil { return err } - if k == label && m.CommonLabels == nil { - m.CommonLabels = make(map[string]string) - } - - if k == annotation && m.CommonAnnotations == nil { + if m.CommonAnnotations == nil { m.CommonAnnotations = make(map[string]string) } for key, value := range o.metadata { - if k == label { - if _, ok := m.CommonLabels[key]; ok { - return fmt.Errorf("%s %s already in kustomization file", k, key) - } - m.CommonLabels[key] = value - } if k == annotation { if _, ok := m.CommonAnnotations[key]; ok { return fmt.Errorf("%s %s already in kustomization file", k, key) @@ -152,6 +141,29 @@ func (o *addMetadataOptions) RunAddMetadata(fsys fs.FileSystem, k KindOfAdd) err m.CommonAnnotations[key] = value } } - + return mf.write(m) +} + +// RunAddLabel runs addLabel command, doing the real work. +func (o *addMetadataOptions) RunAddLabel(fsys fs.FileSystem, k KindOfAdd) error { + mf, err := newKustomizationFile(constants.KustomizationFileName, fsys) + if err != nil { + return err + } + m, err := mf.read() + if err != nil { + return err + } + + if m.CommonLabels == nil { + m.CommonLabels = make(map[string]string) + } + + for key, value := range o.metadata { + if _, ok := m.CommonLabels[key]; ok { + return fmt.Errorf("%s %s already in kustomization file", k, key) + } + m.CommonLabels[key] = value + } return mf.write(m) }