-
Notifications
You must be signed in to change notification settings - Fork 0
/
nwatch.go
executable file
·299 lines (250 loc) · 6.54 KB
/
nwatch.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
///usr/bin/env true; exec /usr/bin/env go run "$0" "$@"
package main
import (
"context"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"os/signal"
"path"
"strings"
"sync"
"syscall"
"time"
"github.com/tzvetkoff-go/optparse"
"github.com/tzvetkoff-go/fnmatch"
"github.com/tzvetkoff-go/nwatch/pkg/watcher"
)
// version ...
var version = "(dev)"
// usage ...
func usage(f io.Writer, name string) {
fmt.Fprintln(f, "Usage:")
fmt.Fprintf(f, " %s [options]\n", name)
fmt.Fprintln(f)
fmt.Fprintln(f, "Options:")
fmt.Fprintln(f, " -h, --help Print help and exit")
fmt.Fprintln(f, " -v, --version Print version and exit")
fmt.Fprintln(f, " -V, --verbose Verbose output")
fmt.Fprintln(f, " -d DIR, --directory=DIR Directories to watch")
fmt.Fprintln(f, " -e EXC, --exclude-dir=EXC Directories to exclude")
fmt.Fprintln(f, " -p PAT, --pattern=PAT File patterns to match")
fmt.Fprintln(f, " -i IGN, --ignore=IGN File patterns to ignore")
fmt.Fprintln(f, " -b BLD, --build=BLD Build command to execute")
fmt.Fprintln(f, " -s SRV, --server=SRV Server command to run after successful build")
fmt.Fprintln(f, " -w ERR, --error-server=ERR Web server address in case of an error")
if f == os.Stderr {
os.Exit(1)
}
os.Exit(0)
}
// mu ...
var mu sync.Mutex
// lastBuildOutput ...
var lastBuildOutput []byte
// runBuild ...
func runBuild(command string) error {
mu.Lock()
defer mu.Unlock()
verbose("runBuild: %s", command)
cmd := exec.Command("/bin/sh", "-c", command)
out, err := cmd.CombinedOutput()
if err != nil {
lastBuildOutput = out
fmt.Fprintln(os.Stderr, "-------- build error: --------")
fmt.Fprintln(os.Stderr, strings.TrimSpace(string(out)))
fmt.Fprintln(os.Stderr, "------------------------------")
}
return err
}
// serverCmd ...
var serverCmd *exec.Cmd
// runServer ...
func runServer(command string) {
mu.Lock()
defer mu.Unlock()
verbose("runServer: %s", command)
if serverCmd != nil && serverCmd.Process != nil {
_ = serverCmd.Process.Kill()
serverCmd.Process.Wait()
serverCmd = nil
}
if errorServerChan != nil {
errorServerChan <- true
if errorServerChan != nil {
close(errorServerChan)
}
errorServerChan = nil
}
serverCmd = exec.Command("/bin/sh", "-c", command)
serverCmd.Stdout = os.Stdout
serverCmd.Stderr = os.Stderr
err := serverCmd.Start()
if err != nil {
serverCmd = nil
fmt.Fprintln(os.Stderr, "-------- server error: --------")
fmt.Fprintln(os.Stderr, strings.TrimSpace(err.Error()))
fmt.Fprintln(os.Stderr, "-------------------------------")
}
}
// errorServerChan ...
var errorServerChan chan bool
// runErrorServer ...
func runErrorServer(address string) {
mu.Lock()
defer mu.Unlock()
verbose("runErrorServer: %s", address)
if serverCmd != nil && serverCmd.Process != nil {
_ = serverCmd.Process.Kill()
serverCmd.Process.Wait()
serverCmd = nil
}
if errorServerChan != nil {
errorServerChan <- true
if errorServerChan != nil {
close(errorServerChan)
}
errorServerChan = nil
}
s := &http.Server{
Addr: address,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/plain")
w.Write(lastBuildOutput)
}),
}
errorServerChan = make(chan bool, 1)
go func() {
<-errorServerChan
if errorServerChan != nil {
close(errorServerChan)
}
errorServerChan = nil
s.Shutdown(context.TODO())
}()
go func() {
e := s.ListenAndServe()
fmt.Println(e)
}()
}
// matchAny ...
func matchAny(s string, patterns []string) bool {
for _, pattern := range patterns {
if fnmatch.Match(pattern, s) {
return true
}
}
return false
}
// pVerbose ...
var pVerbose = optparse.Bool("verbose", 'V', false)
// verbose ...
func verbose(format string, args ...interface{}) {
if *pVerbose {
fmt.Fprintf(os.Stderr, ">> "+format+"\n", args...)
}
}
// main ...
func main() {
// Options
pHelp := optparse.Bool("help", 'h', false)
pVersion := optparse.Bool("version", 'v', false)
pDirectories := optparse.StringList("directory", 'd')
pExcludes := optparse.StringList("exclude", 'e')
pPatterns := optparse.StringList("pattern", 'p')
pIgnores := optparse.StringList("ignore", 'i')
pBuild := optparse.String("build", 'b', "")
pServer := optparse.String("server", 's', "")
pErrorServer := optparse.String("error-server", 'w', "")
// Parse
args, err := optparse.Parse()
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %s\n\n", os.Args[0], err.Error())
usage(os.Stderr, os.Args[0])
}
// We don't accept positional arguments
if len(args) != 0 {
fmt.Fprintf(os.Stderr, "%s: wrong number of arguments (given %d, expected %d)\n\n", os.Args[0], len(args), 0)
usage(os.Stderr, os.Args[0])
}
// Help
if *pHelp {
usage(os.Stdout, os.Args[0])
}
// Version
if *pVersion {
fmt.Println(version)
os.Exit(0)
}
// Default to current directory
if len(*pDirectories) == 0 {
*pDirectories = []string{"."}
}
// Default to *
if len(*pPatterns) == 0 {
*pPatterns = []string{"*"}
}
// Build command is required
if *pBuild == "" {
fmt.Fprintf(os.Stderr, "%s: --build is required\n\n", os.Args[0])
usage(os.Stderr, os.Args[0])
}
// Create watcher
watcher, err := watcher.NewWatcher(*pExcludes)
if err != nil {
panic(err)
}
// Watch in the background
go func() {
ticker := time.Tick(100 * time.Millisecond)
hasChanges := false
for {
select {
case evt := <-watcher.Events:
verbose("evt: %s", evt)
basename := path.Base(evt)
if !matchAny(basename, *pPatterns) {
verbose("no-match: %q does not match any of %q", basename, *pPatterns)
continue
}
if matchAny(basename, *pIgnores) {
verbose("match-ignore: %q matches at least one of %q", basename, *pIgnores)
continue
}
hasChanges = true
case <-ticker:
if hasChanges {
hasChanges = false
if runBuild(*pBuild) == nil && *pServer != "" {
runServer(*pServer)
} else if *pErrorServer != "" {
runErrorServer(*pErrorServer)
}
}
}
}
}()
// Exit strategy
ch := make(chan os.Signal)
go func() {
<-ch
fmt.Println("\nAborting...")
watcher.Close()
}()
signal.Notify(ch, syscall.SIGINT)
signal.Notify(ch, syscall.SIGTERM)
// Run initial build if we have a server command passed
if *pServer != "" && runBuild(*pBuild) == nil {
runServer(*pServer)
} else if *pErrorServer != "" {
runErrorServer(*pErrorServer)
}
// Add directories to watch
for _, directory := range *pDirectories {
watcher.Add(directory)
}
// Finally, watch
watcher.Run()
}