Skip to content

Commit

Permalink
Fixes 4108; exclude or include hidden files in kustomize edit command…
Browse files Browse the repository at this point in the history
… according to glob pattern
  • Loading branch information
m-Bilal committed Sep 8, 2021
1 parent 4d002af commit 2a7ffc6
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
10 changes: 9 additions & 1 deletion kustomize/commands/edit/add/flagsandargs.go
Expand Up @@ -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
Expand All @@ -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.ShouldGlobHiddenFiles(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 != "" {
Expand Down
35 changes: 35 additions & 0 deletions kustomize/commands/edit/add/flagsandargs_test.go
Expand Up @@ -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)
}
}
22 changes: 22 additions & 0 deletions kustomize/commands/internal/util/util.go
Expand Up @@ -6,6 +6,7 @@ package util
import (
"fmt"
"log"
"path/filepath"
"strings"

"sigs.k8s.io/kustomize/api/ifc"
Expand Down Expand Up @@ -96,3 +97,24 @@ func trimQuotes(s string) string {
}
return s
}

// Checks whether the given glob pattern intentionally wants to include hidden files
func ShouldGlobHiddenFiles(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 strings.HasPrefix(filepath.Base(path), ".") {
continue
} else {
result = append(result, path)
}
}
return result
}
41 changes: 41 additions & 0 deletions kustomize/commands/internal/util/util_test.go
Expand Up @@ -95,6 +95,47 @@ func TestGlobPatternsWithLoaderRemoteFile(t *testing.T) {
}
}

func TestShouldGlobHiddenFiles(t *testing.T) {
hiddenFilesInclusionPatterns := []string{
".*",
"/.*",
"dir/.*",
}
hiddenFilesExclusionPatterns := []string{
"*",
"/*",
"dir/*",
}
for _, pattern := range hiddenFilesInclusionPatterns {
res := ShouldGlobHiddenFiles(pattern)
if !res {
t.Fatalf("Incorrectly not including hidden files for glob pattern: %s\n", pattern)
}
}
for _, pattern := range hiddenFilesExclusionPatterns {
res := ShouldGlobHiddenFiles(pattern)
if res {
t.Fatalf("Incorrectly including hidden files for glob pattern: %s\n", pattern)
}
}
}

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
}
Expand Down

0 comments on commit 2a7ffc6

Please sign in to comment.