diff --git a/pkg/commands/edit/add/flagsandargs.go b/pkg/commands/edit/add/flagsandargs.go index b915cbf51..a842ed817 100644 --- a/pkg/commands/edit/add/flagsandargs.go +++ b/pkg/commands/edit/add/flagsandargs.go @@ -54,12 +54,28 @@ func (a *flagsAndArgs) Validate(args []string) error { return nil } +// ExpandFileSource normalizes a string list, possibly +// containing globs, into a validated, globless list. +// For example, this list: +// some/path +// some/dir/a* +// bfile=some/dir/b* +// becomes: +// some/path +// some/dir/airplane +// some/dir/ant +// some/dir/apple +// bfile=some/dir/banana +// i.e. everything is converted to a key=value pair, +// where the value is always a relative file path, +// and the key, if missing, is the same as the value. +// In the case where the key is explicitly declared, +// the globbing, if present, must have exactly one match. func (a *flagsAndArgs) ExpandFileSource(fSys fs.FileSystem) error { var results []string - var key string for _, pattern := range a.FileSources { var patterns []string - key = "" + key := "" // check if the pattern is in `--from-file=[key=]source` format // and if so split it to send only the file-pattern to glob function s := strings.Split(pattern, "=") @@ -77,8 +93,8 @@ func (a *flagsAndArgs) ExpandFileSource(fSys fs.FileSystem) error { // and extend it with the `key=` prefix if key != "" { if len(result) != 1 { - msg := fmt.Sprintf("%s pattern should not catch more than one file", pattern) - return fmt.Errorf(msg) + return fmt.Errorf( + "'pattern '%s' catches files %v, should catch only one.", pattern, result) } fileSource := fmt.Sprintf("%s=%s", key, result[0]) results = append(results, fileSource) diff --git a/pkg/commands/edit/add/flagsandargs_test.go b/pkg/commands/edit/add/flagsandargs_test.go index 5839f5a18..797a4639e 100644 --- a/pkg/commands/edit/add/flagsandargs_test.go +++ b/pkg/commands/edit/add/flagsandargs_test.go @@ -89,7 +89,7 @@ func TestExpandFileSource(t *testing.T) { fakeFS := fs.MakeFakeFS() fakeFS.Create("dir/fa1") fakeFS.Create("dir/fa2") - fakeFS.Create("dir/reademe") + fakeFS.Create("dir/readme") fa := flagsAndArgs{ FileSources: []string{"dir/fa*"}, } @@ -105,14 +105,18 @@ func TestExpandFileSource(t *testing.T) { func TestExpandFileSourceWithKey(t *testing.T) { fakeFS := fs.MakeFakeFS() - fakeFS.Create("dir/fa1") - fakeFS.Create("dir/reademe") + fakeFS.Create("dir/faaaaaaaaaabbbbbbbbbccccccccccccccccc") + fakeFS.Create("dir/foobar") + fakeFS.Create("dir/simplebar") + fakeFS.Create("dir/readme") fa := flagsAndArgs{ - FileSources: []string{"foo-key=dir/fa*"}, + FileSources: []string{"foo-key=dir/fa*", "bar-key=dir/foobar", "dir/simplebar"}, } fa.ExpandFileSource(fakeFS) expected := []string{ - "foo-key=dir/fa1", + "foo-key=dir/faaaaaaaaaabbbbbbbbbccccccccccccccccc", + "bar-key=dir/foobar", + "dir/simplebar", } if !reflect.DeepEqual(fa.FileSources, expected) { t.Fatalf("FileSources is not correctly expanded: %v", fa.FileSources) @@ -123,7 +127,7 @@ func TestExpandFileSourceWithKeyAndError(t *testing.T) { fakeFS := fs.MakeFakeFS() fakeFS.Create("dir/fa1") fakeFS.Create("dir/fa2") - fakeFS.Create("dir/reademe") + fakeFS.Create("dir/readme") fa := flagsAndArgs{ FileSources: []string{"foo-key=dir/fa*"}, }