-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.go
216 lines (176 loc) · 4.14 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Package find allows to search for files/folders with options.
package find
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)
var ErrTemplateType = errors.New("cannot define type of the template")
// Templater defines type constraint for generic Find function.
type Templater interface {
~string | ~[]string
}
// FindWithIterator acts the same way as [Find] but returns channels instead.
// String channel will return every match. Error channel returns first occured
// error during search or if [WithErrorsSkip] was set, first critical error.
// As soon as search is over or interrupted, both channels will be closed.
// For example:
//
// outCh, errCh := FindWithIterator(ctx, where, ts, opts...)
// for f := range outCh {
// // do something here...
// }
// if err := <-errCh {
// // process error...
// }
func FindWithIterator[T Templater](
ctx context.Context,
where string,
t T,
opts ...optFunc,
) (chan string, chan error) {
opt := defaultOptionsWithCustom(opts...)
opt.iterCh = make(chan string, opt.maxIter)
opt.errCh = make(chan error, 1)
opt.iter = true
go func() {
defer func() {
close(opt.iterCh)
close(opt.errCh)
}()
resPath, err := resolvePath(where)
if err != nil {
opt.errCh <- err
return
}
opt.orig = where
opt.resOrig = resPath
ts, err := newTemplates(t, opt.caseFunc)
if err != nil {
opt.errCh <- err
return
}
if _, err := find(ctx, resPath, ts, opt); err != nil {
opt.errCh <- err
}
}()
return opt.iterCh, opt.errCh
}
// Find searches for matches with the given templates in where.
func Find[T Templater](
ctx context.Context,
where string,
t T,
opts ...optFunc,
) ([]string, error) {
// Primary path resolution, even if `skip` flag was set,
// this error is critical and should not be omitted.
resPath, err := resolvePath(where)
if err != nil {
return nil, err
}
opt := defaultOptionsWithCustom(opts...)
// Pre-save location file and its resolved path, for further
// usage if relative paths will be needed.
opt.orig = where
opt.resOrig = resPath
ts, err := newTemplates(t, opt.caseFunc)
if err != nil {
return nil, err
}
return find(ctx, resPath, ts, opt)
}
func find(
ctx context.Context,
where string,
ts Templates,
opt *options,
) ([]string, error) {
resPath, data, err := readAndResolve(where)
if err != nil {
lErr := opt.logError(err)
return nil, lErr
}
res := make([]string, 0)
for _, f := range data {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
if opt.max == 0 {
return res, nil
}
p := filepath.Join(resPath, f.Name())
var found string
if opt.isSearchedType(f.IsDir()) && opt.match(ts, p) {
switch {
case opt.name:
found = f.Name()
case opt.relative:
found = strings.ReplaceAll(p, opt.resOrig, opt.orig)
default:
found = p
}
if err := opt.printOutput(found); err != nil {
return nil, err
}
if opt.iter {
opt.iterCh <- found
} else {
res = append(res, found)
}
if opt.max != -1 {
opt.max--
}
}
if opt.rec && f.IsDir() {
recData, err := find(ctx, p, ts, opt)
if err != nil {
return nil, err
}
res = append(res, recData...)
}
}
}
return res, nil
}
// resolvePath resolves symlinks and relative paths.
func resolvePath(p string) (string, error) {
info, err := os.Lstat(p)
if err != nil {
return "", err
}
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
if p, err = filepath.EvalSymlinks(p); err != nil {
return "", err
}
}
return filepath.Abs(p)
}
func readAndResolve(p string) (string, []os.DirEntry, error) {
resPath, err := resolvePath(p)
if err != nil {
return "", nil, err
}
data, err := os.ReadDir(resPath)
return resPath, data, err
}
func newTemplates[T Templater](t T, fn caseFunc) (Templates, error) {
var ts Templates
switch any(t).(type) {
case string:
ts = Templates{NewTemplate(fn(any(t).(string)))}
case []string:
sl := make([]string, 0, len(any(t).([]string)))
for _, str := range any(t).([]string) {
sl = append(sl, fn(str))
}
ts = NewTemplates(sl)
default:
return nil, fmt.Errorf("%w: %v", ErrTemplateType, t)
}
return ts, nil
}