Skip to content

Commit e612dec

Browse files
committed
Added FilterName and FilterOutName filters
1 parent 6ca2230 commit e612dec

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

readdir.go

+24
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,30 @@ func FilterOutDirectories() ReadDirFilter {
152152
}
153153
}
154154

155+
// FilterNames is a ReadDirFilter that accepts only the given filenames
156+
func FilterNames(allowedNames ...string) ReadDirFilter {
157+
return func(file *Path) bool {
158+
for _, name := range allowedNames {
159+
if file.Base() == name {
160+
return true
161+
}
162+
}
163+
return false
164+
}
165+
}
166+
167+
// FilterOutNames is a ReadDirFilter that rejects the given filenames
168+
func FilterOutNames(rejectedNames ...string) ReadDirFilter {
169+
return func(file *Path) bool {
170+
for _, name := range rejectedNames {
171+
if file.Base() == name {
172+
return false
173+
}
174+
}
175+
return true
176+
}
177+
}
178+
155179
// FilterSuffixes creates a ReadDirFilter that accepts only the given
156180
// filename suffixes
157181
func FilterSuffixes(allowedSuffixes ...string) ReadDirFilter {

readdir_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,24 @@ func TestReadDirRecursiveFiltered(t *testing.T) {
216216
pathEqualsTo(t, "_testdata/symlinktofolder/subfolder/file4", l[8])
217217
pathEqualsTo(t, "_testdata/test.txt", l[9])
218218
pathEqualsTo(t, "_testdata/test.txt.gz", l[10])
219+
220+
l, err = testdata.ReadDirRecursiveFiltered(nil, FilterNames("folder"))
221+
require.NoError(t, err)
222+
l.Sort()
223+
require.Len(t, l, 1)
224+
pathEqualsTo(t, "_testdata/folder", l[0])
225+
226+
l, err = testdata.ReadDirRecursiveFiltered(FilterNames("symlinktofolder"), FilterOutNames(".hidden"))
227+
require.NoError(t, err)
228+
require.Len(t, l, 9)
229+
l.Sort()
230+
pathEqualsTo(t, "_testdata/anotherFile", l[0])
231+
pathEqualsTo(t, "_testdata/file", l[1])
232+
pathEqualsTo(t, "_testdata/folder", l[2])
233+
pathEqualsTo(t, "_testdata/symlinktofolder", l[3])
234+
pathEqualsTo(t, "_testdata/symlinktofolder/file2", l[4])
235+
pathEqualsTo(t, "_testdata/symlinktofolder/file3", l[5])
236+
pathEqualsTo(t, "_testdata/symlinktofolder/subfolder", l[6])
237+
pathEqualsTo(t, "_testdata/test.txt", l[7])
238+
pathEqualsTo(t, "_testdata/test.txt.gz", l[8])
219239
}

0 commit comments

Comments
 (0)