-
Notifications
You must be signed in to change notification settings - Fork 92
/
walk.go
180 lines (153 loc) · 3.99 KB
/
walk.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
package pathlib
import (
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"time"
)
type Forced func(os.FileInfo) bool
type Ignore func(os.FileInfo) bool
type Report func(string, string, os.FileInfo)
type IgnoreOlder time.Time
func (it IgnoreOlder) Ignore(candidate os.FileInfo) bool {
return candidate.ModTime().Before(time.Time(it))
}
type IgnoreNewer time.Time
func (it IgnoreNewer) Ignore(candidate os.FileInfo) bool {
return candidate.ModTime().After(time.Time(it))
}
func IgnoreNothing(_ os.FileInfo) bool {
return false
}
func IgnoreDirectories(target os.FileInfo) bool {
return target.IsDir()
}
func ForceNothing(_ os.FileInfo) bool {
return false
}
func ForceFilename(filename string) Forced {
return func(target os.FileInfo) bool {
return !target.IsDir() && target.Name() == filename
}
}
func NoReporting(string, string, os.FileInfo) {
}
func sorted(files []os.FileInfo) {
sort.SliceStable(files, func(left, right int) bool {
return files[left].Name() < files[right].Name()
})
}
type composite []Ignore
func (it composite) Ignore(file os.FileInfo) bool {
for _, ignore := range it {
if ignore(file) {
return true
}
}
return false
}
type exactIgnore string
func (it exactIgnore) Ignore(file os.FileInfo) bool {
return file.Name() == string(it)
}
type globIgnore string
func (it globIgnore) Ignore(file os.FileInfo) bool {
name := file.Name()
result, err := filepath.Match(string(it), name)
if err == nil && result {
return true
}
if file.IsDir() {
result, err = filepath.Match(string(it), name+"/")
return err == nil && result
}
return false
}
func CompositeIgnore(ignores ...Ignore) Ignore {
return composite(ignores).Ignore
}
func IgnorePattern(text string) Ignore {
return CompositeIgnore(exactIgnore(text).Ignore, globIgnore(text).Ignore)
}
func LoadIgnoreFile(filename string) (Ignore, error) {
content, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
result := make([]Ignore, 0, 10)
for _, line := range strings.SplitAfter(string(content), "\n") {
line = strings.TrimSpace(line)
if len(line) == 0 || strings.HasPrefix(line, "#") {
continue
}
result = append(result, IgnorePattern(line))
}
return CompositeIgnore(result...), nil
}
func LoadIgnoreFiles(filenames []string) (Ignore, error) {
result := make([]Ignore, 0, len(filenames))
for _, filename := range filenames {
ignore, err := LoadIgnoreFile(filename)
if err != nil {
return nil, err
}
result = append(result, ignore)
}
return CompositeIgnore(result...), nil
}
func folderEntries(directory string) ([]os.FileInfo, error) {
handle, err := os.Open(directory)
if err != nil {
return nil, err
}
defer handle.Close()
entries, err := handle.Readdir(-1)
if err != nil {
return nil, err
}
return entries, nil
}
func recursiveWalk(directory, prefix string, force Forced, ignore Ignore, report Report) error {
entries, err := folderEntries(directory)
if err != nil {
return err
}
sorted(entries)
for _, entry := range entries {
if !force(entry) && ignore(entry) {
continue
}
nextPrefix := filepath.Join(prefix, entry.Name())
entryPath := filepath.Join(directory, entry.Name())
if entry.IsDir() {
recursiveWalk(entryPath, nextPrefix, force, ignore, report)
} else {
report(entryPath, nextPrefix, entry)
}
}
return nil
}
func ForceWalk(directory string, force Forced, ignore Ignore, report Report) error {
fullpath, err := filepath.Abs(directory)
if err != nil {
return err
}
return recursiveWalk(fullpath, ".", force, ignore, report)
}
func Walk(directory string, ignore Ignore, report Report) error {
return ForceWalk(directory, ForceNothing, ignore, report)
}
func Glob(directory string, pattern string) []string {
result := []string{}
ignore := func(entry os.FileInfo) bool {
match, err := filepath.Match(pattern, entry.Name())
return err != nil || !entry.IsDir() && !match
}
capture := func(_, localpath string, _ os.FileInfo) {
result = append(result, localpath)
}
ForceWalk(directory, ForceNothing, ignore, capture)
return result
}