-
Notifications
You must be signed in to change notification settings - Fork 1
/
tool.go
292 lines (269 loc) · 5.89 KB
/
tool.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
package building
import (
"archive/tar"
"bytes"
"flag"
"io"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
"sync"
"syscall"
)
var (
AlpineVersion = "3.8"
containers = flag.Bool("containers", false, "always build in containers")
cross = flag.Bool("cross", false, "build for all platforms (linux, darwin, windows)")
parallel = flag.Bool("parallel", false, "build in parallel")
)
func init() {
// Manual flags parsing to enable containers before any actual work
for _, arg := range os.Args {
if arg == "-containers" {
*containers = true
return
}
}
}
type Tool struct {
root string
name string
url string
dir string
env []string
instructions string
names string
container bool
output io.Writer
input io.Reader
success bool
}
func (t Tool) WithDir(dir string) Tool {
dir = filepath.Clean(dir)
if filepath.IsAbs(dir) {
b.Fatalln("dir must be relative", dir)
}
if strings.Contains(dir, "..") {
b.Fatalln("dir must be a folder under project root", dir)
}
t.dir = dir
return t
}
func (t Tool) WithEnv(env ...string) Tool {
t.env = append(t.env, env...)
return t
}
func (t Tool) WithOutput(w io.Writer) Tool {
t.output = w
return t
}
func (t Tool) WithInput(r io.Reader) Tool {
t.input = r
return t
}
func (t Tool) WithSuccess() Tool {
t.success = true
return t
}
func (t Tool) WithTool(tool Tool) Tool {
t.instructions += "\n" + tool.instructions
if t.container || tool.container {
t.container = true
t.names += "-" + tool.name
t.buildImage()
}
return t
}
func (b *B) MakeTool(name, check, url, instructions string, args ...string) Tool {
t := b.makeTool(name, check, url, instructions)
if len(args) > 0 {
t.Run(args...)
}
return t
}
func (b *B) makeTool(name, check, url, instructions string) Tool {
b.mutex.Lock()
defer b.mutex.Unlock()
if t, ok := b.tools[name]; ok {
return t
}
t := Tool{
root: b.root,
name: name,
url: url,
instructions: instructions,
names: name,
container: noApplication(name, check),
}
if t.container && name != "" && url != "" && !*containers {
b.Print("missing " + name + ": consider installing it to speed up the build, see " + url)
}
if t.container {
t.buildImage()
}
b.tools[name] = t
return t
}
func (b *B) WithOS(f func(goos string)) {
platforms := []string{runtime.GOOS}
if *cross {
platforms = []string{"linux", "darwin", "windows"}
}
wg := sync.WaitGroup{}
for _, goos := range platforms {
b.Println("building for", goos)
if *parallel {
wg.Add(1)
go func(goos string) {
f(goos)
wg.Done()
}(goos)
} else {
f(goos)
}
}
wg.Wait()
}
func noApplication(name, check string) bool {
b.Debugln("checking for", name)
cmd := exec.Command(name)
if check != "" {
cmd = exec.Command(name, check)
}
err := cmd.Run()
if err == nil {
return *containers
}
_, ok := err.(*exec.ExitError)
return !ok
}
func (t Tool) buildImage() {
b.Println("preparing image for", t.name)
buf := &bytes.Buffer{}
tarFile(t.instructions, "Dockerfile", buf)
cmd := exec.Command("docker", "build", "-t", t.image(), "-")
cmd.Stderr = os.Stderr
if isDebug() {
cmd.Stdout = os.Stdout
}
cmd.Stdin = buf
if err := cmd.Run(); err != nil {
b.Fatalln(err)
}
}
func (t Tool) image() string {
if t.root == "" {
b.Fatalln("missing root")
}
return strings.Replace(t.root, "/", "-", -1) + "-build-" + t.names
}
func tarFile(content, filename string, writer io.Writer) {
tw := tar.NewWriter(writer)
hdr := &tar.Header{
Name: filename,
Mode: 0600,
Size: int64(len(content)),
}
if err := tw.WriteHeader(hdr); err != nil {
b.Fatal(err)
}
if _, err := tw.Write([]byte(content)); err != nil {
b.Fatal(err)
}
if err := tw.Close(); err != nil {
b.Fatal(err)
}
}
func (t Tool) Run(args ...string) int {
if t.output == nil {
t.output = os.Stdout
}
if t.input == nil {
t.input = os.Stdin
}
t.print(args)
if t.container {
return t.runContainer(args)
}
return t.runApplication(args)
}
func (t Tool) runApplication(args []string) int {
if t.dir != "" {
err := os.MkdirAll(t.dir, 0755)
if err != nil {
b.Fatal(err)
}
}
cmd := exec.Command(t.name, args...)
cmd.Dir = t.dir
cmd.Env = append(os.Environ(), t.env...)
cmd.Stderr = os.Stderr
cmd.Stdout = t.output
cmd.Stdin = t.input
code, err := run(cmd, t.success)
if err != nil {
b.Fatal(err)
}
return code
}
func (t Tool) print(args []string) {
prefix := "running"
if t.container {
prefix += " [container]"
}
if t.dir != "" {
prefix += " (in " + t.dir + ")"
}
b.Println(prefix, append([]string{t.name}, args...))
}
func (t Tool) runContainer(args []string) int {
// $$$$ MAT error out if docker in windows containers mode
wd, err := os.Getwd()
if err != nil {
b.Fatalln(err)
}
if t.root == "" {
b.Fatalln("missing root")
}
w := path.Join("/go/src", t.root, t.dir)
// $$$$ MAT create w if needed
var envs []string
for _, e := range t.env {
envs = append(envs, "-e", e)
}
// $$$$ MAT use --net=none by default and allow to customize by tool
arg := append([]string{"run", "--rm", "-v", wd + ":" + w, "-w", w, "-i"}, envs...)
arg = append(arg, t.image(), t.name)
// $$$$ MAT try and replace wd in args with w ?
// $$$$ do the same with TEMPDIR -> /tmp, and mount it ? any dir ?
// $$$$ MAT if GOPATH set, mount it instead of wd ?
arg = append(arg, args...)
b.Debugln("running", append([]string{"docker"}, arg...))
cmd := exec.Command("docker", arg...)
cmd.Stderr = os.Stderr
cmd.Stdout = t.output
cmd.Stdin = t.input
code, err := run(cmd, t.success)
if err != nil {
b.Fatalln(err)
}
return code
}
func run(cmd *exec.Cmd, success bool) (int, error) {
if err := cmd.Run(); err != nil {
exit, ok := err.(*exec.ExitError)
if ok {
if success {
err = nil
}
if status, ok := exit.Sys().(syscall.WaitStatus); ok {
return status.ExitStatus(), err
}
}
return 1, err
}
return 0, nil
}