-
Notifications
You must be signed in to change notification settings - Fork 1
/
goup.go
213 lines (182 loc) · 4.16 KB
/
goup.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
// goup watches application dependencies for changes and restarts
// it whenever files change.
package main
import (
"bytes"
"encoding/json"
"go/build"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"syscall"
"github.com/fsnotify/fsnotify"
)
var logger = log.New(os.Stderr, "goup -> ", 0)
var watchOps = []fsnotify.Op{fsnotify.Write, fsnotify.Create, fsnotify.Remove}
var watchExt = []string{".go"}
type project struct {
Name string
Deps []string
Target string
Dir string
cmd *exec.Cmd
restarting bool
watched []string
stdin []byte
}
func main() {
prj, err := read()
if err != nil {
logger.Fatalf("failed import: %v", err)
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
prj.terminate()
os.Exit(0)
}()
watcher, err := fsnotify.NewWatcher()
if err != nil {
logger.Fatalf("failed to create fsnotify watcher: %v", err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event := <-watcher.Events:
if prj.restarting || !validate(event).valid() {
continue
}
prj.restarting = true
logger.Println(event)
go prj.restart()
case err := <-watcher.Errors:
logger.Println("watch error:", err)
}
}
}()
for _, pkgDir := range prj.watched {
if err := watcher.Add(pkgDir); err != nil {
logger.Fatalf("failed to register: %s - %v", pkgDir, err)
}
}
if err := watcher.Add(prj.Dir); err != nil {
logger.Fatalf("failed to register: %s - %v", prj.Dir, err)
}
prj.restarting = true
go prj.restart()
<-done
}
func (p *project) terminate() {
// send termination signal, to running application
if p.cmd != nil && p.cmd.Process != nil {
logger.Println("terminating process:", p.cmd.Process.Pid)
if err := p.cmd.Process.Signal(syscall.SIGTERM); err != nil {
logger.Println("failed to terminate:", p.cmd.Process.Pid, "reason:", err)
}
}
}
func (p *project) restart() {
if p.Name == "main" {
logger.Println("restarting application")
} else {
logger.Println("recompiling package")
}
// use go install, it rebuilds only changed packages
cmd := exec.Command("go", "install")
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
logger.Println("failed to run go install -", err)
p.restarting = false
return // could not recompile due to error, wait for another change
}
if p.Name != "main" {
p.restarting = false
return // nothing to do for a library
}
p.terminate()
var stdin io.Reader = os.Stdin
if len(p.stdin) > 0 {
buf := make([]byte, len(p.stdin))
copy(buf, p.stdin)
stdin = bytes.NewReader(buf)
}
p.cmd = exec.Command(p.Target, os.Args[1:]...)
p.cmd.Stdout = os.Stdout
p.cmd.Stdin = stdin
p.cmd.Stderr = os.Stderr
if err := p.cmd.Start(); err != nil {
logger.Println("failed to run command:", err)
p.restarting = false
return // could not start command
}
p.restarting = false
// wait until terminated
logger.Println("started on PID:", p.cmd.Process.Pid)
p.cmd.Wait()
// @TODO: maybe restart with few retries on error exit codes
}
func read() (*project, error) {
data, err := exec.Command("go", "list", "-json", "-e").Output()
if err != nil {
return nil, err
}
prj := &project{}
if err = json.Unmarshal(data, prj); err != nil {
return nil, err
}
ps := string(os.PathSeparator)
for _, dep := range prj.Deps {
if strings.Index(dep, ps+"vendor"+ps) != -1 {
continue // skip vendor
}
pkg, err := build.Import(dep, prj.Dir, 0)
if err != nil {
continue
}
if pkg.Goroot {
continue
}
prj.watched = append(prj.watched, pkg.Dir)
}
stat, err := os.Stdin.Stat()
if err != nil {
return nil, err
}
if (stat.Mode() & os.ModeCharDevice) == 0 {
prj.stdin, err = ioutil.ReadAll(os.Stdin)
if err != nil {
return nil, err
}
}
return prj, nil
}
type validate fsnotify.Event
func (v validate) valid() bool {
return v.ops() && v.ext()
}
func (v validate) ops() bool {
for _, op := range watchOps {
if v.Op&op == op {
return true
}
}
return false
}
func (v validate) ext() bool {
ext := filepath.Ext(v.Name)
for _, e := range watchExt {
if ext == e {
return true
}
}
return false
}