-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
53 lines (43 loc) · 1.15 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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"strings"
"github.com/rjeczalik/notify"
)
func main() {
var dir, cmdstr string
flag.StringVar(&dir, "dir", "", "the directory to watch")
flag.StringVar(&cmdstr, "cmd", "", "the command to run on change events (simply pipe to stdout if none given)")
flag.Parse()
if dir == "" {
flag.Usage()
log.Fatal("need dir argument")
}
watcher := make(chan notify.EventInfo, 1)
dir = strings.Join([]string{dir, "..."}, "/")
err := notify.Watch(dir, watcher, notify.Write)
if err != nil {
log.Fatal(err)
}
defer notify.Stop(watcher)
for {
select {
case ev := <-watcher:
if cmdstr == "" {
fmt.Printf("%s\n", ev.Path())
} else {
cmd := exec.Command(cmdstr, ev.Path())
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Println("Error executing command:", err)
}
}
}
}
}