This repository has been archived by the owner on Jul 28, 2023. It is now read-only.
forked from radovskyb/watcher
/
main.go
175 lines (152 loc) · 3.76 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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")
startcmd := flag.Bool("startcmd", false, "run the command when watcher starts")
listFiles := flag.Bool("list", false, "list watched files on start")
stdinPipe := flag.Bool("pipe", false, "pipe event's info to command's stdin")
keepalive := flag.Bool("keepalive", false, "keep alive when a cmd returns code != 0")
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 != "" {
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)
for {
select {
case event := <-w.Event:
// Print the event's info.
fmt.Println(event)
// Run the command if one was specified.
if *cmd != "" {
c := exec.Command(cmdName, cmdArgs...)
if *stdinPipe {
c.Stdin = strings.NewReader(event.String())
} else {
c.Stdin = os.Stdin
}
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if err := c.Run(); err != nil {
if (c.ProcessState == nil || !c.ProcessState.Success()) && *keepalive {
log.Println(err)
continue
}
log.Fatalln(err)
}
}
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)
}()
// Run the command before watcher starts if one was specified.
go func() {
if *cmd != "" && *startcmd {
c := exec.Command(cmdName, cmdArgs...)
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if err := c.Run(); err != nil {
if (c.ProcessState == nil || !c.ProcessState.Success()) && *keepalive {
log.Println(err)
return
}
log.Fatalln(err)
}
}
}()
// Start the watching process.
if err := w.Start(parsedInterval); err != nil {
log.Fatalln(err)
}
<-closed
}