-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaddy-watch.go
63 lines (53 loc) · 1.08 KB
/
caddy-watch.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
package main
import (
"fmt"
"gopkg.in/fsnotify.v1"
"log"
"os"
"path/filepath"
)
// Let's do this
// take in paths as CL Args
// walk the paths
func main() {
fmt.Println("caddy-hot-watcher connected")
done := make(chan bool)
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
go func() {
for {
select {
case event := <-watcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write {
fmt.Println(event.Name)
}
case err := <-watcher.Errors:
fmt.Println("error:", err)
}
}
}()
root := "./"
err = filepath.Walk(root, watchThis(*watcher, root))
if err != nil {
log.Fatal(err)
}
<-done
}
func watchThis(watcher fsnotify.Watcher, root string) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {
// skip these dirs
if path == "node_modules" ||
path == "jspm_packages" ||
path == ".git" {
return filepath.SkipDir
}
// recurse unless self
if info.IsDir() && path != root {
return filepath.Walk(path, watchThis(watcher, path))
}
return watcher.Add(path)
}
}