forked from jaytaylor/go-find
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind.go
93 lines (81 loc) · 1.7 KB
/
find.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package find
import (
"regexp"
)
type Find struct {
Paths []string
predicates predicates
}
func NewFind(paths ...string) *Find {
find := &Find{
Paths: paths,
}
return find
}
func (finder *Find) Evaluate() ([]string, error) {
results := []string{}
for _, path := range finder.Paths {
hits, err := finder.predicates.Evaluate(path)
if err != nil {
return nil, err
}
results = append(results, hits...)
}
return results, nil
}
func (finder *Find) MinDepth(n int) *Find {
c := &minDepthPredicate{
n: n,
}
finder.predicates = append(finder.predicates, c)
return finder
}
func (finder *Find) MaxDepth(n int) *Find {
c := &maxDepthPredicate{
n: n,
}
finder.predicates = append(finder.predicates, c)
return finder
}
func (finder *Find) Type(t string) *Find {
c := &typePredicate{
t: t,
}
finder.predicates = append(finder.predicates, c)
return finder
}
func (finder *Find) Name(pattern string) *Find {
c := &namePredicatae{
pattern: pattern,
}
finder.predicates = append(finder.predicates, c)
return finder
}
func (finder *Find) WholeName(pattern string) *Find {
c := &wholeNamePredicate{
pattern: pattern,
}
finder.predicates = append(finder.predicates, c)
return finder
}
func (finder *Find) Regex(expr *regexp.Regexp) *Find {
c := ®exPredicate{
expr: expr,
}
finder.predicates = append(finder.predicates, c)
return finder
}
func (finder *Find) Empty() *Find {
c := &emptyPredicate{}
finder.predicates = append(finder.predicates, c)
return finder
}
func (finder *Find) Mount() *Find {
fsType := getFileSystemType(finder.Paths...)
c := &mountPredicate{
path: finder.Paths,
fsType: fsType,
}
finder.predicates = append(finder.predicates, c)
return finder
}