Skip to content

Commit

Permalink
Filesystem improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleu committed Dec 14, 2024
1 parent 1d87759 commit fd34f2c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/lib/filesystem/fileloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type FileLoader interface {
CopyRecursive(src string, tgt string, ignore []string, logger util.Logger) error
Move(src string, tgt string) error
ListFiles(path string, ignore []string, logger util.Logger) FileInfos
ListFilesRecursive(path string, ignore []string, logger util.Logger) ([]string, error)
ListFilesRecursive(path string, ignore []string, logger util.Logger, patterns ...string) ([]string, error)
ListTree(cfg util.ValueMap, path string, ignore []string, logger util.Logger, tags ...string) (*Tree, error)
ListJSON(path string, ignore []string, trimExtension bool, logger util.Logger) []string
ListExtension(path string, ext string, ignore []string, trimExtension bool, logger util.Logger) []string
Expand Down
32 changes: 23 additions & 9 deletions app/lib/filesystem/fsignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,42 @@ func buildIgnore(ign []string) []string {
}

const (
keyPrefix = "^"
keySuffix = "$"
keyPrefix = "^"
keySuffix = "$"
keyWildcard = "*"
)

func checkIgnore(ignore []string, fp string) bool {
for _, i := range ignore {
switch {
case strings.HasPrefix(i, keyPrefix):
i = strings.TrimPrefix(i, keyPrefix)
if fp == i {
return true
}
if strings.HasPrefix(i, keyWildcard) && strings.HasSuffix(i, keyWildcard) {
if strings.Contains(fp, strings.TrimSuffix(strings.TrimPrefix(i, keyWildcard), keyWildcard)) {
return true
}
}
if strings.HasPrefix(i, keyPrefix) || strings.HasSuffix(i, keyWildcard) {
i = strings.TrimPrefix(strings.TrimSuffix(i, keyWildcard), keyPrefix)
if fp == strings.TrimSuffix(i, "/") || fp == strings.TrimSuffix(i, "\\") {
return true
}
if strings.HasPrefix(fp, i) {
return true
}
case strings.HasSuffix(i, keySuffix):
if strings.HasSuffix(fp, strings.TrimSuffix(i, keySuffix)) {
}
if strings.HasSuffix(i, keySuffix) || strings.HasPrefix(i, keyWildcard) {
if strings.HasSuffix(fp, strings.TrimSuffix(strings.TrimPrefix(i, keyWildcard), keySuffix)) {
return true
}
case fp == i:
return true
}
}
return false
}

func checkPatterns(patterns []string, pth string) bool {
if len(patterns) == 0 {
return true
}
return checkIgnore(patterns, pth)
}
5 changes: 4 additions & 1 deletion app/lib/filesystem/fslist.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (f *FileSystem) ListDirectories(path string, ign []string, logger util.Logg
})
}

func (f *FileSystem) ListFilesRecursive(path string, ign []string, _ util.Logger) ([]string, error) {
func (f *FileSystem) ListFilesRecursive(path string, ign []string, _ util.Logger, patterns ...string) ([]string, error) {
ignore := buildIgnore(ign)
p := f.getPath(path)
ret := &util.StringSlice{}
Expand All @@ -70,6 +70,9 @@ func (f *FileSystem) ListFilesRecursive(path string, ign []string, _ util.Logger
if checkIgnore(ignore, m) {
return nil
}
if !checkPatterns(patterns, m) {
return nil
}
if info != nil && (!info.IsDir()) && (info.Mode()&os.ModeSymlink != os.ModeSymlink) && (strings.Contains(fp, "/") || strings.Contains(fp, "\\")) {
ret.Push(m)
}
Expand Down

0 comments on commit fd34f2c

Please sign in to comment.