From 563ddc472df2c2e4f27694cbe55920b5f632018e Mon Sep 17 00:00:00 2001 From: m-Bilal Date: Mon, 6 Sep 2021 00:45:40 +0530 Subject: [PATCH] Fixes #4108; remove hidden files in kustomize edit command to correctly mimic shell globbing behaviour --- kustomize/commands/edit/add/flagsandargs.go | 5 ++++- .../commands/edit/add/flagsandargs_test.go | 22 +++++++++++++++++++ kustomize/commands/internal/util/util.go | 16 ++++++++++++++ kustomize/commands/internal/util/util_test.go | 16 ++++++++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) diff --git a/kustomize/commands/edit/add/flagsandargs.go b/kustomize/commands/edit/add/flagsandargs.go index 58b7b30072c..ffb4d4944d9 100644 --- a/kustomize/commands/edit/add/flagsandargs.go +++ b/kustomize/commands/edit/add/flagsandargs.go @@ -85,10 +85,13 @@ func (a *flagsAndArgs) ExpandFileSource(fSys filesys.FileSystem) error { } else { patterns = append(patterns, s[0]) } - result, err := util.GlobPatterns(fSys, patterns) + allFilePaths, err := util.GlobPatterns(fSys, patterns) if err != nil { return err } + // allFilePaths will contain hidden files (/.*) + // so, we remove all those hidden files to ensure correct globbing behaviour + result := util.RemoveHiddenFilesAndDirs(allFilePaths) // if the format is `--from-file=[key=]source` accept only one result // and extend it with the `key=` prefix if key != "" { diff --git a/kustomize/commands/edit/add/flagsandargs_test.go b/kustomize/commands/edit/add/flagsandargs_test.go index c3bb9d0dbe3..ca9c9bb9b66 100644 --- a/kustomize/commands/edit/add/flagsandargs_test.go +++ b/kustomize/commands/edit/add/flagsandargs_test.go @@ -149,3 +149,25 @@ func TestExpandFileSourceWithKeyAndError(t *testing.T) { t.Fatalf("FileSources should not be correctly expanded: %v", fa.FileSources) } } + +func TestExpandFileSourceWithHiddenFiles(t *testing.T) { + fSys := filesys.MakeEmptyDirInMemory() + _, err := fSys.Create("dir/fa1") + require.NoError(t, err) + _, err = fSys.Create("dir/fa2") + require.NoError(t, err) + _, err = fSys.Create("dir/.fa3") + require.NoError(t, err) + fa := flagsAndArgs{ + FileSources: []string{"dir/*"}, + } + err = fa.ExpandFileSource(fSys) + require.NoError(t, err) + expected := []string{ + "dir/fa1", + "dir/fa2", + } + if !reflect.DeepEqual(fa.FileSources, expected) { + t.Fatalf("FileSources is not correctly expanded: %v", fa.FileSources) + } +} diff --git a/kustomize/commands/internal/util/util.go b/kustomize/commands/internal/util/util.go index 943c4d4d62a..fcd48c15ba8 100644 --- a/kustomize/commands/internal/util/util.go +++ b/kustomize/commands/internal/util/util.go @@ -96,3 +96,19 @@ func trimQuotes(s string) string { } return s } + +// Removes paths containing hidden files/folders from a list of paths +func RemoveHiddenFilesAndDirs(paths []string) []string { + if len(paths) == 0 { + return paths + } + var result []string + for _, path := range paths { + if strings.Contains(path, "/.") { + continue + } else { + result = append(result, path) + } + } + return result +} diff --git a/kustomize/commands/internal/util/util_test.go b/kustomize/commands/internal/util/util_test.go index 6fab551039e..7ee8b1d71a8 100644 --- a/kustomize/commands/internal/util/util_test.go +++ b/kustomize/commands/internal/util/util_test.go @@ -95,6 +95,22 @@ func TestGlobPatternsWithLoaderRemoteFile(t *testing.T) { } } +func TestRemoveHiddenFilesAndDirs(t *testing.T) { + paths := []string{ + "dir/fa1", + "dir/fa2", + "dir/.fa3", + } + result := RemoveHiddenFilesAndDirs(paths) + expected := []string{ + "dir/fa1", + "dir/fa2", + } + if !reflect.DeepEqual(result, expected) { + t.Fatalf("Hidden dirs not being removed, expected %v but got %v", expected, result) + } +} + type fakeLoader struct { path string }