From bf73633cda0c6304d65361e250f53e8dfdef1e18 Mon Sep 17 00:00:00 2001 From: Jingfang Liu Date: Wed, 1 Aug 2018 15:10:46 -0700 Subject: [PATCH] Add glob support in subcommands add patch and add configmap --- pkg/commands/addpatch.go | 26 +++++++++++------- pkg/commands/addpatch_test.go | 15 ++++++----- pkg/commands/addresource.go | 16 +++-------- pkg/commands/cmapflagsandargs.go | 11 ++++++++ pkg/commands/cmapflagsandargs_test.go | 21 +++++++++++++++ pkg/commands/configmap.go | 5 ++++ pkg/commands/util.go | 39 +++++++++++++++++++++++++++ 7 files changed, 103 insertions(+), 30 deletions(-) create mode 100644 pkg/commands/util.go diff --git a/pkg/commands/addpatch.go b/pkg/commands/addpatch.go index fd311261a..36b243889 100644 --- a/pkg/commands/addpatch.go +++ b/pkg/commands/addpatch.go @@ -18,7 +18,7 @@ package commands import ( "errors" - "fmt" + "log" "github.com/spf13/cobra" @@ -27,7 +27,7 @@ import ( ) type addPatchOptions struct { - patchFilePath string + patchFilePaths []string } // newCmdAddPatch adds the name of a file containing a patch to the kustomization file. @@ -56,10 +56,10 @@ func newCmdAddPatch(fsys fs.FileSystem) *cobra.Command { // Validate validates addPatch command. func (o *addPatchOptions) Validate(args []string) error { - if len(args) != 1 { + if len(args) == 0 { return errors.New("must specify a patch file") } - o.patchFilePath = args[0] + o.patchFilePaths = args return nil } @@ -70,8 +70,12 @@ func (o *addPatchOptions) Complete(cmd *cobra.Command, args []string) error { // RunAddPatch runs addPatch command (do real work). func (o *addPatchOptions) RunAddPatch(fsys fs.FileSystem) error { - if !fsys.Exists(o.patchFilePath) { - return errors.New(o.patchFilePath + " doesn't exist") + patches, err := globPatterns(fsys, o.patchFilePaths) + if err != nil { + return err + } + if len(patches) == 0 { + return nil } mf, err := newKustomizationFile(constants.KustomizationFileName, fsys) @@ -84,11 +88,13 @@ func (o *addPatchOptions) RunAddPatch(fsys fs.FileSystem) error { return err } - if stringInSlice(o.patchFilePath, m.Patches) { - return fmt.Errorf("patch %s already in kustomization file", o.patchFilePath) + for _, patch := range patches { + if stringInSlice(patch, m.Patches) { + log.Printf("patch %s already in kustomization file", patch) + continue + } + m.Patches = append(m.Patches, patch) } - m.Patches = append(m.Patches, o.patchFilePath) - return mf.write(m) } diff --git a/pkg/commands/addpatch_test.go b/pkg/commands/addpatch_test.go index 1705ebb0a..971c89eb4 100644 --- a/pkg/commands/addpatch_test.go +++ b/pkg/commands/addpatch_test.go @@ -36,10 +36,11 @@ sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. func TestAddPatchHappyPath(t *testing.T) { fakeFS := fs.MakeFakeFS() fakeFS.WriteFile(patchFileName, []byte(patchFileContent)) + fakeFS.WriteFile(patchFileName+"another", []byte(patchFileContent)) fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent)) cmd := newCmdAddPatch(fakeFS) - args := []string{patchFileName} + args := []string{patchFileName + "*"} err := cmd.RunE(cmd, args) if err != nil { t.Errorf("unexpected cmd error: %v", err) @@ -51,6 +52,9 @@ func TestAddPatchHappyPath(t *testing.T) { if !strings.Contains(string(content), patchFileName) { t.Errorf("expected patch name in kustomization") } + if !strings.Contains(string(content), patchFileName+"another") { + t.Errorf("expected patch name in kustomization") + } } func TestAddPatchAlreadyThere(t *testing.T) { @@ -65,13 +69,10 @@ func TestAddPatchAlreadyThere(t *testing.T) { t.Fatalf("unexpected cmd error: %v", err) } - // adding an existing patch should return an error + // adding an existing patch shouldn't return an error err = cmd.RunE(cmd, args) - if err == nil { - t.Errorf("expected already there problem") - } - if err.Error() != "patch "+patchFileName+" already in kustomization file" { - t.Errorf("unexpected error %v", err) + if err != nil { + t.Errorf("unexpected cmd error: %v", err) } } diff --git a/pkg/commands/addresource.go b/pkg/commands/addresource.go index ae6e99c5c..14c0baa2d 100644 --- a/pkg/commands/addresource.go +++ b/pkg/commands/addresource.go @@ -70,20 +70,10 @@ func (o *addResourceOptions) Complete(cmd *cobra.Command, args []string) error { // RunAddResource runs addResource command (do real work). func (o *addResourceOptions) RunAddResource(fsys fs.FileSystem) error { - var resources []string - - for _, pattern := range o.resourceFilePaths { - files, err := fsys.Glob(pattern) - if err != nil { - return err - } - if len(files) == 0 { - log.Printf("%s has no match", pattern) - continue - } - resources = append(resources, files...) + resources, err := globPatterns(fsys, o.resourceFilePaths) + if err != nil { + return err } - if len(resources) == 0 { return nil } diff --git a/pkg/commands/cmapflagsandargs.go b/pkg/commands/cmapflagsandargs.go index 1a593a693..c706c0206 100644 --- a/pkg/commands/cmapflagsandargs.go +++ b/pkg/commands/cmapflagsandargs.go @@ -18,6 +18,8 @@ package commands import ( "fmt" + + "github.com/kubernetes-sigs/kustomize/pkg/fs" ) // cMapFlagsAndArgs encapsulates the options for add configmap commands. @@ -48,3 +50,12 @@ func (a *cMapFlagsAndArgs) Validate(args []string) error { // TODO: Should we check if the path exists? if it's valid, if it's within the same (sub-)directory? return nil } + +func (a *cMapFlagsAndArgs) ExpandFileSource(fSys fs.FileSystem) error { + result, err := globPatterns(fSys, a.FileSources) + if err != nil { + return err + } + a.FileSources = result + return nil +} diff --git a/pkg/commands/cmapflagsandargs_test.go b/pkg/commands/cmapflagsandargs_test.go index cc5048d0a..7ee92a905 100644 --- a/pkg/commands/cmapflagsandargs_test.go +++ b/pkg/commands/cmapflagsandargs_test.go @@ -17,7 +17,10 @@ limitations under the License. package commands import ( + "reflect" "testing" + + "github.com/kubernetes-sigs/kustomize/pkg/fs" ) func TestDataConfigValidation_NoName(t *testing.T) { @@ -81,3 +84,21 @@ func TestDataConfigValidation_Flags(t *testing.T) { } } } + +func TestExpandFileSource(t *testing.T) { + fakeFS := fs.MakeFakeFS() + fakeFS.Create("dir/config1") + fakeFS.Create("dir/config2") + fakeFS.Create("dir/reademe") + config := cMapFlagsAndArgs{ + FileSources: []string{"dir/config*"}, + } + config.ExpandFileSource(fakeFS) + expected := []string{ + "dir/config1", + "dir/config2", + } + if !reflect.DeepEqual(config.FileSources, expected) { + t.Fatalf("FileSources is not correctly expanded: %v", config.FileSources) + } +} diff --git a/pkg/commands/configmap.go b/pkg/commands/configmap.go index c7b4de365..10b1eaeee 100644 --- a/pkg/commands/configmap.go +++ b/pkg/commands/configmap.go @@ -50,6 +50,11 @@ func newCmdAddConfigMap(fSys fs.FileSystem) *cobra.Command { return err } + err = flagsAndArgs.ExpandFileSource(fSys) + if err != nil { + return err + } + // Load the kustomization file. mf, err := newKustomizationFile(constants.KustomizationFileName, fSys) if err != nil { diff --git a/pkg/commands/util.go b/pkg/commands/util.go new file mode 100644 index 000000000..c01780be2 --- /dev/null +++ b/pkg/commands/util.go @@ -0,0 +1,39 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package commands + +import ( + "log" + + "github.com/kubernetes-sigs/kustomize/pkg/fs" +) + +func globPatterns(fsys fs.FileSystem, patterns []string) ([]string, error) { + var result []string + for _, pattern := range patterns { + files, err := fsys.Glob(pattern) + if err != nil { + return nil, err + } + if len(files) == 0 { + log.Printf("%s has no match", pattern) + continue + } + result = append(result, files...) + } + return result, nil +}