-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
executable file
·127 lines (103 loc) · 2.79 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 (
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"time"
"github.com/fsnotify/fsnotify"
)
var watcher *fsnotify.Watcher
var (
container string
rootPath string
delay int
ignoreArg string
ignores []string
)
func init() {
flag.StringVar(&container, "container", "", "Name of the container instance that you wish to notify of filesystem changes")
flag.StringVar(&rootPath, "path", "", "Root path where to watch for changes")
flag.IntVar(&delay, "delay", 100, "Delay in milliseconds before notifying about a file that's changed")
flag.StringVar(&ignoreArg, "ignore", "node_modules;vendor", "Semicolon-separated list of directories to ignore. "+
"Glob expressions are supported.")
}
func main() {
flag.Parse()
ignores = strings.Split(ignoreArg, ";")
watcher, _ = fsnotify.NewWatcher()
defer watcher.Close()
if rootPath == "" {
rootPath = "."
}
if err := filepath.Walk(rootPath, watchDir); err != nil {
fmt.Println("ERROR", err)
}
// Map of filenames we're currently notifying about.
var processes sync.Map
for {
select {
case event := <-watcher.Events:
switch event.Op {
case fsnotify.Write:
if _, ok := processes.Load(event.Name); ok {
continue
}
processes.Store(event.Name, nil)
go func(event fsnotify.Event) {
defer processes.Delete(event.Name)
// Wait for further events to accomodate the way editors
// save files.
time.Sleep(time.Duration(delay) * time.Millisecond)
// Ensure the file hasn't been renamed or removed.
if _, ok := processes.Load(event.Name); !ok {
return
}
notifyDocker(event)
}(event)
case fsnotify.Rename, fsnotify.Remove:
processes.Delete(event.Name)
}
case err := <-watcher.Errors:
fmt.Println("Error: ", err)
}
}
}
func notifyDocker(event fsnotify.Event) {
if event.Op != fsnotify.Write {
return
}
file := filepath.ToSlash(event.Name)
containerPath := strings.TrimPrefix(file, rootPath)
if strings.HasPrefix(containerPath, "/") {
containerPath = strings.TrimPrefix(containerPath, "/")
}
fmt.Println("Updating container file", containerPath)
_, err := exec.Command("docker", "exec", container, "/bin/sh", "-c", fmt.Sprintf("chmod $(stat -c %%a %s) %s", containerPath, containerPath)).Output()
if err != nil {
fmt.Printf("Error notifying container about file change: %v\n", err)
}
}
func watchDir(path string, fi os.FileInfo, err error) error {
if !fi.Mode().IsDir() {
return nil
}
// Ignore hidden directories.
if len(path) > 1 && strings.HasPrefix(path, ".") {
return filepath.SkipDir
}
for _, pattern := range ignores {
ok, err := filepath.Match(pattern, fi.Name())
if err != nil {
return err
}
if ok {
return filepath.SkipDir
}
}
fmt.Println("Watching ", path)
return watcher.Add(path)
}