Skip to content

Commit 5647e37

Browse files
committed
Made ReadDirRecursiveFiltered recurse internally
This prepares for the next change that requires to keep a shared state in the recursive calls. This change also avoids the need to pass `recursionFilter` and `filters` back into the recursive call.
1 parent 9c94322 commit 5647e37

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

Diff for: readdir.go

+30-24
Original file line numberDiff line numberDiff line change
@@ -81,41 +81,47 @@ func (p *Path) ReadDirRecursive() (PathList, error) {
8181
// - `filters` are the filters that are checked to determine if the entry should be
8282
// added to the resulting PathList
8383
func (p *Path) ReadDirRecursiveFiltered(recursionFilter ReadDirFilter, filters ...ReadDirFilter) (PathList, error) {
84-
infos, err := os.ReadDir(p.path)
85-
if err != nil {
86-
return nil, err
87-
}
84+
var search func(*Path) (PathList, error)
8885

89-
accept := func(p *Path) bool {
90-
for _, filter := range filters {
91-
if !filter(p) {
92-
return false
86+
search = func(currPath *Path) (PathList, error) {
87+
infos, err := os.ReadDir(currPath.path)
88+
if err != nil {
89+
return nil, err
90+
}
91+
92+
accept := func(p *Path) bool {
93+
for _, filter := range filters {
94+
if !filter(p) {
95+
return false
96+
}
9397
}
98+
return true
9499
}
95-
return true
96-
}
97100

98-
paths := PathList{}
99-
for _, info := range infos {
100-
path := p.Join(info.Name())
101+
paths := PathList{}
102+
for _, info := range infos {
103+
path := currPath.Join(info.Name())
101104

102-
if accept(path) {
103-
paths.Add(path)
104-
}
105+
if accept(path) {
106+
paths.Add(path)
107+
}
105108

106-
if recursionFilter == nil || recursionFilter(path) {
107-
if isDir, err := path.IsDirCheck(); err != nil {
108-
return nil, err
109-
} else if isDir {
110-
subPaths, err := path.ReadDirRecursiveFiltered(recursionFilter, filters...)
111-
if err != nil {
109+
if recursionFilter == nil || recursionFilter(path) {
110+
if isDir, err := path.IsDirCheck(); err != nil {
112111
return nil, err
112+
} else if isDir {
113+
subPaths, err := search(path)
114+
if err != nil {
115+
return nil, err
116+
}
117+
paths.AddAll(subPaths)
113118
}
114-
paths.AddAll(subPaths)
115119
}
116120
}
121+
return paths, nil
117122
}
118-
return paths, nil
123+
124+
return search(p)
119125
}
120126

121127
// FilterDirectories is a ReadDirFilter that accepts only directories

0 commit comments

Comments
 (0)