Skip to content

Commit c1e6b48

Browse files
committed
refactor filters
1 parent 64147e0 commit c1e6b48

2 files changed

Lines changed: 75 additions & 26 deletions

File tree

find/filter.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package find
2+
3+
import (
4+
"github.com/ayoisaiah/f2/v2/internal/config"
5+
"github.com/ayoisaiah/f2/v2/internal/file"
6+
)
7+
8+
// Filter defines the interface for all file discovery filters.
9+
type Filter interface {
10+
// Skip returns true if the match should be excluded from renaming.
11+
Skip(conf *config.Config, match *file.Change) bool
12+
}
13+
14+
// Filters manages a collection of filters.
15+
type Filters []Filter
16+
17+
// Skip runs the match through all registered filters.
18+
// It returns true if any filter decides to skip the match.
19+
func (f Filters) Skip(conf *config.Config, match *file.Change) bool {
20+
for _, filter := range f {
21+
if filter.Skip(conf, match) {
22+
return true
23+
}
24+
}
25+
26+
return false
27+
}
28+
29+
// ExcludeFilter excludes files that match the exclusion regex.
30+
type ExcludeFilter struct{}
31+
32+
func (f ExcludeFilter) Skip(conf *config.Config, match *file.Change) bool {
33+
return conf.ExcludeRegex != nil &&
34+
conf.ExcludeRegex.MatchString(match.Source)
35+
}
36+
37+
// IncludeDirFilter excludes directories unless explicitly included.
38+
type IncludeDirFilter struct{}
39+
40+
func (f IncludeDirFilter) Skip(conf *config.Config, match *file.Change) bool {
41+
return !conf.IncludeDir && match.IsDir
42+
}
43+
44+
// OnlyDirFilter excludes non-directory files if OnlyDir is set.
45+
type OnlyDirFilter struct{}
46+
47+
func (f OnlyDirFilter) Skip(conf *config.Config, match *file.Change) bool {
48+
return conf.OnlyDir && !match.IsDir
49+
}
50+
51+
// IncludeRegexFilter excludes files that do not match the inclusion regex.
52+
type IncludeRegexFilter struct{}
53+
54+
func (f IncludeRegexFilter) Skip(conf *config.Config, match *file.Change) bool {
55+
return conf.IncludeRegex != nil &&
56+
!conf.IncludeRegex.MatchString(match.Source)
57+
}
58+
59+
// NewFilters creates a new collection of filters based on the configuration.
60+
func NewFilters(_ *config.Config) Filters {
61+
return Filters{
62+
ExcludeFilter{},
63+
IncludeDirFilter{},
64+
OnlyDirFilter{},
65+
IncludeRegexFilter{},
66+
}
67+
}

find/find.go

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,6 @@ const (
3131
dotCharacter = 46
3232
)
3333

34-
// shouldFilter decides whether a match should be included in the final
35-
// pool of files for renaming.
36-
func shouldFilter(conf *config.Config, match *file.Change) bool {
37-
if conf.ExcludeRegex != nil &&
38-
conf.ExcludeRegex.MatchString(match.Source) {
39-
return true
40-
}
41-
42-
if !conf.IncludeDir && match.IsDir {
43-
return true
44-
}
45-
46-
if conf.OnlyDir && !match.IsDir {
47-
return true
48-
}
49-
50-
if conf.IncludeRegex != nil &&
51-
!conf.IncludeRegex.MatchString(match.Source) {
52-
return true
53-
}
54-
55-
return false
56-
}
57-
5834
// skipFileIfHidden checks if a file is hidden, and if so, returns a boolean
5935
// confirming whether it should be skipped or not.
6036
func skipFileIfHidden(
@@ -214,6 +190,7 @@ func checkIfMatch(
214190
conf *config.Config,
215191
path string,
216192
entry fs.DirEntry,
193+
filters Filters,
217194
sortVars *variables.Variables,
218195
) (*file.Change, bool, error) {
219196
var err error
@@ -251,7 +228,7 @@ func checkIfMatch(
251228
slog.Any("match", match),
252229
)
253230

254-
if shouldFilter(conf, match) {
231+
if filters.Skip(conf, match) {
255232
slog.Debug(
256233
"excluding file based on filter criteria",
257234
slog.Any("match", match),
@@ -275,6 +252,7 @@ func walkDirectory(
275252
conf *config.Config,
276253
rootPath string,
277254
processedPaths map[string]bool,
255+
filters Filters,
278256
sortVars *variables.Variables,
279257
) (file.Changes, error) {
280258
var matches file.Changes
@@ -339,6 +317,7 @@ func walkDirectory(
339317
conf,
340318
currentPath,
341319
entry,
320+
filters,
342321
sortVars,
343322
)
344323
if err != nil {
@@ -369,6 +348,7 @@ func shouldSkipExcludedDir(conf *config.Config, entry fs.DirEntry) bool {
369348
func searchPaths(
370349
ctx context.Context,
371350
conf *config.Config,
351+
filters Filters,
372352
sortVars, searchVars *variables.Variables,
373353
) (file.Changes, error) {
374354
processedPaths := make(map[string]bool)
@@ -393,6 +373,7 @@ func searchPaths(
393373
conf,
394374
rootPath,
395375
fs.FileInfoToDirEntry(fileInfo),
376+
filters,
396377
sortVars,
397378
)
398379
if matchErr != nil {
@@ -413,6 +394,7 @@ func searchPaths(
413394
conf,
414395
rootPath,
415396
processedPaths,
397+
filters,
416398
sortVars,
417399
)
418400
if err != nil {
@@ -607,7 +589,7 @@ func Find(
607589
if conf.CSVFilename != "" {
608590
matches, err = handleCSV(conf)
609591
} else {
610-
matches, err = searchPaths(ctx, conf, &sortVars, &searchVars)
592+
matches, err = searchPaths(ctx, conf, NewFilters(conf), &sortVars, &searchVars)
611593
if err != nil {
612594
return nil, err
613595
}

0 commit comments

Comments
 (0)