Skip to content

Commit

Permalink
♻️ feat: fsutil/finder - refactoring the find and filter logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed May 25, 2023
1 parent 7bfa5f4 commit 359d163
Show file tree
Hide file tree
Showing 10 changed files with 876 additions and 449 deletions.
31 changes: 31 additions & 0 deletions fsutil/finder/README.md
@@ -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)
}
```

75 changes: 75 additions & 0 deletions fsutil/finder/config.go
@@ -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)
}
42 changes: 42 additions & 0 deletions fsutil/finder/elem.go
@@ -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
}

0 comments on commit 359d163

Please sign in to comment.