-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
98 lines (83 loc) · 2.2 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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/loov/watchrun/pipeline"
"github.com/loov/watchrun/watch"
)
var (
ignore = watch.Globs{NoDefault: false, Default: watch.DefaultIgnore, Additional: nil}
care = watch.Globs{NoDefault: false, Default: nil, Additional: nil}
loglevel = LogLevelInfo
interval = flag.Duration("interval", 300*time.Millisecond, "interval to wait between monitoring")
monitor = flag.String("monitor", ".", "files/folders/globs to monitor")
recurse = flag.Bool("recurse", true, "when watching a folder should recurse")
verbose = flag.Bool("verbose", false, "verbose output (same as -log=debug)")
clear = flag.Bool("clear", false, "clear the screen after rerunning the commands")
)
func init() {
flag.Var(&ignore, "ignore", "ignore files/folders that match these globs")
flag.Var(&care, "care", "check only changes to files that match these globs")
flag.Var(&loglevel, "log", "logging level (debug, info, warn, error, silent)")
}
func main() {
flag.Parse()
if *verbose {
loglevel = LogLevelDebug
}
args := flag.Args()
if len(args) == 0 {
flag.PrintDefaults()
return
}
procs := pipeline.ParseArgs(args)
monitoring := strings.Split(*monitor, ";")
ignoring := ignore.All()
caring := care.All()
if loglevel.Matches(LogLevelDebug) {
fmt.Println("Options:")
fmt.Println(" interval : ", *interval)
fmt.Println(" recursive : ", *recurse)
fmt.Println(" monitoring : ", monitoring)
fmt.Println(" ignoring : ", ignoring)
fmt.Println(" caring : ", caring)
fmt.Println()
fmt.Println("Processes:")
for _, proc := range procs {
fmt.Printf(" %s %s\n", proc.Cmd, strings.Join(proc.Args, " "))
}
fmt.Println()
}
watcher := watch.New(
*interval,
monitoring,
ignoring,
caring,
*recurse,
)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
watcher.Stop()
}()
var pipe *pipeline.Pipeline
for range watcher.Changes {
if pipe != nil {
pipe.Kill()
}
if *clear {
ClearScreen()
}
logln(LogLevelInfo, "<<", time.Now(), ">>")
pipe = pipeline.Run(pipelineLog{}, procs)
}
if pipe != nil {
pipe.Kill()
}
}