Skip to content

Commit

Permalink
Fixes #4108; remove hidden files in kustomize edit command to correct…
Browse files Browse the repository at this point in the history
…ly mimic shell globbing behaviour
  • Loading branch information
m-Bilal committed Sep 5, 2021
1 parent 4d002af commit 563ddc4
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
5 changes: 4 additions & 1 deletion kustomize/commands/edit/add/flagsandargs.go
Expand Up @@ -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 != "" {
Expand Down
22 changes: 22 additions & 0 deletions kustomize/commands/edit/add/flagsandargs_test.go
Expand Up @@ -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)
}
}
16 changes: 16 additions & 0 deletions kustomize/commands/internal/util/util.go
Expand Up @@ -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
}
16 changes: 16 additions & 0 deletions kustomize/commands/internal/util/util_test.go
Expand Up @@ -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
}
Expand Down

0 comments on commit 563ddc4

Please sign in to comment.