-
Notifications
You must be signed in to change notification settings - Fork 29
/
gow_main.go
177 lines (148 loc) · 3.48 KB
/
gow_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
/**
Go Watch: missing watch mode for the "go" command. Invoked exactly like the
"go" command, but also watches Go files and reruns on changes.
*/
package main
import (
l "log"
"os"
"os/exec"
"syscall"
"github.com/mitranim/gg"
)
var (
log = l.New(os.Stderr, `[gow] `, 0)
cwd = gg.Cwd()
)
func main() {
var main Main
defer main.Exit()
defer main.Deinit()
main.Init()
main.Run()
}
type Main struct {
Opt Opt
Cmd Cmd
Stdio Stdio
Watcher Watcher
TermState TermState
Sig Sig
ChanRestart gg.Chan[struct{}]
ChanKill gg.Chan[syscall.Signal]
}
func (self *Main) Init() {
self.Opt.Init(os.Args[1:])
self.ChanRestart.Init()
self.ChanKill.Init()
self.Cmd.Init(self)
self.Sig.Init(self)
self.WatchInit()
self.TermState.Init(self)
self.Stdio.Init(self)
}
/*
We MUST call this before exiting because:
* We modify global OS state: terminal, subprocs.
* OS will NOT auto-cleanup after us.
Otherwise:
* Terminal is left in unusable state.
* Subprocs become orphan daemons.
We MUST call this manually before using `syscall.Kill` or `syscall.Exit` on the
current process. Syscalls terminate the process bypassing Go `defer`.
*/
func (self *Main) Deinit() {
self.Stdio.Deinit()
self.TermState.Deinit()
self.WatchDeinit()
self.Sig.Deinit()
self.Cmd.Deinit()
}
func (self *Main) Run() {
go self.Stdio.Run()
go self.Sig.Run()
go self.WatchRun()
self.CmdRun()
}
func (self *Main) WatchInit() {
wat := new(WatchNotify)
wat.Init(self)
self.Watcher = wat
}
func (self *Main) WatchDeinit() {
if self.Watcher != nil {
self.Watcher.Deinit()
self.Watcher = nil
}
}
func (self *Main) WatchRun() {
if self.Watcher != nil {
self.Watcher.Run()
}
}
func (self *Main) CmdRun() {
if !self.Opt.Postpone {
self.Cmd.Restart()
}
for {
select {
case <-self.ChanRestart:
self.Opt.TermInter()
self.Cmd.Restart()
case sig := <-self.ChanKill:
self.Terminate(sig)
return
}
}
}
func (self *Main) CmdWait(cmd *exec.Cmd) {
self.Opt.LogSubErr(cmd.Wait())
self.Opt.TermSuf()
}
// Must be deferred.
func (self *Main) Exit() {
err := gg.AnyErrTraced(recover())
if err != nil {
self.Opt.LogErr(err)
os.Exit(1)
}
os.Exit(0)
}
func (self *Main) OnFsEvent(event FsEvent) {
if !self.ShouldRestart(event) {
return
}
if self.Opt.Verb {
log.Println(`restarting on FS event:`, event)
}
self.Restart()
}
func (self *Main) ShouldRestart(event FsEvent) bool {
return event != nil &&
!(self.Opt.Lazy && self.Cmd.IsRunning()) &&
self.Opt.AllowPath(event.Path())
}
func (self *Main) Restart() { self.ChanRestart.SendZeroOpt() }
func (self *Main) Kill(val syscall.Signal) { self.ChanKill.SendOpt(val) }
func (self *Main) Terminate(sig syscall.Signal) {
/**
This should terminate any descendant processes, using their default behavior
for the given signal. If any misbehaving processes do not terminate on a
kill signal, this is out of our hands for now. We could use SIGKILL to
ensure termination, but it's unclear if we should.
*/
self.Cmd.Broadcast(sig)
/**
This should restore previous terminal state and un-register our custom signal
handling.
*/
self.Deinit()
/**
Re-send the signal after un-registering our signal handling. If our process is
still running by the time the signal is received, the signal will be handled
by the Go runtime, using the default behavior. Most of the time, this signal
should not be received because after calling this method, we also return
from the main function.
*/
gg.Nop1(syscall.Kill(os.Getpid(), sig))
}