-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
159 lines (134 loc) · 3.13 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
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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"strings"
"time"
"unicode"
"github.com/radovskyb/watcher"
)
func main() {
interval := flag.String("interval", "100ms", "watcher poll interval")
recursive := flag.Bool("recursive", true, "watch folders recursively")
dotfiles := flag.Bool("dotfiles", true, "watch dot files")
cmd := flag.String("cmd", "", "command to run when an event occurs")
listFiles := flag.Bool("list", false, "list watched files on start")
ignore := flag.String("ignore", "", "comma separated list of paths to ignore")
flag.Parse()
// Retrieve the list of files and folders.
files := flag.Args()
// If no files/folders were specified, watch the current directory.
if len(files) == 0 {
curDir, err := os.Getwd()
if err != nil {
log.Fatalln(err)
}
files = append(files, curDir)
}
var cmdName string
var cmdArgs []string
if *cmd == "" {
log.Fatalln("No command provided")
}
split := strings.FieldsFunc(*cmd, unicode.IsSpace)
cmdName = split[0]
if len(split) > 1 {
cmdArgs = split[1:]
}
// Create a new Watcher with the specified options.
w := watcher.New()
w.IgnoreHiddenFiles(!*dotfiles)
// Get any of the paths to ignore.
ignoredPaths := strings.Split(*ignore, ",")
for _, path := range ignoredPaths {
trimmed := strings.TrimSpace(path)
if trimmed == "" {
continue
}
err := w.Ignore(trimmed)
if err != nil {
log.Fatalln(err)
}
}
done := make(chan struct{})
go func() {
defer close(done)
var c *exec.Cmd
c = exec.Command(cmdName, cmdArgs...)
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if err := c.Start(); err != nil {
log.Fatalln(err)
}
for {
select {
case event := <-w.Event:
// Print the event's info.
fmt.Println(event)
_ = c.Process.Kill()
// Run the command if one was specified.
c = exec.Command(cmdName, cmdArgs...)
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if err := c.Start(); err != nil {
log.Println(err)
continue
}
case err := <-w.Error:
if err == watcher.ErrWatchedFileDeleted {
fmt.Println(err)
continue
}
log.Fatalln(err)
case <-w.Closed:
return
}
}
}()
// Add the files and folders specified.
for _, file := range files {
if *recursive {
if err := w.AddRecursive(file); err != nil {
log.Fatalln(err)
}
} else {
if err := w.Add(file); err != nil {
log.Fatalln(err)
}
}
}
// Print a list of all of the files and folders being watched.
if *listFiles {
for path, f := range w.WatchedFiles() {
fmt.Printf("%s: %s\n", path, f.Name())
}
fmt.Println()
}
fmt.Printf("Watching %d files\n", len(w.WatchedFiles()))
// Parse the interval string into a time.Duration.
parsedInterval, err := time.ParseDuration(*interval)
if err != nil {
log.Fatalln(err)
}
closed := make(chan struct{})
c := make(chan os.Signal)
signal.Notify(c, os.Kill, os.Interrupt)
go func() {
<-c
w.Close()
<-done
fmt.Println("watcher closed")
close(closed)
}()
// Start the watching process.
if err := w.Start(parsedInterval); err != nil {
log.Fatalln(err)
}
<-closed
}