From 1382d87d7f40ac3fcea776e0018fcfcd5b824179 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 13 Feb 2019 00:24:20 +0200 Subject: [PATCH 1/3] Change ExpandFileSource to work with key=val patterns Signed-off-by: Chris --- pkg/commands/edit/add/flagsandargs.go | 32 +++++++++++++++++++--- pkg/commands/edit/add/flagsandargs_test.go | 30 ++++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) 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 From f5f8e49fa336e63747576c4f036715f60724493e Mon Sep 17 00:00:00 2001 From: Chris Mark Date: Wed, 13 Feb 2019 09:38:13 +0200 Subject: [PATCH 2/3] Add explanatory comments and format Signed-off-by: Chris Mark --- pkg/commands/edit/add/flagsandargs.go | 4 ++++ pkg/commands/edit/add/flagsandargs_test.go | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/commands/edit/add/flagsandargs.go b/pkg/commands/edit/add/flagsandargs.go index fb68fc87e..b915cbf51 100644 --- a/pkg/commands/edit/add/flagsandargs.go +++ b/pkg/commands/edit/add/flagsandargs.go @@ -60,6 +60,8 @@ func (a *flagsAndArgs) ExpandFileSource(fSys fs.FileSystem) error { for _, pattern := range a.FileSources { var patterns []string 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, "=") if len(s) == 2 { patterns = append(patterns, s[1]) @@ -71,6 +73,8 @@ func (a *flagsAndArgs) ExpandFileSource(fSys fs.FileSystem) error { if err != nil { return err } + // if the format is `--from-file=[key=]source` accept only one result + // 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) diff --git a/pkg/commands/edit/add/flagsandargs_test.go b/pkg/commands/edit/add/flagsandargs_test.go index 108634d00..5839f5a18 100644 --- a/pkg/commands/edit/add/flagsandargs_test.go +++ b/pkg/commands/edit/add/flagsandargs_test.go @@ -131,4 +131,4 @@ func TestExpandFileSourceWithKeyAndError(t *testing.T) { if err == nil { t.Fatalf("FileSources should not be correctly expanded: %v", fa.FileSources) } -} \ No newline at end of file +} From 77eebb89fd511c0f18aaacd6d9c8aabe6cdd0b5b Mon Sep 17 00:00:00 2001 From: Chris Mark Date: Mon, 18 Feb 2019 09:28:04 +0200 Subject: [PATCH 3/3] Review changes Signed-off-by: Chris Mark --- pkg/commands/edit/add/flagsandargs.go | 24 ++++++++++++++++++---- pkg/commands/edit/add/flagsandargs_test.go | 16 +++++++++------ 2 files changed, 30 insertions(+), 10 deletions(-) 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*"}, }