forked from go-playground/justdoit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
227 lines (171 loc) · 4.45 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
package main
import (
"flag"
"os"
"os/exec"
"strings"
"sync"
"path/filepath"
"regexp"
"gopkg.in/fsnotify.v1"
"github.com/go-playground/log"
"github.com/go-playground/log/handlers/console"
)
func init() {
cLog := console.New(true)
log.AddHandler(cLog, log.AllLevels...)
}
var (
lock sync.Mutex
running bool
proc *exec.Cmd
)
func main() {
flagWatch := flag.String("watch", "./", "Directory to watch for changes (recursive)")
flagExclude := flag.String("exclude", "(.git|vendor)$", "Regex of paths to exclude")
flagInclude := flag.String("include", `(.+\.go|.+\.c)$`, "Regex of files to include")
flagBuild := flag.String("build", "go install -v", "Command to Build/Compile program")
flagRun := flag.String("run", "", "Command to run your application")
flag.Parse()
if len(strings.TrimSpace(*flagBuild)) == 0 {
log.Fatal("build is a required argument")
}
if len(strings.TrimSpace(*flagRun)) == 0 {
log.Fatal("run is a required argument")
}
var include *regexp.Regexp
var exclude *regexp.Regexp
absWatch, err := filepath.Abs(*flagWatch)
if err != nil {
log.WithFields(log.F("error", err)).Fatal("invalid watch directory, could not determine absolute path")
}
fi, err := os.Stat(absWatch)
if err != nil || !fi.IsDir() {
log.WithFields(log.F("error", err)).Fatal("invalid watch directory")
}
if len(*flagInclude) > 0 {
include, err = regexp.Compile(*flagInclude)
if err != nil {
log.WithFields(log.F("error", err)).Fatal("invalid include regex")
}
}
if len(*flagExclude) > 0 {
exclude, err = regexp.Compile(*flagExclude)
if err != nil {
log.WithFields(log.F("error", err)).Fatal("invalid include regex")
}
}
notif := make(chan struct{})
go watch(notif, absWatch, include, exclude)
// trigger event for initial run of application
go func() {
notif <- struct{}{}
}()
build(*flagBuild, *flagRun, notif)
}
func build(buildCmd, executeCms string, event <-chan struct{}) {
buildArgs := strings.Split(buildCmd, " ")
executeArgs := strings.Split(executeCms, " ")
for range event {
log.Notice("Running build command")
if !execute(buildArgs, false) {
kill(true)
continue
}
go run(executeArgs)
}
}
func kill(unlock bool) {
lock.Lock()
if running {
// process can be nil
if proc != nil && proc.Process != nil {
log.Notice("Killing Process")
err := proc.Process.Kill()
if err != nil {
log.WithFields(log.F("error", err)).Error("could not kill process")
}
}
running = false
}
if unlock {
lock.Unlock()
}
}
func run(args []string) {
kill(false)
execute(args, true)
}
// runs generically provided command
func execute(args []string, setProc bool) (success bool) {
cmd := exec.Command(args[0], args[1:]...)
cmd.Env = os.Environ()
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if setProc {
proc = cmd
running = true
lock.Unlock()
log.Notice("Executing run command")
}
err := cmd.Run()
if !setProc && err != nil { // not outputting killed for already running
log.WithFields(log.F("error", err)).Notice("error stopping cmd")
return false
}
return true
}
func watch(notif chan<- struct{}, watch string, include, exclude *regexp.Regexp) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.WithFields(log.F("error", err)).Fatal("issue creating watcher")
}
defer watcher.Close()
walker := func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
if exclude != nil && exclude.MatchString(path) {
return filepath.SkipDir
}
err = watcher.Add(path)
if err != nil {
log.WithFields(log.F("error", err)).Warn("issue adding directory to watch")
}
}
return nil
}
err = filepath.Walk(watch, walker)
if err != nil {
log.WithFields(log.F("error", err)).Fatal("could not walk watch path")
}
// evt := make(chan struct{})
// go func() {
// for {
// <-evt
// SELECT:
// select {
// case <-time.After(700 * time.Millisecond):
// notif <- struct{}{}
// case <-evt:
// goto SELECT
// }
// }
// }()
go func() {
for {
select {
case event := <-watcher.Events:
if event.Op&fsnotify.Remove == fsnotify.Remove || event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create {
if include != nil && !include.MatchString(event.Name) {
continue
}
notif <- struct{}{}
// evt <- struct{}{}
}
case err := <-watcher.Errors:
log.WithFields(log.F("error", err)).Error("watcher error")
}
}
}()
<-make(chan bool)
}