Documentation ¶
Overview ¶
Package finder Provides a simple and convenient filedir lookup function, supports filtering, excluding, matching, ignoring, etc. and with some commonly built-in matchers.
Index ¶
- Variables
- type BodyMatcher
- type BodyMatcherFunc
- type BodyMatchers
- type Config
- type Elem
- type FileFinder
- type FindFlag
- type Finder
- func (f *Finder) Add(fls ...Matcher) *Finder
- func (f *Finder) AddFile(fls ...Matcher) *Finder
- func (f *Finder) AddFiles(fls []Matcher) *Finder
- func (f *Finder) AddScan(dirPaths ...string) *Finder
- func (f *Finder) AddScanDir(dirPaths ...string) *Finder
- func (f *Finder) AddScanDirs(dirPaths []string) *Finder
- func (f *Finder) Adds(fls []Matcher) *Finder
- func (f *Finder) CacheNum() int
- func (f *Finder) CacheResult(enable ...bool) *Finder
- func (f *Finder) Caches() []Elem
- func (f *Finder) Collect(fls ...Matcher) *Finder
- func (f *Finder) Config() Config
- func (f *Finder) ConfigFn(fns ...func(c *Config)) *Finder
- func (f *Finder) Each(fn func(el Elem))
- func (f *Finder) EachContents(fn func(contents, filePath string))
- func (f *Finder) EachElem(fn func(el Elem))
- func (f *Finder) EachFile(fn func(file *os.File))
- func (f *Finder) EachPath(fn func(filePath string))
- func (f *Finder) EachStat(fn func(fi os.FileInfo, filePath string))
- func (f *Finder) Elems() <-chan Elem
- func (f *Finder) Err() error
- func (f *Finder) Exclude(fls ...Matcher) *Finder
- func (f *Finder) ExcludeDir(dirs ...string) *Finder
- func (f *Finder) ExcludeDotDir(exclude ...bool) *Finder
- func (f *Finder) ExcludeDotFile(exclude ...bool) *Finder
- func (f *Finder) ExcludeExt(exts ...string) *Finder
- func (f *Finder) ExcludeFile(files ...string) *Finder
- func (f *Finder) ExcludeName(names ...string) *Finder
- func (f *Finder) ExcludePath(paths ...string) *Finder
- func (f *Finder) ExcludeRule(rules ...string) *Finder
- func (f *Finder) ExcludeRules(rules []string) *Finder
- func (f *Finder) Excludes(fls []Matcher) *Finder
- func (f *Finder) FileAndDir() *Finder
- func (f *Finder) Find() <-chan Elem
- func (f *Finder) FindNames() []string
- func (f *Finder) FindPaths() []string
- func (f *Finder) Include(fls ...Matcher) *Finder
- func (f *Finder) IncludeDir(dirs ...string) *Finder
- func (f *Finder) IncludeExt(exts ...string) *Finder
- func (f *Finder) IncludeFile(files ...string) *Finder
- func (f *Finder) IncludeName(names ...string) *Finder
- func (f *Finder) IncludePath(paths ...string) *Finder
- func (f *Finder) IncludeRule(rules ...string) *Finder
- func (f *Finder) IncludeRules(rules []string) *Finder
- func (f *Finder) Includes(fls []Matcher) *Finder
- func (f *Finder) MatchDir(fls ...Matcher) *Finder
- func (f *Finder) MatchDirs(fls []Matcher) *Finder
- func (f *Finder) MatchFile(fls ...Matcher) *Finder
- func (f *Finder) MatchFiles(fls []Matcher) *Finder
- func (f *Finder) NoDotDir(exclude ...bool) *Finder
- func (f *Finder) NoDotFile(exclude ...bool) *Finder
- func (f *Finder) Not(fls ...Matcher) *Finder
- func (f *Finder) NotDir(fls ...Matcher) *Finder
- func (f *Finder) NotDirs(fls []Matcher) *Finder
- func (f *Finder) NotFile(fls ...Matcher) *Finder
- func (f *Finder) NotFiles(fls []Matcher) *Finder
- func (f *Finder) Nots(fls []Matcher) *Finder
- func (f *Finder) Num() int
- func (f *Finder) OnlyFindDir() *Finder
- func (f *Finder) Reset()
- func (f *Finder) ResetResult()
- func (f *Finder) Results() <-chan Elem
- func (f *Finder) ScanDir(dirPaths ...string) *Finder
- func (f *Finder) String() string
- func (f *Finder) UseAbsPath(enable ...bool) *Finder
- func (f *Finder) With(fls ...Matcher) *Finder
- func (f *Finder) WithConfig(c *Config) *Finder
- func (f *Finder) WithConfigFn(fns ...func(c *Config)) *Finder
- func (f *Finder) WithDir(fls ...Matcher) *Finder
- func (f *Finder) WithDirName(dirs ...string) *Finder
- func (f *Finder) WithDirs(fls []Matcher) *Finder
- func (f *Finder) WithExts(exts []string) *Finder
- func (f *Finder) WithFileExt(exts ...string) *Finder
- func (f *Finder) WithFileName(files ...string) *Finder
- func (f *Finder) WithFilter(fls ...Matcher) *Finder
- func (f *Finder) WithFlags(flags FindFlag) *Finder
- func (f *Finder) WithMatchers(fls []Matcher) *Finder
- func (f *Finder) WithMaxDepth(i int) *Finder
- func (f *Finder) WithNames(names []string) *Finder
- func (f *Finder) WithPaths(paths []string) *Finder
- func (f *Finder) WithRules(addOrNot bool, rules []string) *Finder
- func (f *Finder) WithStrFlag(s string) *Finder
- func (f *Finder) WithSubPath(paths ...string) *Finder
- func (f *Finder) WithUseAbsPath(enable ...bool) *Finder
- func (f *Finder) Without(fls ...Matcher) *Finder
- func (f *Finder) WithoutDir(dirs ...string) *Finder
- func (f *Finder) WithoutDotDir(exclude ...bool) *Finder
- func (f *Finder) WithoutDotFile(exclude ...bool) *Finder
- func (f *Finder) WithoutExt(exts ...string) *Finder
- func (f *Finder) WithoutExts(exts []string) *Finder
- func (f *Finder) WithoutFile(files ...string) *Finder
- func (f *Finder) WithoutNames(names []string) *Finder
- func (f *Finder) WithoutPath(paths ...string) *Finder
- func (f *Finder) WithoutPaths(paths []string) *Finder
- type Matcher
- type MatcherFunc
- func FileSize(min, max uint64) MatcherFunc
- func GlobMatch(patterns ...string) MatcherFunc
- func GlobMatches(patterns []string) MatcherFunc
- func HumanModTime(expr string) MatcherFunc
- func HumanSize(expr string) MatcherFunc
- func MatchDotDir() MatcherFunc
- func MatchDotFile() MatcherFunc
- func MatchExt(exts ...string) MatcherFunc
- func MatchExts(exts []string) MatcherFunc
- func MatchModTime(start, end time.Time) MatcherFunc
- func MatchMtime(start, end time.Time) MatcherFunc
- func MatchName(names ...string) MatcherFunc
- func MatchNames(names []string) MatcherFunc
- func MatchPath(subPaths ...string) MatcherFunc
- func MatchPaths(subPaths []string) MatcherFunc
- func MatchPrefix(prefixes ...string) MatcherFunc
- func MatchPrefixes(prefixes []string) MatcherFunc
- func MatchSuffix(suffixes ...string) MatcherFunc
- func MatchSuffixes(suffixes []string) MatcherFunc
- func NameLike(patterns ...string) MatcherFunc
- func NameLikes(patterns []string) MatcherFunc
- func RegexMatch(pattern string) MatcherFunc
- func SizeRange(min, max uint64) MatcherFunc
- func StartWithDot() MatcherFunc
- type MultiMatcher
Constants ¶
This section is empty.
Variables ¶
var ( CommonlyDotDirs = []string{".git", ".idea", ".vscode", ".svn", ".hg"} CommonlyDotFiles = []string{".gitignore", ".dockerignore", ".npmignore", ".DS_Store", ".env"} )
commonly dot file and dirs
var MatchDir = MatcherFunc(func(el Elem) bool {
return el.IsDir()
})
MatchDir only allow dir path.
var MatchFile = MatcherFunc(func(el Elem) bool {
return !el.IsDir()
})
MatchFile only allow file path.
Functions ¶
This section is empty.
Types ¶
type BodyMatcher ¶ added in v0.6.10
BodyMatcher for match file contents.
type BodyMatcherFunc ¶ added in v0.6.9
BodyMatcherFunc for match file contents.
type BodyMatchers ¶ added in v0.6.10
type BodyMatchers struct {
Matchers []BodyMatcher
}
BodyMatchers multi body matchers as Matcher
func NewBodyMatchers ¶ added in v0.6.10
func NewBodyMatchers(fls ...BodyMatcher) *BodyMatchers
NewBodyMatchers create a new body matchers
Usage:
bf := finder.NewBodyMatchers( finder.BodyMatcherFunc(func(filePath string, buf *bytes.Buffer) bool { // match file contents return true }), ) es := finder.NewFinder('path/to/dir').Add(bf).Elems() for el := range es { fmt.Println(el.Path()) }
func (*BodyMatchers) AddMatcher ¶ added in v0.6.10
func (mf *BodyMatchers) AddMatcher(fls ...BodyMatcher)
AddMatcher add matchers
func (*BodyMatchers) Apply ¶ added in v0.6.10
func (mf *BodyMatchers) Apply(el Elem) bool
Apply check file contents is match.
type Config ¶ added in v0.6.9
type Config struct { // ScanDirs scan dir paths for find. ScanDirs []string `json:"scan_dirs"` // FindFlags for find result. default is FlagFile FindFlags FindFlag `json:"find_flags"` // MaxDepth for find result. default is 0 - not limit MaxDepth int `json:"max_depth"` // UseAbsPath use abs path for find result. default is false UseAbsPath bool `json:"use_abs_path"` // CacheResult cache result for find result. default is false CacheResult bool `json:"cache_result"` // ExcludeDotDir exclude dot dir. default is true ExcludeDotDir bool `json:"exclude_dot_dir"` // ExcludeDotFile exclude dot dir. default is false ExcludeDotFile bool `json:"exclude_dot_file"` // Matchers generic include matchers for file/dir elems Matchers []Matcher // ExMatchers generic exclude matchers for file/dir elems ExMatchers []Matcher // DirMatchers include matchers for dir elems DirMatchers []Matcher // DirExMatchers exclude matchers for dir elems DirExMatchers []Matcher // FileMatchers include matchers for file elems FileMatchers []Matcher // FileExMatchers exclude matchers for file elems FileExMatchers []Matcher // IncludeDirs include dir name list. eg: {"model"} IncludeDirs []string `json:"include_dirs"` // IncludeExts include file ext name list. eg: {".go", ".md"} IncludeExts []string `json:"include_exts"` // IncludeFiles include file name list. eg: {"go.mod"} IncludeFiles []string `json:"include_files"` // IncludePaths include file/dir path list. eg: {"path/to"} IncludePaths []string `json:"include_paths"` // IncludeNames include file/dir name list. eg: {"test", "some.go"} IncludeNames []string `json:"include_names"` // ExcludeDirs exclude dir name list. eg: {"test"} ExcludeDirs []string `json:"exclude_dirs"` // ExcludeExts exclude file ext name list. eg: {".go", ".md"} ExcludeExts []string `json:"exclude_exts"` // ExcludeFiles exclude file name list. eg: {"go.mod"} ExcludeFiles []string `json:"exclude_files"` // ExcludePaths exclude file/dir path list. eg: {"path/to"} ExcludePaths []string `json:"exclude_paths"` // ExcludeNames exclude file/dir name list. eg: {"test", "some.go"} ExcludeNames []string `json:"exclude_names"` // contains filtered or unexported fields }
Config for finder
func NewEmptyConfig ¶ added in v0.6.9
func NewEmptyConfig() *Config
NewEmptyConfig create a new Config
type Elem ¶ added in v0.6.9
type Elem interface { fs.DirEntry // Path get file/dir full path. eg: "/path/to/file.go" Path() string // Info get file info. like fs.DirEntry.Info(), but will cache result. Info() (fs.FileInfo, error) }
Elem of find file/dir path result
type FindFlag ¶ added in v0.6.9
type FindFlag uint8
FindFlag type for find result.
type Finder ¶ added in v0.6.9
type Finder struct {
// contains filtered or unexported fields
}
Finder struct
func EmptyFinder ¶
func EmptyFinder() *Finder
EmptyFinder new empty Finder instance. alias of NewEmpty()
func NewWithConfig ¶ added in v0.6.9
NewWithConfig new instance with config.
func (*Finder) AddScanDir ¶ added in v0.6.9
AddScanDir add source dir for find. alias of AddScanDirs()
func (*Finder) AddScanDirs ¶ added in v0.6.9
AddScanDirs add source dir for find
func (*Finder) CacheResult ¶ added in v0.6.9
CacheResult cache result for find result.
func (*Finder) EachContents ¶ added in v0.6.9
EachContents handle each found file contents
func (*Finder) ExcludeDir ¶ added in v0.6.9
ExcludeDir exclude dir names.
func (*Finder) ExcludeDotDir ¶ added in v0.6.9
ExcludeDotDir exclude dot dir names. eg: ".idea"
func (*Finder) ExcludeDotFile ¶ added in v0.6.9
ExcludeDotFile exclude dot dir names. eg: ".gitignore"
func (*Finder) ExcludeExt ¶ added in v0.6.9
ExcludeExt exclude file exts.
eg: ExcludeExt(".go", ".java")
func (*Finder) ExcludeFile ¶ added in v0.6.9
ExcludeFile exclude file names.
func (*Finder) ExcludeName ¶ added in v0.6.9
ExcludeName exclude file names. alias of WithoutNames()
func (*Finder) ExcludePath ¶ added in v0.6.9
ExcludePath exclude file paths.
func (*Finder) ExcludeRule ¶ added in v0.6.13
ExcludeRule exclude rules for finder
func (*Finder) ExcludeRules ¶ added in v0.6.13
ExcludeRules exclude rules for finder
func (*Finder) FileAndDir ¶ added in v0.6.9
FileAndDir both find file and dir.
func (*Finder) Find ¶ added in v0.6.9
Find files in given dir paths. will return a channel, you can use it to get the result.
Usage:
f := NewFinder("/path/to/dir") for el := range f.Find() { fmt.Println(el.Path()) }
func (*Finder) IncludeDir ¶ added in v0.6.9
IncludeDir include dir names.
func (*Finder) IncludeExt ¶ added in v0.6.9
IncludeExt include file exts.
func (*Finder) IncludeFile ¶ added in v0.6.9
IncludeFile include file names.
func (*Finder) IncludeName ¶ added in v0.6.9
IncludeName include file or dir names.
func (*Finder) IncludePath ¶ added in v0.6.9
IncludePath include file or dir paths.
func (*Finder) IncludeRule ¶ added in v0.6.13
IncludeRule include rules for finder
func (*Finder) IncludeRules ¶ added in v0.6.13
IncludeRules include rules for finder
func (*Finder) MatchFiles ¶ added in v0.6.9
MatchFiles add include file matchers
func (*Finder) NoDotFile ¶ added in v0.6.9
NoDotFile exclude dot dir names. alias of ExcludeDotFile().
func (*Finder) OnlyFindDir ¶ added in v0.6.9
OnlyFindDir only find dir.
func (*Finder) Reset ¶ added in v0.6.9
func (f *Finder) Reset()
Reset filters config setting and results info.
func (*Finder) ResetResult ¶ added in v0.6.9
func (f *Finder) ResetResult()
ResetResult reset result info.
func (*Finder) UseAbsPath ¶ added in v0.6.9
UseAbsPath use absolute path for find result. alias of WithUseAbsPath()
func (*Finder) WithConfig ¶ added in v0.6.9
WithConfig on the finder
func (*Finder) WithConfigFn ¶ added in v0.6.9
WithConfigFn the finder
func (*Finder) WithDirName ¶ added in v0.6.9
WithDirName include dir names. alias of IncludeDir()
func (*Finder) WithFileExt ¶ added in v0.6.9
WithFileExt include file exts. alias of IncludeExt()
func (*Finder) WithFileName ¶ added in v0.6.9
WithFileName include file names. alias of IncludeFile()
func (*Finder) WithFilter ¶ added in v0.6.9
WithFilter add include matchers
func (*Finder) WithMatchers ¶ added in v0.6.9
WithMatchers add include matchers
func (*Finder) WithMaxDepth ¶ added in v0.6.9
WithMaxDepth set max depth for find.
func (*Finder) WithNames ¶ added in v0.6.9
WithNames include file or dir names. alias of IncludeName()
func (*Finder) WithPaths ¶ added in v0.6.9
WithPaths include file or dir paths. alias of IncludePath()
func (*Finder) WithRules ¶ added in v0.6.13
WithRules on the finder
Rule Format:
NAME:pattern1,pattern2
Examples:
ext:.go,.yaml name:*_test.go,go.mod
func (*Finder) WithStrFlag ¶ added in v0.6.9
WithStrFlag set find flags by string.
func (*Finder) WithSubPath ¶ added in v0.6.9
WithSubPath include file or dir paths. alias of IncludePath()
func (*Finder) WithUseAbsPath ¶ added in v0.6.9
WithUseAbsPath use absolute path for find result.
func (*Finder) WithoutDir ¶ added in v0.6.9
WithoutDir exclude dir names. alias of ExcludeDir()
func (*Finder) WithoutDotDir ¶ added in v0.6.9
WithoutDotDir exclude dot dir names. alias of ExcludeDotDir().
func (*Finder) WithoutDotFile ¶ added in v0.6.9
WithoutDotFile exclude dot dir names. alias of ExcludeDotFile().
func (*Finder) WithoutExt ¶ added in v0.6.9
WithoutExt exclude file exts. alias of ExcludeExt()
func (*Finder) WithoutExts ¶ added in v0.6.9
WithoutExts exclude file exts. alias of ExcludeExt()
func (*Finder) WithoutFile ¶ added in v0.6.9
WithoutFile exclude file names. alias of ExcludeFile()
func (*Finder) WithoutNames ¶ added in v0.6.9
WithoutNames exclude file or dir names.
func (*Finder) WithoutPath ¶ added in v0.6.9
WithoutPath exclude file paths. alias of ExcludePath()
func (*Finder) WithoutPaths ¶ added in v0.6.9
WithoutPaths exclude file paths. alias of ExcludePath()
type Matcher ¶ added in v0.6.9
type Matcher interface { // Apply check find elem. return False will skip this file. Apply(elem Elem) bool }
Matcher for match file path.
type MatcherFunc ¶ added in v0.6.9
MatcherFunc for match file info, return False will skip this file
func FileSize ¶ added in v0.6.9
func FileSize(min, max uint64) MatcherFunc
FileSize match file by file size. unit: byte
func GlobMatch ¶ added in v0.6.9
func GlobMatch(patterns ...string) MatcherFunc
GlobMatch file/dir name by given patterns.
Usage:
f := NewFinder('path/to/dir') f.AddFilter(GlobMatch("*_test.go"))
func GlobMatches ¶ added in v0.6.9
func GlobMatches(patterns []string) MatcherFunc
GlobMatches file/dir name by given patterns.
func HumanModTime ¶ added in v0.6.9
func HumanModTime(expr string) MatcherFunc
HumanModTime filter file by modify time string.
Usage:
f := finder.NewFinder() f.Include(HumanModTime(">10m")) // before 10 minutes f.Include(HumanModTime("<10m")) // latest 10 minutes, to Now
func HumanSize ¶ added in v0.6.9
func HumanSize(expr string) MatcherFunc
HumanSize match file by file size string. eg: ">1k", "<2m", "1g~3g"
func MatchDotDir ¶ added in v0.6.9
func MatchDotDir() MatcherFunc
MatchDotDir match dot dirname. eg: ".idea"
func MatchDotFile ¶ added in v0.6.9
func MatchDotFile() MatcherFunc
MatchDotFile match dot filename. eg: ".idea"
func MatchExt ¶ added in v0.6.9
func MatchExt(exts ...string) MatcherFunc
MatchExt match filepath by given file ext.
Usage:
f := NewFinder('path/to/dir') f.Add(MatchExt(".go")) f.Not(MatchExt(".md"))
func MatchExts ¶ added in v0.6.9
func MatchExts(exts []string) MatcherFunc
MatchExts filter filepath by given file ext.
func MatchModTime ¶ added in v0.6.9
func MatchModTime(start, end time.Time) MatcherFunc
MatchModTime filter file by modify time.
func MatchMtime ¶ added in v0.6.9
func MatchMtime(start, end time.Time) MatcherFunc
MatchMtime match file by modify time.
Note: if time is zero, it will be ignored.
Usage:
f := NewFinder('path/to/dir') // -600 seconds to now(last 10 minutes) f.AddFile(MatchMtime(timex.NowAddSec(-600), timex.ZeroTime)) // before 600 seconds(before 10 minutes) f.AddFile(MatchMtime(timex.ZeroTime, timex.NowAddSec(-600)))
func MatchName ¶ added in v0.6.9
func MatchName(names ...string) MatcherFunc
MatchName match filepath by given names.
Usage:
f := NewFinder('path/to/dir') f.Not(MatchName("README.md", "*_test.go"))
func MatchNames ¶ added in v0.6.9
func MatchNames(names []string) MatcherFunc
MatchNames match filepath by given names or patterns.
Usage:
f.Not(MatchNames([]string{"README.md", "*_test.go"}))
func MatchPath ¶ added in v0.6.9
func MatchPath(subPaths ...string) MatcherFunc
MatchPath match file/dir by given sub paths.
Usage:
f := NewFinder('path/to/dir') f.Add(MatchPath("need/path"))
func MatchPaths ¶ added in v0.6.9
func MatchPaths(subPaths []string) MatcherFunc
MatchPaths match file/dir by given sub paths.
func MatchPrefix ¶ added in v0.6.9
func MatchPrefix(prefixes ...string) MatcherFunc
MatchPrefix match filepath by check given prefixes.
Usage:
f := NewFinder('path/to/dir') f.Add(finder.MatchPrefix("app_", "README"))
func MatchPrefixes ¶ added in v0.6.9
func MatchPrefixes(prefixes []string) MatcherFunc
MatchPrefixes match filepath by check given prefixes.
func MatchSuffix ¶ added in v0.6.9
func MatchSuffix(suffixes ...string) MatcherFunc
MatchSuffix match filepath by check path has suffixes.
Usage:
f := NewFinder('path/to/dir') f.Add(finder.MatchSuffix("util.go", "en.md")) f.Not(finder.MatchSuffix("_test.go", ".log"))
func MatchSuffixes ¶ added in v0.6.9
func MatchSuffixes(suffixes []string) MatcherFunc
MatchSuffixes match filepath by check path has suffixes.
func NameLike ¶ added in v0.6.9
func NameLike(patterns ...string) MatcherFunc
NameLike exclude filepath by given name match.
func NameLikes ¶ added in v0.6.9
func NameLikes(patterns []string) MatcherFunc
NameLikes filter filepath by given name match.
func RegexMatch ¶ added in v0.6.9
func RegexMatch(pattern string) MatcherFunc
RegexMatch match name by given regex pattern
Usage:
f := NewFinder('path/to/dir') f.AddFilter(RegexMatch(`[A-Z]\w+`))
func SizeRange ¶ added in v0.6.9
func SizeRange(min, max uint64) MatcherFunc
SizeRange match file by file size. unit: byte
func StartWithDot ¶ added in v0.6.9
func StartWithDot() MatcherFunc
StartWithDot match dot file/dir. eg: ".gitignore"
func (MatcherFunc) Apply ¶ added in v0.6.9
func (fn MatcherFunc) Apply(elem Elem) bool
Apply check file path. return False will skip this file.
type MultiMatcher ¶ added in v0.6.10
MultiMatcher wrapper for multi matchers
func NewDirMatchers ¶ added in v0.6.10
func NewDirMatchers(fls ...Matcher) *MultiMatcher
NewDirMatchers create a new dir matchers
func NewFileMatchers ¶ added in v0.6.10
func NewFileMatchers(fls ...Matcher) *MultiMatcher
NewFileMatchers create a new dir matchers
func (*MultiMatcher) Add ¶ added in v0.6.10
func (mf *MultiMatcher) Add(fls ...Matcher)
Add matchers
func (*MultiMatcher) Apply ¶ added in v0.6.10
func (mf *MultiMatcher) Apply(el Elem) bool
Apply check file path is match.