/
golang.go
443 lines (337 loc) · 10.5 KB
/
golang.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package gomason
import (
"fmt"
"github.com/sirupsen/logrus"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/a8m/envsubst"
"github.com/pkg/errors"
)
func init() {
languagesMap[LanguageGolang] = Golang{}
}
// Golang struct. For golang, workdir is GOPATH
type Golang struct{}
// CreateWorkDir Creates an empty but workable GOPATH in the directory specified. Returns
// the full GOPATH
func (Golang) CreateWorkDir(workDir string) (gopath string, err error) {
gopath = filepath.Join(workDir, "go")
subdirs := []string{
filepath.Join(gopath, "src"),
filepath.Join(gopath, "bin"),
filepath.Join(gopath, "pkg"),
}
for _, dir := range subdirs {
err = os.MkdirAll(dir, 0755)
if err != nil {
err = errors.Wrapf(err, "failed creating directory")
return gopath, err
}
}
return gopath, err
}
// Checkout Actually checks out the code you're trying to test into your temporary GOPATH
func (Golang) Checkout(gopath string, meta Metadata, branch string) (err error) {
err = os.Chdir(gopath)
if err != nil {
err = errors.Wrapf(err, "failed to cwd to %s", gopath)
return err
}
// install the code via go get after all, we don't really want to play if it's not in a repo.
gobinary := "go"
gocommand, err := exec.LookPath(gobinary)
if err != nil {
err = errors.Wrapf(err, "failed to find go binary: %s", gobinary)
return err
}
runenv := append(os.Environ(), fmt.Sprintf("GOPATH=%s", gopath))
runenv = append(runenv, "GO111MODULE=off")
var cmd *exec.Cmd
if meta.InsecureGet {
cmd = exec.Command(gocommand, "get", "-v", "-insecure", meta.Package)
} else {
cmd = exec.Command(gocommand, "get", "-v", "-d", fmt.Sprintf("%s/...", meta.Package))
}
logrus.Debugf("Running %s with GOPATH=%s", cmd.Args, gopath)
cmd.Env = runenv
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err == nil {
logrus.Debugf("Checkout of %s complete", meta.Package)
}
git, err := exec.LookPath("git")
if err != nil {
err := errors.Wrap(err, "Failed to find git executable in path")
return err
}
codepath := filepath.Join(gopath, "src", meta.Package)
err = os.Chdir(codepath)
if err != nil {
err = errors.Wrapf(err, "changing working dir to %q", codepath)
return err
}
if branch != "" {
logrus.Debugf("Checking out branch: %s", branch)
cmd := exec.Command(git, "checkout", branch)
err = cmd.Run()
if err == nil {
logrus.Debugf("Checkout of branch: %s complete.", branch)
}
}
return err
}
// Prep Commands run pre-build/ pre-test the checked out code in your temporary GOPATH
func (Golang) Prep(gopath string, meta Metadata, local bool) (err error) {
logrus.Debug("Running Prep Commands")
var codepath string
if local {
wd, err := os.Getwd()
if err != nil {
err = errors.Wrapf(err, "failed getting CWD")
return err
}
codepath = wd
} else {
codepath = fmt.Sprintf("%s/src/%s", gopath, meta.Package)
err = os.Chdir(codepath)
if err != nil {
err = errors.Wrapf(err, "failed to cwd to %s", gopath)
return err
}
// set the gopath in the environment so that we can interpolate it below
_ = os.Setenv("GOPATH", gopath)
}
for _, cmdString := range meta.BuildInfo.PrepCommands {
// interpolate any environment variables into the command string
cmdString, err = envsubst.String(cmdString)
if err != nil {
err = errors.Wrap(err, "failed to substitute env vars")
return err
}
cmd := exec.Command("bash", "-c", cmdString)
logrus.Debugf("Running %q with GOPATH=%s", cmdString, gopath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
err = errors.Wrapf(err, "failed running %q", cmdString)
return err
}
}
logrus.Debugf("Prep steps for %s complete", meta.Package)
return err
}
// Test Runs 'go test -v ./...' in the checked out code directory
func (Golang) Test(gopath string, gomodule string, timeout string, local bool) (err error) {
if !local {
wd := filepath.Join(gopath, "src", gomodule)
logrus.Debugf("Changing working directory to %s.", wd)
err = os.Chdir(wd)
if err != nil {
err = errors.Wrapf(err, "changing working dir to %q", wd)
return err
}
}
logrus.Debugf("Running 'go test -v ./...'.")
// TODO Should this use a shell exec like build?
var cmd *exec.Cmd
// Things break if you pass in an arg that has an empty string. Splitting it up like this fixes https://github.com/nikogura/gomason/issues/24
if timeout != "" {
cmd = exec.Command("go", "test", "-v", "-timeout", timeout, "./...")
} else {
cmd = exec.Command("go", "test", "-v", "./...")
}
runenv := append(os.Environ(), fmt.Sprintf("GOPATH=%s", gopath))
runenv = append(runenv, "GO111MODULE=on")
cmd.Env = runenv
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err = cmd.Run()
if err != nil {
err = errors.Wrapf(err, "failed running %s", cmd)
}
logrus.Debugf("Done with go test.")
return err
}
// Build uses `gox` to build binaries per metadata file
func (g Golang) Build(gopath string, meta Metadata, skipTargets string, local bool) (err error) {
logrus.Debugf("Checking to see that gox is installed.")
// Install gox if it's not already there
if _, err := os.Stat(filepath.Join(gopath, "bin/gox")); os.IsNotExist(err) {
err = GoxInstall(gopath)
if err != nil {
err = errors.Wrap(err, "Failed to install gox")
return err
}
}
var wd string
if local {
wd, err = os.Getwd()
if err != nil {
err = errors.Wrapf(err, "failed getting CWD")
return err
}
} else {
wd = fmt.Sprintf("%s/src/%s", gopath, meta.Package)
logrus.Debugf("Changing working directory to: %s", wd)
err = os.Chdir(wd)
if err != nil {
err = errors.Wrapf(err, "changing working dir to %q", wd)
return err
}
}
gox := fmt.Sprintf("%s/bin/gox", gopath)
logrus.Debugf("Gox is: %s", gox)
var metadatapath string
if local {
metadatapath = fmt.Sprintf("%s/%s", wd, METADATA_FILENAME)
} else {
metadatapath = fmt.Sprintf("%s/src/%s/%s", gopath, meta.Package, METADATA_FILENAME)
}
md, err := ReadMetadata(metadatapath)
if err != nil {
err = errors.Wrap(err, "Failed to read metadata file from checked out code")
return err
}
skipTargetsMap := make(map[string]int)
if skipTargets != "" {
targetsList := strings.Split(skipTargets, ",")
for _, t := range targetsList {
skipTargetsMap[t] = 1
}
}
for _, target := range md.BuildInfo.Targets {
// skip this target if we're told to do so
_, skip := skipTargetsMap[target.Name]
if skip {
continue
}
logrus.Debugf("Building target: %q in dir %s", target.Name, wd)
// This gets weird because go's exec shell doesn't like the arg format that gox expects
// Building it thusly keeps the various quoting levels straight
runenv := os.Environ()
if !local {
gopathenv := fmt.Sprintf("GOPATH=%s", gopath)
runenv = append(runenv, gopathenv)
}
// allow user to turn off go modules
if !target.Legacy {
runenv = append(runenv, "GO111MODULE=on")
}
cgo := ""
// build with cgo if we're told to do so.
if target.Cgo {
cgo = " -cgo"
}
for k, v := range target.Flags {
runenv = append(runenv, fmt.Sprintf("%s=%s", k, v))
logrus.Debugf("Build Flag: %s=%s", k, v)
}
ldflags := ""
if target.Ldflags != "" {
ldflags = fmt.Sprintf(" -ldflags %q ", target.Ldflags)
logrus.Debugf("LD Flag: %s", ldflags)
}
// Interesting idea, but breaks multiple binary builds such as dbt. To properly implement, we'd have to find and handle each binary instead of relying on the './...'.
//outputTemplate := fmt.Sprintf("%s_{{.OS}}_{{.Arch}}", meta.Name)
//args := gox + cgo + ldflags + ` -osarch="` + target.Name + `"` + ` -output="` + outputTemplate + `"` + " ./..."
args := gox + cgo + ldflags + ` -osarch="` + target.Name + `"` + " ./..."
logrus.Debugf("Running gox with: %s in dir %s", args, wd)
// Calling it through sh makes everything happy
cmd := exec.Command("sh", "-c", args)
cmd.Env = runenv
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err = cmd.Run()
if err != nil {
err = errors.Wrapf(err, "failed building target %s", target.Name)
return err
}
logrus.Debugf("Gox build of target %s complete and successful.", target.Name)
}
err = BuildExtras(md, wd)
if err != nil {
err = errors.Wrapf(err, "Failed to build extras")
return err
}
return err
}
// GoxInstall Installs github.com/mitchellh/gox, the go cross compiler.
func GoxInstall(gopath string) (err error) {
logrus.Debugf("Installing gox with GOPATH=%s, GOBIN=%s/bin", gopath, gopath)
gocommand, err := exec.LookPath("go")
if err != nil {
err = errors.Wrap(err, "Failed to find go binary")
return err
}
cmd := exec.Command(gocommand, "install", "-v", "github.com/mitchellh/gox@latest")
env := append(os.Environ(), fmt.Sprintf("GOPATH=%s", gopath))
env = append(os.Environ(), fmt.Sprintf("GOBIN=%s/bin", gopath))
cmd.Env = env
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
wd, err := os.Getwd()
if err != nil {
err = errors.Wrapf(err, "Error getting current working directory")
return err
}
err = os.Chdir(gopath)
if err != nil {
err = errors.Wrapf(err, "Error changing directory into %s", gopath)
return err
}
err = cmd.Run()
if err != nil {
err = errors.Wrapf(err, "failed installing gox")
return err
}
goxPath := filepath.Join(gopath, "bin/gox")
if _, err := os.Stat(goxPath); os.IsNotExist(err) {
err = errors.New(fmt.Sprintf("Gox still not installed to %s", goxPath))
return err
}
err = os.Chdir(wd)
if err != nil {
err = errors.Wrapf(err, "Error returning to directory %s", wd)
return err
}
return err
}
// BuildExtras builds the extra artifacts specified in the metadata file
func BuildExtras(meta Metadata, workdir string) (err error) {
logrus.Debugf("Building Extra Artifacts")
for _, extra := range meta.BuildInfo.Extras {
templateName := filepath.Join(workdir, extra.Template)
outputFileName := filepath.Join(workdir, extra.FileName)
executable := extra.Executable
logrus.Debugf("Reading template from %s", templateName)
logrus.Debugf("Writing to %s", outputFileName)
var mode os.FileMode
if executable {
mode = 0755
} else {
mode = 0644
}
tmplBytes, err := os.ReadFile(templateName)
if err != nil {
err = errors.Wrapf(err, "failed to read template file %s", templateName)
return err
}
output, err := ParseTemplateForMetadata(string(tmplBytes), meta)
if err != nil {
err = errors.Wrapf(err, "failed to inject metadata into template text")
return err
}
err = os.WriteFile(outputFileName, []byte(output), mode)
if err != nil {
err = errors.Wrapf(err, "failed to write file %s", outputFileName)
return err
}
}
return err
}