-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
549 lines (492 loc) · 14.6 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
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
// monitfiles is a small exercise cli app that uses go routines to monitor file changes and execute
// a script. The sub directories will also be searched for the file types being monitores.
// Build with:
// $ go build
// Execute with:
// $ monitfiles -f "htm html css js" -i 1 -p "/path/to/root" -s scripts/brave_reload.sh -b -v
// $ monitfiles --path . --filetypes "htm html css js" --script "/path/to/script" -v
// $ monitfiles --path . --filetypes "htm html css js" -w -s "/path/to/file"
//
// There is a comand line associated, try:
// > help
// > moo
// > list
//
// TODO: if several files change at a time dump the multiple requests with a timeout.
//
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"time"
"github.com/pkg/browser"
)
var (
errInvalidParam = errors.New("invalid param(s)")
errFile = errors.New("invalid file")
errName = errors.New("invalid file name")
errPath = errors.New("path error")
errNotAPath = errors.New("invalid path, please select a path not a file")
errInvalidTypes = errors.New("invalid file types")
errInvalidScript = errors.New("empty script")
errMaxFiles = errors.New("MAX files limit reached, please consider new limit")
errUnsuported = errors.New("unsuported platform")
)
// Configs struct where the flags are passed into
type Configs struct {
Path string
FileTypes []string
FileNames []string
FileTypeNone bool
ExcludeDotDirs bool
IncludeFileNames []string
ExcludeFileNames []string
Script string
Web bool // opend the URL in Script in the default browser
Blocking bool // blocks script execution, waits to finish, defaults to false
Verbose bool
MaxFiles uint
Interval uint // seconds
ScannedDirs uint
Files uint
Trigger chan bool
}
// Store struct for each file monitoring. The State and Done channels serve to communicate with the main thread.
// The ticker guarantees independent go routine execution.
type Store struct {
ID uint
Filename string
FileType string
Path string
ModTime time.Time
Info os.FileInfo
Updated uint
Ticker *time.Ticker
State chan bool
Done chan bool
}
// Storage is the global slice of stores for files
type Storage []Store
func main() {
exit := exitErrors[run(os.Args, os.Stdout)]
os.Exit(exit.Exit)
}
func run(ags []string, stdout io.Writer) int {
var err error
config := &Configs{
Path: "",
FileTypes: []string{},
FileNames: []string{},
FileTypeNone: false,
ExcludeDotDirs: true,
IncludeFileNames: []string{}, //TODO
ExcludeFileNames: []string{}, //TODO
Script: "",
Web: false, // opens the URL in Script in the active browser
Blocking: false,
Verbose: false,
MaxFiles: 0,
Interval: 2,
ScannedDirs: 0,
Files: 0,
}
// flags
var flagPath string
var flagFileTypes string
var flagFileNames string
var flagFileTypeNone bool // for files with no extension
var flagExcludeDotDirs bool // exclude .dirs (dot dirs like .git)
var flagBlocking bool
var flagVerbose bool
var flagMaxFiles uint
var flagInterval uint
var flagScript string
var flagWeb bool
var flagVersion bool
flag.StringVar(&flagPath, "path", ".", "path to monitor")
flag.StringVar(&flagPath, "p", "", "(shorthand for path)")
flag.StringVar(&flagFileTypes, "filetypes", "htm html css js", "file types to be monitored for changes")
flag.StringVar(&flagFileTypes, "f", "htm html css js", "(shorthand for filetypes)")
flag.StringVar(&flagFileNames, "filenames", "", "file names to be monitored for changes")
flag.StringVar(&flagFileNames, "n", "", "(shorthand for filenames)")
flag.BoolVar(&flagFileTypeNone, "none", false, "file types without extension (boolean, set to true to activate)")
flag.BoolVar(&flagExcludeDotDirs, "no-dot", true, "exclude (dot) dirs like .git (boolean, set to false to enable entering them)")
flag.StringVar(&flagScript, "script", "", "comand to be called upon change detection")
flag.StringVar(&flagScript, "s", "", "(shorthand for script)")
flag.BoolVar(&flagWeb, "w", false, "opens the script URL in predefined browser")
flag.BoolVar(&flagBlocking, "b", false, "blocks script execution, waits to finish, defaults to false")
flag.BoolVar(&flagVerbose, "v", false, "verbose output")
flag.UintVar(&flagMaxFiles, "max", 200, "max number of files to monitor")
flag.UintVar(&flagInterval, "i", 2, "interval in seconds for monitor changes")
flag.BoolVar(&flagVersion, "version", false, "version of the app")
flag.Parse() // TODO: catch it here, kills the program flow
if flag.NFlag() < 1 {
flag.PrintDefaults()
return errNoParams
}
config.Verbose = flagVerbose
if flagVersion {
fmt.Println("Version: ", string(version))
if config.Verbose {
fmt.Println("Release: ", string(goToolChainRev))
fmt.Println("Compiled and built with <3 from Gophers:", string(goToolChainVer))
}
return errOK
}
config.Path, err = validPath(flagPath)
if err != nil {
log.Printf("use -h for help")
log.Fatalf("*** Error: %s", err)
}
config.FileTypes, err = validFileTypes(flagFileTypes)
if err != nil {
log.Printf("use -h for help")
log.Fatalf("*** Error: %s", err)
}
config.FileNames, err = validFileNames(flagFileNames)
if err != nil {
log.Printf("Warning, some filenames were discarded...")
}
config.FileTypeNone = flagFileTypeNone
config.ExcludeDotDirs = flagExcludeDotDirs
config.Blocking = flagBlocking
config.MaxFiles = flagMaxFiles
config.Interval = flagInterval
config.Web = flagWeb
config.Script, err = validScript(flagScript)
if err != nil {
log.Printf("use -h for help")
return errScript
}
// channels
config.Trigger = make(chan bool)
storage := &Storage{}
config.ScannedDirs, config.Files, err = storage.New(*config)
if err != nil {
return errStorage
}
// start monitoring
for i := range *storage {
(*storage)[i].Monitor(config)
}
log.Print("************************************************")
log.Printf("Root path: %s", config.Path)
log.Printf("File types: %s", config.FileTypes)
log.Printf("File names: %s", config.FileNames)
log.Printf("File types with no extension ? %t", config.FileTypeNone)
log.Printf("Exclude dot dirs ? %t", config.ExcludeDotDirs)
log.Printf("Max number of files: %d", config.MaxFiles)
log.Printf("Blocking ? %t", config.Blocking)
log.Printf("Verbose ? %t", config.Verbose)
log.Printf("Interval: %d seconds", config.Interval)
log.Printf("Script: %s", config.Script)
log.Printf("Web: %t", config.Web)
log.Printf("Number of directories scanned: %d", config.ScannedDirs)
log.Printf("Number of files added and being monitored: %d", config.Files)
log.Print("************************************************")
fmt.Println()
fmt.Print("> ")
// user interface
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
fmt.Print("> ")
line := scanner.Text()
if line == "" {
continue
}
if err := parser(line, storage, config); err != errContinue {
return err
}
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading standard input:", err)
}
return errUnknown
}
// parser is the function parses and interprets the command
func parser(cmd string, storage *Storage, config *Configs) int {
fmt.Println("")
switch cmd {
case "quit":
for i := range *storage {
(*storage)[i].Done <- true
}
if config.Verbose {
fmt.Println("\U0001F44B bye!")
}
return errOK
case "?", "help", "h":
fmt.Println("available commands: quit help moo count list fire configs start stop")
case "moo":
fmt.Println("^__^ \n(oo)\\_______ \n(__)\\ )\\/\\ \n ||----w | \n || ||\n ")
case "count":
fmt.Printf("%d files on store \n", len(*storage))
case "configs":
fmt.Println("configs:")
fmt.Printf(" Root path: %s \n", config.Path)
fmt.Printf(" File types: %s \n", config.FileTypes)
fmt.Printf(" File names: %s \n", config.FileNames)
fmt.Printf(" File types with no extension ? %t \n", config.FileTypeNone)
fmt.Printf(" Exclude dot dirs ? %t \n", config.ExcludeDotDirs)
fmt.Printf(" Blocking ? %t \n", config.Blocking)
fmt.Printf(" Verbose ? %t \n", config.Verbose)
fmt.Printf(" Max number of files: %d \n", config.MaxFiles)
fmt.Printf(" Interval: %d seconds \n", config.Interval)
fmt.Printf(" Script: %s \n", config.Script)
fmt.Printf(" Web: %t \n", config.Web)
fmt.Printf(" Number of directories scanned: %d \n", config.ScannedDirs)
fmt.Printf(" Number of files added and being monitored: %d \n", config.Files)
case "list":
for _, s := range *storage {
fmt.Printf("%d (%d updates) %s last modified at %v \n", s.ID, s.Updated, s.Path, s.ModTime)
}
case "fire":
// exec script
Exec(config)
case "start":
for i := range *storage {
(*storage)[i].State <- true
}
if config.Verbose {
log.Printf("+++ monitoring %d files at interval %d seconds \n", config.Files, config.Interval)
}
case "stop":
for i := range *storage {
(*storage)[i].State <- false
}
if config.Verbose {
log.Printf("+++ stopped monitoring %d files \n", config.Files)
}
case "debug":
default:
if config.Verbose {
fmt.Println("unknown command...")
}
}
fmt.Print("> ")
// no error, do nothing
return errContinue
}
// Exec the script
func Exec(config *Configs) {
var out []byte
var err error
if config.Verbose {
log.Printf("Script run: %s\n", config.Script)
}
if config.Web {
// if -w flag and the script contains an URL to be opened in a browser
err = browser.OpenURL(config.Script)
} else {
// script execution
cmd := exec.Command(config.Script)
if config.Blocking {
out, err = cmd.Output()
if config.Verbose {
log.Printf("Output: %s \n", out)
}
} else {
err = cmd.Start()
if err == nil {
err = cmd.Wait()
}
}
}
if config.Verbose && err != nil {
log.Printf("Script error: %v\n", err)
}
}
// Monitor a file for file changes every interval.
// On each tick, file changes are checked. If file check returns error a log warning is generated only.
// Upon Done, goroutines is returned, channels are closed.
// State channel activates/deactivates the monitoring action, yet trigger continues.
func (s *Store) Monitor(config *Configs) {
go func(s *Store) {
defer close(s.Done)
defer s.Ticker.Stop()
var state = true
for {
select {
case <-s.Done:
return
case state = <-s.State:
case <-s.Ticker.C:
f, err := os.Stat(s.Path)
if err != nil {
if config.Verbose {
log.Printf("file check error for (%d) %s (%s)", s.ID, s.Filename, err)
}
} else {
if state && f.ModTime() != s.ModTime {
if config.Verbose {
log.Printf(" +++ file change: (%d) %s", s.ID, s.Filename)
}
// update the record with new information
s.ModTime = f.ModTime()
s.Info = f
s.Updated++
// exec script
Exec(config)
}
}
}
}
}(s)
}
// New storage preloads all files in the storage structure.
// Returns:
// - number of scanned dirs
// - number of added files
// - error
// A ticker channel is added so that there's independence per store.
func (s *Storage) New(config Configs) (uint, uint, error) {
var nd, nf uint = 0, 0
// per dir
err := filepath.Walk(config.Path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if nf > config.MaxFiles {
return errMaxFiles
}
if info.IsDir() {
// directory exclusion
if (info.Name()[0:1] == ".") && config.ExcludeDotDirs {
return filepath.SkipDir
} else {
nd++
if config.Verbose {
log.Printf("* entering directory: %s", info.Name())
}
}
}
// file picking
if !info.IsDir() {
if config.Verbose {
log.Printf(" > checking %s", info.Name())
}
if !info.Mode().IsRegular() {
return nil
}
// check extension size and if allowed
extSize := len(filepath.Ext(info.Name()))
if extSize == 0 && !config.FileTypeNone {
return nil
}
// check if extension inside slice of valid ones
ext := ""
if extSize != 0 {
ext = filepath.Ext(info.Name())[1:]
}
i := sort.SearchStrings(config.FileTypes, ext)
caseExtListed := extSize > 0 && i < len(config.FileTypes) && config.FileTypes[i] == ext
caseNoExt := extSize == 0 && config.FileTypeNone
if caseExtListed || caseNoExt {
// add the file
if config.Verbose {
log.Printf(" + adding %s (%v)", info.Name(), info.ModTime())
}
nf++
f := Store{
ID: nf,
Filename: info.Name(),
FileType: ext,
Path: path,
ModTime: info.ModTime(),
Updated: 0,
Info: info,
Done: make(chan bool),
State: make(chan bool),
Ticker: time.NewTicker(time.Duration(config.Interval) * time.Second),
}
*s = append(*s, f)
}
}
return nil
})
// per name (-filenames)
for _, n := range config.FileNames {
info, err := os.Stat(n)
if err != nil {
log.Printf("## %s %s %v", n, errName, err)
continue
}
// add the file
if config.Verbose {
log.Printf(" + adding %s (%v)", info.Name(), info.ModTime())
}
nf++
f := Store{
ID: nf,
Filename: info.Name(),
FileType: filepath.Ext(info.Name())[1:],
Path: n,
ModTime: info.ModTime(),
Updated: 0,
Info: info,
Done: make(chan bool),
State: make(chan bool),
Ticker: time.NewTicker(time.Duration(config.Interval) * time.Second),
}
*s = append(*s, f)
}
return nd, nf, err
}
// validPath check the existance of a path and converts to Absolute path.
func validPath(p string) (string, error) {
stat, err := os.Stat(p)
if err != nil {
return "", errPath
}
if !stat.IsDir() {
return "", errNotAPath
}
res, err := filepath.Abs(p)
if err != nil {
return "", errPath
}
return res, err
}
// validFileTypes checks if not empty, creates a slice of file types and converts to lower case
// saving n convertions that way later. Slice is sorted to ease comparisions later.
func validFileTypes(ft string) ([]string, error) {
var err error
res := strings.Fields(strings.ToLower(ft))
if len(res) < 1 {
return res, errInvalidTypes
}
sort.StringSlice(res).Sort()
return res, err
}
// validFileNames checks filenames and removes invalid ones.
func validFileNames(fn string) ([]string, error) {
var err error
names := strings.Fields(fn)
if len(names) < 1 {
return nil, nil
}
res := make([]string, len(names))
for i, n := range names {
if _, err := os.Open(n); err == nil {
res[i] = n
}
}
sort.StringSlice(res).Sort()
return res, err
}
func validScript(s string) (string, error) {
var err error
if len(s) == 0 {
return "", errInvalidScript
}
return s, err
}