Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ feat: fsutil/finder - refactoring the find and filter logic
- Loading branch information
Showing
10 changed files
with
876 additions
and
449 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# finder | ||
|
||
[![GoDoc](https://godoc.org/github.com/goutil/fsutil/finder?status.svg)](https://godoc.org/github.com/goutil/fsutil/finder) | ||
|
||
`finder` provide a finder tool for find files, dirs. | ||
|
||
## Usage | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/gookit/goutil/dump" | ||
"github.com/goutil/fsutil/finder" | ||
) | ||
|
||
func main() { | ||
ff := finder.NewFinder() | ||
ff.AddPath("/tmp") | ||
ff.AddPath("/usr/local") | ||
ff.AddPath("/usr/local/bin") | ||
ff.AddPath("/usr/local/lib") | ||
ff.AddPath("/usr/local/libexec") | ||
ff.AddPath("/usr/local/sbin") | ||
ff.AddPath("/usr/local/share") | ||
|
||
ss := ff.FindPaths() | ||
dump.P(ss) | ||
} | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package finder | ||
|
||
// commonly dot file and dirs | ||
var ( | ||
CommonlyDotDir = []string{".git", ".idea", ".vscode", ".svn", ".hg"} | ||
CommonlyDotFile = []string{".gitignore", ".dockerignore", ".npmignore", ".DS_Store"} | ||
) | ||
|
||
// FindFlag type for find result. | ||
type FindFlag uint8 | ||
|
||
// flags for find result. | ||
const ( | ||
FlagFile FindFlag = iota + 1 // only find files(default) | ||
FlagDir | ||
) | ||
|
||
// Config for finder | ||
type Config struct { | ||
curDepth int | ||
|
||
// DirPaths src paths for find file. | ||
DirPaths []string | ||
// FindFlags for find result. default is FlagFile | ||
FindFlags FindFlag | ||
// MaxDepth for find result. default is 0 | ||
MaxDepth int | ||
// CacheResult cache result for find result. default is false | ||
CacheResult bool | ||
|
||
// IncludeDirs name list. eg: {"model"} | ||
IncludeDirs []string | ||
// IncludeExts name list. eg: {".go", ".md"} | ||
IncludeExts []string | ||
// IncludeFiles name list. eg: {"go.mod"} | ||
IncludeFiles []string | ||
// IncludePaths list. eg: {"path/to"} | ||
IncludePaths []string | ||
|
||
// ExcludeDirs name list. eg: {"test"} | ||
ExcludeDirs []string | ||
// ExcludeExts name list. eg: {".go", ".md"} | ||
ExcludeExts []string | ||
// ExcludeFiles name list. eg: {"go.mod"} | ||
ExcludeFiles []string | ||
// ExcludePaths list. eg: {"path/to"} | ||
ExcludePaths []string | ||
// ExcludeNames file/dir name list. eg: {"test", "some.go"} | ||
ExcludeNames []string | ||
|
||
ExcludeDotDir bool | ||
ExcludeDotFile bool | ||
|
||
// Filters generic filters for filter file/dir paths | ||
Filters []Filter | ||
|
||
DirFilters []Filter // filters for filter dir paths | ||
FileFilters []Filter // filters for filter file paths | ||
BodyFilters []BodyFilter // filters for filter file body | ||
} | ||
|
||
// NewConfig create a new Config | ||
func NewConfig(dirs ...string) *Config { | ||
return &Config{ | ||
DirPaths: dirs, | ||
FindFlags: FlagFile, | ||
// with default setting. | ||
ExcludeDotDir: true, | ||
} | ||
} | ||
|
||
// NewFinder create a new FileFinder by config | ||
func (c *Config) NewFinder() *FileFinder { | ||
return NewWithConfig(c) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package finder | ||
|
||
import ( | ||
"io/fs" | ||
) | ||
|
||
// Elem of find file/dir result | ||
type Elem interface { | ||
fs.DirEntry | ||
// Path get file/dir path. eg: "/path/to/file.go" | ||
Path() string | ||
// Info get file info. like fs.DirEntry.Info(), but will cache result. | ||
Info() (fs.FileInfo, error) | ||
} | ||
|
||
type elem struct { | ||
fs.DirEntry | ||
path string | ||
stat fs.FileInfo | ||
sErr error | ||
} | ||
|
||
// NewElem create a new Elem instance | ||
func NewElem(fPath string, ent fs.DirEntry) Elem { | ||
return &elem{ | ||
path: fPath, | ||
DirEntry: ent, | ||
} | ||
} | ||
|
||
// Path get full file/dir path. eg: "/path/to/file.go" | ||
func (e *elem) Path() string { | ||
return e.path | ||
} | ||
|
||
// Info get file info, will cache result | ||
func (e *elem) Info() (fs.FileInfo, error) { | ||
if e.stat == nil { | ||
e.stat, e.sErr = e.DirEntry.Info() | ||
} | ||
return e.stat, e.sErr | ||
} |
Oops, something went wrong.