Skip to content

Commit e78b303

Browse files
committed
Excluding sub-directories from dir walker
1 parent aa5341f commit e78b303

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

util/files.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,26 @@ func ReadEntireFile(fullPath string) string {
5050

5151
// GetFilePathListWithPattern returns the full paths for files matching the pattern in the base path.
5252
func GetFilePathListWithPattern(basePath, fileNamePattern string) []string {
53+
sepCount := strings.Count(basePath, string(os.PathSeparator))
54+
5355
var files []string
5456
filepath.WalkDir(basePath, func(s string, d fs.DirEntry, e error) error {
5557
if e != nil {
5658
return e
5759
}
60+
61+
// Skipping sub-directories.
62+
if d.IsDir() && strings.Count(s, string(os.PathSeparator)) > sepCount {
63+
log.Println("Skipping sub-directory:", s)
64+
return fs.SkipDir
65+
}
66+
5867
if matched, _ := filepath.Match(fileNamePattern, d.Name()); matched {
5968
files = append(files, s)
6069
}
6170
return nil
6271
})
63-
log.Println("Found", len(files), "matched files with pattern:", fileNamePattern)
72+
log.Println("Found", len(files), "matched files with pattern:", fileNamePattern, "in", basePath)
6473
return files
6574
}
6675

0 commit comments

Comments
 (0)