-
Notifications
You must be signed in to change notification settings - Fork 0
/
finder.go
165 lines (132 loc) · 3.89 KB
/
finder.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
// Package finder looks for files and directories in an {fs.Fs} filesystem.
package locafero
import (
"errors"
"io/fs"
"path/filepath"
"strings"
"github.com/sourcegraph/conc/iter"
"github.com/spf13/afero"
)
// Finder looks for files and directories in an [afero.Fs] filesystem.
type Finder struct {
// Paths represents a list of locations that the [Finder] will search in.
//
// They are essentially the root directories or starting points for the search.
//
// Examples:
// - home/user
// - etc
Paths []string
// Names are specific entries that the [Finder] will look for within the given Paths.
//
// It provides the capability to search for entries with depth,
// meaning it can target deeper locations within the directory structure.
//
// It also supports glob syntax (as defined by [filepath.Match]), offering greater flexibility in search patterns.
//
// Examples:
// - config.yaml
// - home/*/config.yaml
// - home/*/config.*
Names []string
// Type restricts the kind of entries returned by the [Finder].
//
// This parameter helps in differentiating and filtering out files from directories or vice versa.
Type FileType
}
// Find looks for files and directories in an [afero.Fs] filesystem.
func (f Finder) Find(fsys afero.Fs) ([]string, error) {
// Arbitrary go routine limit (TODO: make this a parameter)
// pool := pool.NewWithResults[[]string]().WithMaxGoroutines(5).WithErrors().WithFirstError()
type searchItem struct {
path string
name string
}
var searchItems []searchItem
for _, searchPath := range f.Paths {
searchPath := searchPath
for _, searchName := range f.Names {
searchName := searchName
searchItems = append(searchItems, searchItem{searchPath, searchName})
// pool.Go(func() ([]string, error) {
// // If the name contains any glob character, perform a glob match
// if strings.ContainsAny(searchName, globMatch) {
// return globWalkSearch(fsys, searchPath, searchName, f.Type)
// }
//
// return statSearch(fsys, searchPath, searchName, f.Type)
// })
}
}
// allResults, err := pool.Wait()
// if err != nil {
// return nil, err
// }
allResults, err := iter.MapErr(searchItems, func(item *searchItem) ([]string, error) {
// If the name contains any glob character, perform a glob match
if strings.ContainsAny(item.name, globMatch) {
return globWalkSearch(fsys, item.path, item.name, f.Type)
}
return statSearch(fsys, item.path, item.name, f.Type)
})
if err != nil {
return nil, err
}
var results []string
for _, r := range allResults {
results = append(results, r...)
}
// Sort results in alphabetical order for now
// sort.Strings(results)
return results, nil
}
func globWalkSearch(fsys afero.Fs, searchPath string, searchName string, searchType FileType) ([]string, error) {
var results []string
err := afero.Walk(fsys, searchPath, func(p string, fileInfo fs.FileInfo, err error) error {
if err != nil {
return err
}
// Skip the root path
if p == searchPath {
return nil
}
var result error
// Stop reading subdirectories
// TODO: add depth detection here
if fileInfo.IsDir() && filepath.Dir(p) == searchPath {
result = fs.SkipDir
}
// Skip unmatching type
if !searchType.matchFileInfo(fileInfo) {
return result
}
match, err := filepath.Match(searchName, fileInfo.Name())
if err != nil {
return err
}
if match {
results = append(results, p)
}
return result
})
if err != nil {
return results, err
}
return results, nil
}
func statSearch(fsys afero.Fs, searchPath string, searchName string, searchType FileType) ([]string, error) {
filePath := filepath.Join(searchPath, searchName)
fileInfo, err := fsys.Stat(filePath)
if errors.Is(err, fs.ErrNotExist) {
return nil, nil
}
if err != nil {
return nil, err
}
// Skip unmatching type
if !searchType.matchFileInfo(fileInfo) {
return nil, nil
}
return []string{filePath}, nil
}