Skip to content

Commit

Permalink
🔥 up: file/finder - refactoring the find elem filter logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 3, 2023
1 parent b8b9075 commit ad445f3
Show file tree
Hide file tree
Showing 8 changed files with 1,088 additions and 512 deletions.
472 changes: 443 additions & 29 deletions fsutil/finder/config.go

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions fsutil/finder/elem.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package finder

import (
"io/fs"

"github.com/gookit/goutil/strutil"
)

// Elem of find file/dir result
Expand Down Expand Up @@ -40,3 +42,8 @@ func (e *elem) Info() (fs.FileInfo, error) {
}
return e.stat, e.sErr
}

// String get string representation
func (e *elem) String() string {
return strutil.OrCond(e.IsDir(), "dir: ", "file: ") + e.Path()
}
55 changes: 18 additions & 37 deletions fsutil/finder/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package finder

import (
"bytes"
"io/fs"

"github.com/gookit/goutil/fsutil"
)
Expand All @@ -21,38 +20,6 @@ func (fn FilterFunc) Apply(elem Elem) bool {
return fn(elem)
}

// ------------------ raw filter wrapper ------------------

// RawFilter for filter file path.
type RawFilter interface {
// Apply check file path. return False will filter this file.
Apply(fPath string, ent fs.DirEntry) bool
}

// RawFilterFunc for filter file info, return False will filter this file
type RawFilterFunc func(fPath string, ent fs.DirEntry) bool

// Apply check file path. return False will filter this file.
func (fn RawFilterFunc) Apply(fPath string, ent fs.DirEntry) bool {
return fn(fPath, ent)
}

// WrapRawFilter wrap a RawFilter to Filter
func WrapRawFilter(rf RawFilter) Filter {
return FilterFunc(func(elem Elem) bool {
return rf.Apply(elem.Path(), elem)
})
}

// WrapRawFilters wrap RawFilter list to Filter list
func WrapRawFilters(rfs ...RawFilter) []Filter {
fls := make([]Filter, len(rfs))
for i, rf := range rfs {
fls[i] = WrapRawFilter(rf)
}
return fls
}

// ------------------ Multi filter wrapper ------------------

// MultiFilter wrapper for multi filters
Expand Down Expand Up @@ -117,6 +84,20 @@ type BodyFilters struct {
}

// NewBodyFilters create a new body filters
//
// Usage:
//
// bf := finder.NewBodyFilters(
// finder.BodyFilterFunc(func(filePath string, buf *bytes.Buffer) bool {
// // filter file contents
// return true
// }),
// )
//
// es := finder.NewFinder('path/to/dir').WithFileFilter(bf).Elems()
// for el := range es {
// fmt.Println(el.Path())
// }
func NewBodyFilters(fls ...BodyFilter) *BodyFilters {
return &BodyFilters{
Filters: fls,
Expand All @@ -129,14 +110,14 @@ func (mf *BodyFilters) AddFilter(fls ...BodyFilter) {
}

// Apply check file path. return False will filter this file.
func (mf *BodyFilters) Apply(fPath string, ent fs.DirEntry) bool {
if ent.IsDir() {
func (mf *BodyFilters) Apply(el Elem) bool {
if el.IsDir() {
return false
}

// read file contents
buf := bytes.NewBuffer(nil)
file, err := fsutil.OpenReadFile(fPath)
file, err := fsutil.OpenReadFile(el.Path())
if err != nil {
return false
}
Expand All @@ -150,7 +131,7 @@ func (mf *BodyFilters) Apply(fPath string, ent fs.DirEntry) bool {

// apply filters
for _, fl := range mf.Filters {
if !fl.Apply(fPath, buf) {
if !fl.Apply(el.Path(), buf) {
return false
}
}
Expand Down
16 changes: 0 additions & 16 deletions fsutil/finder/filter_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1 @@
package finder_test

import (
"testing"

"github.com/gookit/goutil/fsutil/finder"
"github.com/gookit/goutil/testutil"
"github.com/gookit/goutil/testutil/assert"
)

func TestFilterFunc(t *testing.T) {
fn := finder.FilterFunc(func(el finder.Elem) bool {
return false
})

assert.False(t, fn(finder.NewElem("path/some.txt", &testutil.DirEnt{})))
}
Loading

0 comments on commit ad445f3

Please sign in to comment.