-
Notifications
You must be signed in to change notification settings - Fork 10
/
utils.go
134 lines (118 loc) · 2.41 KB
/
utils.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
package runner
import (
"github.com/fsnotify/fsnotify"
"log"
"os"
"path/filepath"
"sort"
"time"
)
func watch(path string, abort <-chan struct{}) (<-chan string, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
for p := range list(path) {
err = watcher.Add(p)
if err != nil {
log.Printf("Failed to watch: %s, error: %s", p, err)
}
}
out := make(chan string)
go func() {
defer close(out)
defer watcher.Close()
for {
select {
case <-abort:
// Abort watching
err := watcher.Close()
if err != nil {
log.Fatalln("Failed to stop watch")
}
return
case fp := <-watcher.Events:
if fp.Op == fsnotify.Create {
info, err := os.Stat(fp.Name)
if err == nil && info.IsDir() {
// Add newly created sub directories to watch list
watcher.Add(fp.Name)
}
}
out <- fp.Name
case err := <-watcher.Errors:
log.Println("Watch Error:", err)
}
}
}()
return out, nil
}
func match(in <-chan string, patterns []string) <-chan string {
out := make(chan string)
go func() {
defer close(out)
for fp := range in {
info, err := os.Stat(fp)
if os.IsNotExist(err) || !info.IsDir() {
_, fn := filepath.Split(fp)
for _, p := range patterns {
if ok, _ := filepath.Match(p, fn); ok {
out <- fp
}
}
}
}
}()
return out
}
func list(root string) <-chan string {
out := make(chan string)
info, err := os.Stat(root)
if err != nil {
log.Fatalf("Failed to visit %s, error: %s\n", root, err)
}
if !info.IsDir() {
go func() {
defer close(out)
out <- root
}()
return out
}
go func() {
defer close(out)
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
if err != nil {
log.Printf("Failed to visit directory: %s, error: %s", path, err)
return err
}
out <- path
}
return nil
})
}()
return out
}
// gather delays further operations for a while and gather
// all changes happened in this period
func gather(first string, changes <-chan string, delay time.Duration) []string {
files := make(map[string]bool)
files[first] = true
after := time.After(delay)
loop:
for {
select {
case fp := <-changes:
files[fp] = true
case <-after:
// After the delay, return collected filenames
break loop
}
}
ret := []string{}
for k := range files {
ret = append(ret, k)
}
sort.Strings(ret)
return ret
}