Skip to content

Commit

Permalink
✨ feat: fs/finder - support quick config finder by string rules
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 1, 2023
1 parent db5b927 commit da08abc
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 3 deletions.
133 changes: 132 additions & 1 deletion fsutil/finder/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package finder

import "strings"
import (
"fmt"
"strings"

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

// commonly dot file and dirs
var (
Expand Down Expand Up @@ -103,6 +109,88 @@ func NewEmptyConfig() *Config {
return &Config{FindFlags: FlagFile}
}

// LoadRules load rules and parse to config
//
// Rule Format:
//
// NAME:pattern1,pattern2
//
// Examples:
//
// ext:.go,.yaml
// name:*_test.go,go.mod
func (c *Config) LoadRules(addOrNot bool, rules []string) error {
es := errorx.Errors{}
for i, rule := range rules {
if !strings.Contains(rule, ":") {
es = append(es, fmt.Errorf("invalid rule#%d: %s", i, rule))
break
}

name, pattern := strutil.TrimCut(rule, ":")
if name == "" || pattern == "" {
es = append(es, fmt.Errorf("invalid rule#%d: %s", i, rule))
break
}

patterns := strutil.Split(pattern, ",")
switch name {
case "ext", "exts":
if addOrNot {
c.IncludeExts = append(c.IncludeExts, patterns...)
} else {
c.ExcludeExts = append(c.ExcludeExts, patterns...)
}
case "name", "names":
if addOrNot {
c.IncludeNames = append(c.IncludeNames, patterns...)
} else {
c.ExcludeNames = append(c.ExcludeNames, patterns...)
}
case "file", "files":
if addOrNot {
c.IncludeFiles = append(c.IncludeFiles, patterns...)
} else {
c.ExcludeFiles = append(c.ExcludeFiles, patterns...)
}
case "path", "paths":
if addOrNot {
c.IncludePaths = append(c.IncludePaths, patterns...)
} else {
c.ExcludePaths = append(c.ExcludePaths, patterns...)
}
case "dir", "dirs":
if addOrNot {
c.IncludeDirs = append(c.IncludeDirs, patterns...)
} else {
c.ExcludeDirs = append(c.ExcludeDirs, patterns...)
}
case "size": // size:>=1M,<=10M
if addOrNot {
for _, expr := range patterns {
c.FileMatchers = append(c.FileMatchers, HumanSize(expr))
}
} else {
for _, expr := range patterns {
c.FileExMatchers = append(c.FileExMatchers, HumanSize(expr))
}
}
case "time", "mtime": // mtime:>=1d,<=10d
if addOrNot {
for _, expr := range patterns {
c.FileMatchers = append(c.FileMatchers, HumanModTime(expr))
}
} else {
for _, expr := range patterns {
c.FileExMatchers = append(c.FileExMatchers, HumanModTime(expr))
}
}
}
}

return es.ErrorOrNil()
}

// NewFinder create a new Finder by config
func (c *Config) NewFinder() *Finder {
return NewWithConfig(c.Init())
Expand Down Expand Up @@ -160,6 +248,49 @@ func (c *Config) Init() *Config {
return c
}

//
// --------- config finder by rules ---------
//

// IncludeRule include rules for finder
func (f *Finder) IncludeRule(rules ...string) *Finder {
f.err = f.c.LoadRules(true, rules)
return f
}

// IncludeRules include rules for finder
func (f *Finder) IncludeRules(rules []string) *Finder {
f.err = f.c.LoadRules(true, rules)
return f
}

// ExcludeRule exclude rules for finder
func (f *Finder) ExcludeRule(rules ...string) *Finder {
f.err = f.c.LoadRules(false, rules)
return f
}

// ExcludeRules exclude rules for finder
func (f *Finder) ExcludeRules(rules []string) *Finder {
f.err = f.c.LoadRules(false, rules)
return f
}

// WithRules on the finder
//
// Rule Format:
//
// NAME:pattern1,pattern2
//
// Examples:
//
// ext:.go,.yaml
// name:*_test.go,go.mod
func (f *Finder) WithRules(addOrNot bool, rules []string) *Finder {
f.err = f.c.LoadRules(addOrNot, rules)
return f
}

//
// --------- config for finder ---------
//
Expand Down
4 changes: 2 additions & 2 deletions fsutil/opwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func MustSave(filePath string, data any, optFns ...OpenOptionFunc) {
basefn.MustOK(SaveFile(filePath, data, optFns...))
}

// SaveFile create file and write contents to file.
// SaveFile create file and write contents to file. will auto create dir.
//
// default option see NewOpenOption()
func SaveFile(filePath string, data any, optFns ...OpenOptionFunc) error {
Expand All @@ -73,7 +73,7 @@ func SaveFile(filePath string, data any, optFns ...OpenOptionFunc) error {

// PutContents create file and write contents to file at once.
//
// data type allow: string, []byte, io.Reader
// data type allow: string, []byte, io.Reader. will auto create dir.
//
// Tip: file flag default is FsCWTFlags (override write)
//
Expand Down

0 comments on commit da08abc

Please sign in to comment.