Skip to content

Commit 3009dd6

Browse files
committed
Added ReadDirFilter feature in ReadDir
1 parent 202137f commit 3009dd6

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

readdir.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,30 @@
2929

3030
package paths
3131

32-
import "io/ioutil"
32+
import (
33+
"io/ioutil"
34+
)
35+
36+
// ReadDirFilter is a filter for Path.ReadDir and Path.ReadDirRecursive methods.
37+
// The filter should return true to accept a file or false to reject it.
38+
type ReadDirFilter func(file *Path) bool
3339

3440
// ReadDir returns a PathList containing the content of the directory
35-
// pointed by the current Path
36-
func (p *Path) ReadDir() (PathList, error) {
41+
// pointed by the current Path. The resulting list is filtered by the given filters chained.
42+
func (p *Path) ReadDir(filters ...ReadDirFilter) (PathList, error) {
3743
infos, err := ioutil.ReadDir(p.path)
3844
if err != nil {
3945
return nil, err
4046
}
4147
paths := PathList{}
48+
fileLoop:
4249
for _, info := range infos {
4350
path := p.Join(info.Name())
51+
for _, filter := range filters {
52+
if !filter(path) {
53+
continue fileLoop
54+
}
55+
}
4456
paths.Add(path)
4557
}
4658
return paths, nil

0 commit comments

Comments
 (0)