-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
127 lines (105 loc) · 2.35 KB
/
main.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
package main
import (
"bufio"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"sync"
"github.com/panjf2000/ants"
)
var pool, _ = ants.NewPool(1000000)
type Match = func(name string) bool
var match Match
var ignore Match
func initMatchers(payload string) {
payload = strings.TrimSuffix(payload, "\n")
patterns := strings.Split(payload, ",")
// Preprocess patterns to separate match and ignore patterns
matchPatterns := []string{}
ignorePatterns := []string{}
for _, pattern := range patterns {
if strings.HasPrefix(pattern, "!") {
ignorePatterns = append(ignorePatterns, strings.TrimPrefix(pattern, "!"))
} else {
matchPatterns = append(matchPatterns, pattern)
}
}
// Preprocess patterns to remove "**/" prefix
processedMatchPatterns := make([]string, len(matchPatterns))
processedIgnorePatterns := make([]string, len(ignorePatterns))
for i, pattern := range matchPatterns {
processedMatchPatterns[i] = strings.Replace(pattern, "**/", "", 1)
}
for i, pattern := range ignorePatterns {
processedIgnorePatterns[i] = strings.Replace(pattern, "**/", "", 1)
}
match = func(name string) bool {
for _, pattern := range processedMatchPatterns {
matched, err := filepath.Match(pattern, name)
if err != nil {
fail(err)
return false
}
if matched {
return true
}
}
return false
}
ignore = func(name string) bool {
for _, pattern := range processedIgnorePatterns {
matched, err := filepath.Match(pattern, name)
if err != nil {
fail(err)
return false
}
if matched {
return true
}
}
return false
}
}
func scanFolder(folder string, deep bool, wg *sync.WaitGroup) {
defer wg.Done()
entrys, err := os.ReadDir(folder)
if err != nil {
fail(err)
return
}
for _, entry := range entrys {
p := path.Join(folder, entry.Name())
if entry.IsDir() {
if deep && !ignore(p) {
wg.Add(1)
pool.Submit(func() { scanFolder(p, deep, wg) })
}
} else {
if !ignore(p) && match(p) {
success(p)
}
}
}
}
func success(path string) {
fmt.Println(path)
}
func fail(err error) {
fmt.Fprintln(os.Stderr, err)
}
func main() {
reader := bufio.NewReader(os.Stdin)
payload, err := reader.ReadString('\n')
if err != nil {
fail(err)
return
}
initMatchers(payload)
var wg sync.WaitGroup
wg.Add(1)
go scanFolder(".", strings.Contains(payload, "**/*"), &wg)
wg.Wait()
success("EOF")
}