From 48cc45dc6bc4fa48e058ec41740b1ff9095605c3 Mon Sep 17 00:00:00 2001 From: m-Bilal Date: Thu, 9 Sep 2021 06:50:09 +0530 Subject: [PATCH] Fixes 4108; exclude or include hidden files in kustomize edit command according to glob pattern --- kustomize/commands/edit/add/flagsandargs.go | 10 ++++- .../commands/edit/add/flagsandargs_test.go | 35 ++++++++++++++++ kustomize/commands/internal/util/util.go | 19 +++++++++ kustomize/commands/internal/util/util_test.go | 41 +++++++++++++++++++ 4 files changed, 104 insertions(+), 1 deletion(-) diff --git a/kustomize/commands/edit/add/flagsandargs.go b/kustomize/commands/edit/add/flagsandargs.go index 58b7b30072c..2d5c937f8e9 100644 --- a/kustomize/commands/edit/add/flagsandargs.go +++ b/kustomize/commands/edit/add/flagsandargs.go @@ -75,6 +75,7 @@ func (a *flagsAndArgs) ExpandFileSource(fSys filesys.FileSystem) error { var results []string for _, pattern := range a.FileSources { var patterns []string + var result []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 @@ -85,10 +86,17 @@ 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 + // we check if hidden files should be included, if not, we remove them + if util.IsHiddenFilePath(patterns[0]) { + result = allFilePaths + } else { + result = util.RemoveHiddenFiles(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..c3becdfe388 100644 --- a/kustomize/commands/edit/add/flagsandargs_test.go +++ b/kustomize/commands/edit/add/flagsandargs_test.go @@ -149,3 +149,38 @@ 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/file-1.xtn") + require.NoError(t, err) + _, err = fSys.Create("dir/file-2") + require.NoError(t, err) + _, err = fSys.Create("dir/.file-3") + require.NoError(t, err) + _, err = fSys.Create("dir/dir-2/.file-4") + require.NoError(t, err) + _, err = fSys.Create("dir/dir-2/file-5.xtn") + require.NoError(t, err) + _, err = fSys.Create("dir/dir-2/.file-6.xtn") + require.NoError(t, err) + _, err = fSys.Create("root-1.xtn") + require.NoError(t, err) + _, err = fSys.Create(".root-2.xtn") + require.NoError(t, err) + fa := flagsAndArgs{ + FileSources: []string{"dir/*", "dir/dir-2/.*", ".*"}, + } + err = fa.ExpandFileSource(fSys) + require.NoError(t, err) + expected := []string{ + "dir/file-1.xtn", + "dir/file-2", + "dir/dir-2/.file-4", + "dir/dir-2/.file-6.xtn", + ".root-2.xtn", + } + 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..4c25e4d0de8 100644 --- a/kustomize/commands/internal/util/util.go +++ b/kustomize/commands/internal/util/util.go @@ -6,6 +6,7 @@ package util import ( "fmt" "log" + "path/filepath" "strings" "sigs.k8s.io/kustomize/api/ifc" @@ -96,3 +97,21 @@ func trimQuotes(s string) string { } return s } + +func IsHiddenFilePath(pattern string) bool { + return strings.HasPrefix(filepath.Base(pattern), ".") +} + +// Removes paths containing hidden files/folders from a list of paths +func RemoveHiddenFiles(paths []string) []string { + if len(paths) == 0 { + return paths + } + var result []string + for _, path := range paths { + if !IsHiddenFilePath(path) { + 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..df01d13c840 100644 --- a/kustomize/commands/internal/util/util_test.go +++ b/kustomize/commands/internal/util/util_test.go @@ -95,6 +95,47 @@ func TestGlobPatternsWithLoaderRemoteFile(t *testing.T) { } } +func TestIsHiddenFilePath(t *testing.T) { + hiddenFilePaths := []string{ + ".*", + "/.*", + "dir/.*", + } + filePaths := []string{ + "*", + "/*", + "dir/*", + } + for _, path := range hiddenFilePaths { + res := IsHiddenFilePath(path) + if !res { + t.Fatalf("Hidden file path not correctly identified: %s\n", path) + } + } + for _, path := range filePaths { + res := IsHiddenFilePath(path) + if res { + t.Fatalf("Normal file path incorrectly identified as hidden file path: %s\n", path) + } + } +} + +func TestRemoveHiddenFiles(t *testing.T) { + paths := []string{ + "dir/fa1", + "dir/fa2", + "dir/.fa3", + } + result := RemoveHiddenFiles(paths) + expected := []string{ + "dir/fa1", + "dir/fa2", + } + if !reflect.DeepEqual(result, expected) { + t.Fatalf("Hidden dirs not being removed, expected %v but got %v\n", expected, result) + } +} + type fakeLoader struct { path string }