-
Notifications
You must be signed in to change notification settings - Fork 60
/
fswatch.go
624 lines (569 loc) · 14.2 KB
/
fswatch.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
package main
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"syscall"
"time"
ignore "github.com/codeskyblue/dockerignore"
"github.com/codeskyblue/kexec"
"github.com/frioux/leatherman/pkg/shellquote"
"github.com/fsnotify/fsnotify"
"github.com/gobuild/log"
"github.com/google/shlex"
"gopkg.in/yaml.v2"
)
const (
FWCONFIG_YAML = "fsw.yml"
FWCONFIG_JSON = "fsw.json"
)
var (
VERSION = "3.2"
)
var signalMaps = map[string]os.Signal{
"INT": syscall.SIGINT,
"HUP": syscall.SIGHUP,
"QUIT": syscall.SIGQUIT,
"TRAP": syscall.SIGTRAP,
"TERM": syscall.SIGTERM,
"KILL": syscall.SIGKILL, // kill -9
}
func init() {
for key, val := range signalMaps {
signalMaps["SIG"+key] = val
signalMaps[fmt.Sprintf("%d", val)] = val
}
log.SetFlags(0)
if runtime.GOOS == "windows" {
log.SetPrefix("fswatch >>> ")
} else {
log.SetPrefix("\033[32mfswatch\033[0m >>> ")
}
}
const (
CBLACK = "30"
CRED = "31"
CGREEN = "32"
CYELLOW = "33"
CBLUE = "34"
CMAGENTA = "35"
CPURPLE = "36"
CDEBUG = CPURPLE
CINFO = CGREEN
CWARNING = CRED
)
func CPrintf(ansiColor string, format string, args ...interface{}) {
if runtime.GOOS != "windows" {
format = "\033[" + ansiColor + "m" + format + "\033[0m"
}
log.Printf(format, args...)
}
type FSEvent struct {
Name string
}
type FWConfig struct {
Description string `yaml:"desc" json:"desc"`
Triggers []TriggerEvent `yaml:"triggers" json:"triggers"`
WatchPaths []string `yaml:"watch_paths" json:"watch_paths"`
WatchDepth int `yaml:"watch_depth" json:"watch_depth"`
}
type TriggerEvent struct {
Name string `yaml:"name" json:"name"`
Pattens []string `yaml:"pattens" json:"pattens"`
matchPattens []string `yaml:"-" json:"-"`
Environ map[string]string `yaml:"env" json:"env"`
Command string `yaml:"cmd" json:"cmd"`
Shell bool `yaml:"shell" json:"shell"`
cmdArgs []string `yaml:"-" json:"-"`
Delay string `yaml:"delay" json:"delay"`
delayDuration time.Duration `yaml:"-" json:"-"`
StopTimeout string `yaml:"stop_timeout" json:"stop_timeout"`
stopTimeoutDuration time.Duration `yaml:"-" json:"-"`
Signal string `yaml:"signal" json:"signal"`
killSignal os.Signal `yaml:"-" json:"-"`
KillSignal string `yaml:"kill_signal" json:"kill_signal"`
exitSignal os.Signal
kcmd *kexec.KCommand
}
func (this *TriggerEvent) Start() (waitC chan error) {
cmdString, _ := shellquote.Quote(this.cmdArgs)
CPrintf(CGREEN, fmt.Sprintf("[%s] exec start: %v", this.Name, cmdString))
startTime := time.Now()
cmd := kexec.Command(this.cmdArgs[0], this.cmdArgs[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
env := os.Environ()
for key, val := range this.Environ {
env = append(env, fmt.Sprintf("%s=%s", key, val))
}
cmd.Env = env
this.kcmd = cmd
waitC = make(chan error, 1)
if err := cmd.Start(); err != nil {
waitC <- err
return
}
go func() {
//var er error
waitC <- cmd.Wait()
log.Infof("[%s] finish in %s", this.Name, time.Since(startTime))
}()
return waitC
}
func (this *TriggerEvent) Stop(waitC chan error) bool {
if this.kcmd != nil {
if this.kcmd.ProcessState != nil && this.kcmd.ProcessState.Exited() {
this.kcmd = nil
return true
}
this.kcmd.Terminate(this.killSignal)
var done bool
select {
case err := <-waitC:
if err != nil {
CPrintf(CDEBUG, "[%s] program exited: %v", this.Name, err)
}
done = true
case <-time.After(this.stopTimeoutDuration):
done = false
}
if !done {
CPrintf(CYELLOW, "[%s] program still alive", this.Name)
//<-waitC
//CPrintf(CYELLOW, "[%s] program still alive, force kill", this.Name)
// this.kcmd.Terminate(syscall.SIGKILL)
} else {
this.kcmd = nil
}
return done
}
return true
}
// when use func (this *TriggerEvent) strange things happened, wired
func (this *TriggerEvent) WatchEvent(evtC chan FSEvent, wg *sync.WaitGroup) {
waitC := this.Start()
for evt := range evtC {
isMatch, err := ignore.Matches(evt.Name, this.Pattens)
if err != nil {
log.Fatal(err)
}
if !isMatch {
continue
}
if this.Stop(waitC) {
CPrintf(CGREEN, "changed: %v", evt.Name)
CPrintf(CGREEN, "delay: %v", this.Delay)
time.Sleep(this.delayDuration)
waitC = this.Start()
}
}
// force kill when exit
this.killSignal = this.exitSignal
this.Stop(waitC)
wg.Done()
}
func getShell() ([]string, error) {
if path, err := exec.LookPath("bash"); err == nil {
return []string{path, "-c"}, nil
}
if path, err := exec.LookPath("sh"); err == nil {
return []string{path, "-c"}, nil
}
// even windows, there still has git-bash or mingw
if runtime.GOOS == "windows" {
return []string{"cmd", "/c"}, nil
}
return nil, fmt.Errorf("Could not find bash or sh on path.")
}
func fixFWConfig(in FWConfig) (out FWConfig, err error) {
out = in
for idx, trigger := range in.Triggers {
outTg := &out.Triggers[idx]
if trigger.Delay == "" {
outTg.Delay = "100ms"
}
outTg.delayDuration, err = time.ParseDuration(outTg.Delay)
if err != nil {
return
}
if trigger.StopTimeout == "" {
outTg.StopTimeout = "500ms"
}
outTg.stopTimeoutDuration, err = time.ParseDuration(outTg.StopTimeout)
if err != nil {
return
}
if outTg.Signal == "" {
outTg.Signal = "KILL"
}
outTg.killSignal = signalMaps[outTg.Signal]
if outTg.KillSignal == "" {
outTg.exitSignal = syscall.SIGKILL
} else {
outTg.exitSignal = signalMaps[outTg.KillSignal]
}
rd := ioutil.NopCloser(bytes.NewBufferString(strings.Join(outTg.Pattens, "\n")))
patterns, er := ignore.ReadIgnore(rd)
if er != nil {
err = er
return
}
outTg.matchPattens = patterns
if outTg.Shell {
sh, er := getShell()
if er != nil {
err = er
return
}
outTg.cmdArgs = append(sh, outTg.Command)
} else {
outTg.cmdArgs, err = shlex.Split(outTg.Command)
if err != nil {
return
}
if len(outTg.cmdArgs) == 0 {
err = errors.New("No command defined")
return
}
}
}
if len(out.WatchPaths) == 0 {
out.WatchPaths = append(out.WatchPaths, ".")
}
if out.WatchDepth < 0 {
out.WatchDepth = 0
}
return
}
func readString(prompt, value string) string {
fmt.Printf("[?] %s (%s) ", prompt, value)
var s = value
fmt.Scanf("%s", &s)
return s
}
func genFWConfig() FWConfig {
var (
name string
command string
)
cwd, _ := os.Getwd()
name = filepath.Base(cwd)
name = readString("name:", name)
for command == "" {
fmt.Print("[?] command (go test -v): ")
reader := bufio.NewReader(os.Stdin)
command, _ = reader.ReadString('\n')
command = strings.TrimSpace(command)
if command == "" {
command = "go test -v"
}
}
fwc := FWConfig{
Description: fmt.Sprintf("Auto generated by fswatch [%s]", name),
Triggers: []TriggerEvent{{
Name: "main",
Pattens: []string{"**/*.go", "**/*.c", "**/*.py"},
Environ: map[string]string{
"DEBUG": "1",
},
Shell: true,
Command: command,
}},
}
out, _ := fixFWConfig(fwc)
return out
}
func uniqStrings(ss []string) []string {
out := make([]string, 0, len(ss))
m := make(map[string]bool, len(ss))
for _, key := range ss {
if !m[key] {
out = append(out, key)
m[key] = true
}
}
return out
}
func isDirectory(path string) bool {
pinfo, err := os.Stat(path)
return err == nil && pinfo.IsDir()
}
var fileModifyTimeMap = make(map[string]time.Time)
func isChanged(path string) bool {
pinfo, err := os.Stat(path)
if err != nil {
return true
}
mtime := pinfo.ModTime()
if mtime.Sub(fileModifyTimeMap[path]) > time.Millisecond*100 { // 100ms
fileModifyTimeMap[path] = mtime
return true
}
return false
}
// visits here for in case of duplicate paths
func WatchPathAndChildren(w *fsnotify.Watcher, paths []string, depth int, visits map[string]bool) error {
if visits == nil {
visits = make(map[string]bool)
}
watchDir := func(dir string) error {
if visits[dir] {
return nil
}
if err := w.Add(dir); err != nil {
if strings.Contains(err.Error(), "too many open files") {
log.Fatalf("Watch directory(%s) error: %v", dir, err)
}
log.Warnf("Watch directory(%s) error: %v", dir, err)
return err
}
log.Debug("Watch directory:", dir)
visits[dir] = true
return nil
}
var err error
for _, path := range paths {
if visits[path] {
continue
}
watchDir(path)
dirs, er := listAllDir(path, depth)
if er != nil {
err = er
log.Warnf("ERR list dir: %s, depth: %d, %v", path, depth, err)
continue
}
for _, dir := range dirs {
watchDir(dir)
}
}
return err
}
func listAllDir(path string, depth int) (dirs []string, err error) {
baseNumSeps := strings.Count(path, string(os.PathSeparator))
err = filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
base := info.Name()
if base != "." && strings.HasPrefix(base, ".") { // ignore hidden dir
return filepath.SkipDir
}
if base == "node_modules" {
return filepath.SkipDir
}
pathDepth := strings.Count(path, string(os.PathSeparator)) - baseNumSeps
if pathDepth > depth {
return filepath.SkipDir
}
dirs = append(dirs, path)
}
return nil
})
return
}
func drainEvent(fwc FWConfig) (globalEventC chan FSEvent, wg *sync.WaitGroup, err error) {
globalEventC = make(chan FSEvent, 1)
wg = &sync.WaitGroup{}
evtChannls := make([]chan FSEvent, 0)
// log.Println(len(fwc.Triggers))
for _, tg := range fwc.Triggers {
wg.Add(1)
evtC := make(chan FSEvent, 1)
evtChannls = append(evtChannls, evtC)
go func(tge TriggerEvent) {
tge.WatchEvent(evtC, wg)
}(tg)
// Can't write like this, the next loop tg changed, but go .. is not finished
// go tg.WatchEvent(evtC, wg)
}
go func() {
for evt := range globalEventC {
for _, eC := range evtChannls {
eC <- evt
}
}
for _, eC := range evtChannls {
close(eC)
}
}()
return
}
var ErrConfigFileNotExist = errors.New("Config file not exists")
func readFWConfig(paths ...string) (fwc FWConfig, err error) {
for _, cfgPath := range paths {
data, err := ioutil.ReadFile(cfgPath)
if err != nil {
continue
}
CPrintf(CINFO, "use config file: %s", cfgPath)
ext := filepath.Ext(cfgPath)
switch ext {
case ".yml":
if er := yaml.Unmarshal(data, &fwc); er != nil {
return fwc, er
}
case ".json":
if er := json.Unmarshal(data, &fwc); er != nil {
return fwc, er
}
default:
err = fmt.Errorf("Unknown format config file: %s", cfgPath)
return fwc, err
}
return fixFWConfig(fwc)
}
//fwc, err = fixFWConfig(fwc)
// data, _ = json.MarshalIndent(fwc, "", " ")
// fmt.Println(string(data))
// return fixFWConfig(fwc)
return fwc, ErrConfigFileNotExist
}
func transformEvent(fsw *fsnotify.Watcher, evtC chan FSEvent) {
go func() {
for err := range fsw.Errors {
log.Errorf("Watch error: %v", err)
}
}()
for evt := range fsw.Events {
if evt.Op == fsnotify.Create && isDirectory(evt.Name) {
log.Info("Add watcher", evt.Name)
fsw.Add(evt.Name)
continue
}
if evt.Op == fsnotify.Remove {
if err := fsw.Remove(evt.Name); err == nil {
log.Info("Remove watcher", evt.Name)
}
continue
}
if !isChanged(evt.Name) {
continue
}
//log.Printf("Changed: %s", evt.Name)
evtC <- FSEvent{ // may panic here
Name: evt.Name,
}
}
}
func initFWConfig() {
log.Infof("no fsw.yml detected, init create")
fwc := genFWConfig()
format := readString("Save format fsw.(json|yml)", "yml")
var data []byte
var cfg string
if strings.ToLower(format) == "json" {
data, _ = json.MarshalIndent(fwc, "", " ")
cfg = FWCONFIG_JSON
ioutil.WriteFile(FWCONFIG_JSON, data, 0644)
} else {
cfg = FWCONFIG_YAML
data, _ = yaml.Marshal(fwc)
ioutil.WriteFile(FWCONFIG_YAML, data, 0644)
}
fmt.Printf("Saved to %s\n", strconv.Quote(cfg))
}
const helpMessage = `fswatch usage:
fswatch [OPTIONS]
fswatch [Sub commands]
OPTIONS:
-h, --help show this help message
-v, --version show version
--config [filename] specify config file [default: fsw.yml]
Examples:
fswatch ls -l # show files when current directory file changes
fswatch # init fsw.yml when not exist
`
var (
version bool = false
configfile string = FWCONFIG_YAML
)
func main() {
flag.BoolVar(&version, "version", false, "Show version")
flag.BoolVar(&version, "v", false, "Show version")
flag.StringVar(&configfile, "config", FWCONFIG_YAML, "Specify config file")
flag.Usage = func() {
fmt.Print(helpMessage)
return
}
var args []string
if len(os.Args) > 1 && strings.HasPrefix(os.Args[1], "-") {
flag.Parse()
args = flag.Args()
} else {
args = os.Args[1:]
}
if version {
fmt.Println(VERSION)
return
}
fwc, err := readFWConfig(configfile, "fsw.yml", "fsw.json", ".fsw.yml", ".fsw.json")
if err != nil {
if err == ErrConfigFileNotExist {
initFWConfig()
}
log.Fatal(err)
}
if len(args) > 0 {
patterns := make([]string, 0, 5)
for _, argument := range args {
if _, err := os.Stat(argument); err == nil {
if fileExt := filepath.Ext(argument); fileExt != "" {
patterns = append(patterns, "**/*"+fileExt)
}
}
}
if len(patterns) == 0 {
patterns = append(patterns, "**/*.go", "**/*.c", "**/*.py")
}
log.Infof("Watche patterns: %v", patterns)
command, _ := shellquote.Quote(args)
fwc.Triggers = []TriggerEvent{{
Pattens: patterns,
Environ: map[string]string{
"DEBUG": "1",
},
Shell: false,
Command: command,
}}
fwc, err = fixFWConfig(fwc)
if err != nil {
log.Fatal(err)
}
}
visits := make(map[string]bool)
fswatcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
err = WatchPathAndChildren(fswatcher, fwc.WatchPaths, fwc.WatchDepth, visits)
if err != nil {
log.Println(err)
}
evtC, wg, err := drainEvent(fwc)
if err != nil {
log.Fatal(err)
}
sigOS := make(chan os.Signal, 1)
signal.Notify(sigOS, syscall.SIGINT)
signal.Notify(sigOS, syscall.SIGTERM)
go func() {
sig := <-sigOS
CPrintf(CPURPLE, "Catch signal %v!", sig)
close(evtC)
}()
go transformEvent(fswatcher, evtC)
wg.Wait()
CPrintf(CPURPLE, "Kill all running ... Done")
}