Skip to content

Add filters FilterName and FilterOutName / Fixed linter warnings #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,7 @@ func (p *PathList) Add(path *Path) {

// AddAll adds all Paths in the list passed as argument
func (p *PathList) AddAll(paths PathList) {
for _, path := range paths {
*p = append(*p, path)
}
*p = append(*p, paths...)
}

// AddIfMissing adds a Path to the PathList if the path is not already
Expand Down
34 changes: 29 additions & 5 deletions readdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ func (p *Path) ReadDirRecursive() (PathList, error) {
// ReadDirRecursiveFiltered returns a PathList containing the content of the directory
// and its subdirectories pointed by the current Path, filtered by the given skipFilter
// and filters:
// - `recursionFilter` is a filter that is checked to determine if the subdirectory must
// by visited recursively (if the filter rejects the entry, the entry is not visited
// but can still be added to the result)
// - `filters` are the filters that are checked to determine if the entry should be
// added to the resulting PathList
// - `recursionFilter` is a filter that is checked to determine if the subdirectory must
// by visited recursively (if the filter rejects the entry, the entry is not visited
// but can still be added to the result)
// - `filters` are the filters that are checked to determine if the entry should be
// added to the resulting PathList
func (p *Path) ReadDirRecursiveFiltered(recursionFilter ReadDirFilter, filters ...ReadDirFilter) (PathList, error) {
infos, err := ioutil.ReadDir(p.path)
if err != nil {
Expand Down Expand Up @@ -152,6 +152,30 @@ func FilterOutDirectories() ReadDirFilter {
}
}

// FilterNames is a ReadDirFilter that accepts only the given filenames
func FilterNames(allowedNames ...string) ReadDirFilter {
return func(file *Path) bool {
for _, name := range allowedNames {
if file.Base() == name {
return true
}
}
return false
}
}

// FilterOutNames is a ReadDirFilter that rejects the given filenames
func FilterOutNames(rejectedNames ...string) ReadDirFilter {
return func(file *Path) bool {
for _, name := range rejectedNames {
if file.Base() == name {
return false
}
}
return true
}
}

// FilterSuffixes creates a ReadDirFilter that accepts only the given
// filename suffixes
func FilterSuffixes(allowedSuffixes ...string) ReadDirFilter {
Expand Down
28 changes: 28 additions & 0 deletions readdir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func TestReadDirRecursiveFiltered(t *testing.T) {
l, err := testdata.ReadDirRecursiveFiltered(nil)
require.NoError(t, err)
l.Sort()
require.Len(t, l, 16)
pathEqualsTo(t, "_testdata/anotherFile", l[0])
pathEqualsTo(t, "_testdata/file", l[1])
pathEqualsTo(t, "_testdata/folder", l[2])
Expand All @@ -130,6 +131,7 @@ func TestReadDirRecursiveFiltered(t *testing.T) {
l, err = testdata.ReadDirRecursiveFiltered(FilterOutDirectories())
require.NoError(t, err)
l.Sort()
require.Len(t, l, 6)
pathEqualsTo(t, "_testdata/anotherFile", l[0])
pathEqualsTo(t, "_testdata/file", l[1])
pathEqualsTo(t, "_testdata/folder", l[2]) // <- this is listed but not traversed
Expand All @@ -140,6 +142,7 @@ func TestReadDirRecursiveFiltered(t *testing.T) {
l, err = testdata.ReadDirRecursiveFiltered(nil, FilterOutDirectories())
require.NoError(t, err)
l.Sort()
require.Len(t, l, 12)
pathEqualsTo(t, "_testdata/anotherFile", l[0])
pathEqualsTo(t, "_testdata/file", l[1])
pathEqualsTo(t, "_testdata/folder/.hidden", l[2])
Expand All @@ -156,6 +159,7 @@ func TestReadDirRecursiveFiltered(t *testing.T) {
l, err = testdata.ReadDirRecursiveFiltered(FilterOutDirectories(), FilterOutDirectories())
require.NoError(t, err)
l.Sort()
require.Len(t, l, 4)
pathEqualsTo(t, "_testdata/anotherFile", l[0])
pathEqualsTo(t, "_testdata/file", l[1])
pathEqualsTo(t, "_testdata/test.txt", l[2])
Expand All @@ -164,6 +168,7 @@ func TestReadDirRecursiveFiltered(t *testing.T) {
l, err = testdata.ReadDirRecursiveFiltered(FilterOutPrefixes("sub"), FilterOutSuffixes("3"))
require.NoError(t, err)
l.Sort()
require.Len(t, l, 12)
pathEqualsTo(t, "_testdata/anotherFile", l[0])
pathEqualsTo(t, "_testdata/file", l[1])
pathEqualsTo(t, "_testdata/folder", l[2])
Expand All @@ -180,6 +185,7 @@ func TestReadDirRecursiveFiltered(t *testing.T) {
l, err = testdata.ReadDirRecursiveFiltered(FilterOutPrefixes("sub"), AndFilter(FilterOutSuffixes("3"), FilterOutPrefixes("fil")))
require.NoError(t, err)
l.Sort()
require.Len(t, l, 9)
pathEqualsTo(t, "_testdata/anotherFile", l[0])
pathEqualsTo(t, "_testdata/folder", l[1])
pathEqualsTo(t, "_testdata/folder/.hidden", l[2])
Expand All @@ -193,6 +199,7 @@ func TestReadDirRecursiveFiltered(t *testing.T) {
l, err = testdata.ReadDirRecursiveFiltered(FilterOutPrefixes("sub"), AndFilter(FilterOutSuffixes("3"), FilterOutPrefixes("fil"), FilterOutSuffixes(".gz")))
require.NoError(t, err)
l.Sort()
require.Len(t, l, 8)
pathEqualsTo(t, "_testdata/anotherFile", l[0])
pathEqualsTo(t, "_testdata/folder", l[1])
pathEqualsTo(t, "_testdata/folder/.hidden", l[2])
Expand All @@ -205,6 +212,7 @@ func TestReadDirRecursiveFiltered(t *testing.T) {
l, err = testdata.ReadDirRecursiveFiltered(OrFilter(FilterPrefixes("sub"), FilterSuffixes("tofolder")))
require.NoError(t, err)
l.Sort()
require.Len(t, l, 11)
pathEqualsTo(t, "_testdata/anotherFile", l[0])
pathEqualsTo(t, "_testdata/file", l[1])
pathEqualsTo(t, "_testdata/folder", l[2])
Expand All @@ -216,4 +224,24 @@ func TestReadDirRecursiveFiltered(t *testing.T) {
pathEqualsTo(t, "_testdata/symlinktofolder/subfolder/file4", l[8])
pathEqualsTo(t, "_testdata/test.txt", l[9])
pathEqualsTo(t, "_testdata/test.txt.gz", l[10])

l, err = testdata.ReadDirRecursiveFiltered(nil, FilterNames("folder"))
require.NoError(t, err)
l.Sort()
require.Len(t, l, 1)
pathEqualsTo(t, "_testdata/folder", l[0])

l, err = testdata.ReadDirRecursiveFiltered(FilterNames("symlinktofolder"), FilterOutNames(".hidden"))
require.NoError(t, err)
require.Len(t, l, 9)
l.Sort()
pathEqualsTo(t, "_testdata/anotherFile", l[0])
pathEqualsTo(t, "_testdata/file", l[1])
pathEqualsTo(t, "_testdata/folder", l[2])
pathEqualsTo(t, "_testdata/symlinktofolder", l[3])
pathEqualsTo(t, "_testdata/symlinktofolder/file2", l[4])
pathEqualsTo(t, "_testdata/symlinktofolder/file3", l[5])
pathEqualsTo(t, "_testdata/symlinktofolder/subfolder", l[6])
pathEqualsTo(t, "_testdata/test.txt", l[7])
pathEqualsTo(t, "_testdata/test.txt.gz", l[8])
}