diff --git a/pkg/commands/edit/add/flagsandargs.go b/pkg/commands/edit/add/flagsandargs.go index 77bcc6b61..fb68fc87e 100644 --- a/pkg/commands/edit/add/flagsandargs.go +++ b/pkg/commands/edit/add/flagsandargs.go @@ -18,6 +18,7 @@ package add import ( "fmt" + "strings" "sigs.k8s.io/kustomize/pkg/fs" ) @@ -54,10 +55,33 @@ func (a *flagsAndArgs) Validate(args []string) error { } func (a *flagsAndArgs) ExpandFileSource(fSys fs.FileSystem) error { - result, err := globPatterns(fSys, a.FileSources) - if err != nil { - return err + var results []string + var key string + for _, pattern := range a.FileSources { + var patterns []string + key = "" + s := strings.Split(pattern, "=") + if len(s) == 2 { + patterns = append(patterns, s[1]) + key = s[0] + } else { + patterns = append(patterns, s[0]) + } + result, err := globPatterns(fSys, patterns) + if err != nil { + return err + } + if key != "" { + if len(result) != 1 { + msg := fmt.Sprintf("%s pattern should not catch more than one file", pattern) + return fmt.Errorf(msg) + } + fileSource := fmt.Sprintf("%s=%s", key, result[0]) + results = append(results, fileSource) + } else { + results = append(results, result...) + } } - a.FileSources = result + a.FileSources = results return nil } diff --git a/pkg/commands/edit/add/flagsandargs_test.go b/pkg/commands/edit/add/flagsandargs_test.go index 5638fe71f..108634d00 100644 --- a/pkg/commands/edit/add/flagsandargs_test.go +++ b/pkg/commands/edit/add/flagsandargs_test.go @@ -102,3 +102,33 @@ func TestExpandFileSource(t *testing.T) { t.Fatalf("FileSources is not correctly expanded: %v", fa.FileSources) } } + +func TestExpandFileSourceWithKey(t *testing.T) { + fakeFS := fs.MakeFakeFS() + fakeFS.Create("dir/fa1") + fakeFS.Create("dir/reademe") + fa := flagsAndArgs{ + FileSources: []string{"foo-key=dir/fa*"}, + } + fa.ExpandFileSource(fakeFS) + expected := []string{ + "foo-key=dir/fa1", + } + if !reflect.DeepEqual(fa.FileSources, expected) { + t.Fatalf("FileSources is not correctly expanded: %v", fa.FileSources) + } +} + +func TestExpandFileSourceWithKeyAndError(t *testing.T) { + fakeFS := fs.MakeFakeFS() + fakeFS.Create("dir/fa1") + fakeFS.Create("dir/fa2") + fakeFS.Create("dir/reademe") + fa := flagsAndArgs{ + FileSources: []string{"foo-key=dir/fa*"}, + } + err := fa.ExpandFileSource(fakeFS) + if err == nil { + t.Fatalf("FileSources should not be correctly expanded: %v", fa.FileSources) + } +} \ No newline at end of file