-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo.go
560 lines (480 loc) · 15.7 KB
/
go.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
package duat
// This file contains methods for Go builds using the duat conventions
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"go/ast"
"go/parser"
"go/token"
"github.com/go-stack/stack" // Forked copy of https://github.com/go-stack/stack
"github.com/jjeffery/kv" // Forked copy of https://github.com/jjeffery/kv
fileio "github.com/karlmutch/duat/pkg/fileio"
)
var (
goModTidy = "go mod tidy"
)
func init() {
goRoot := os.Getenv("GOROOT")
if len(goRoot) != 0 {
goModTidy = filepath.Join(goRoot, "bin", "go mod tidy")
}
}
// Look for directories inside the root 'dir' and return their paths, skip any vendor directories
//
func findDirs(dir string) (dirs []string, err kv.Error) {
dirs = []string{}
errGo := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
return nil
}
if strings.HasPrefix(path, "vendor/") || info.Name() == "vendor" || info.Name() == ".git" {
return filepath.SkipDir
}
dirs = append(dirs, path)
return nil
})
if errGo != nil {
return nil, kv.Wrap(errGo).With("dir", dir).With("stack", stack.Trace().TrimRuntime())
}
return dirs, err
}
func FindGoDirs(dir string, funcs []string) (dirs []string, err kv.Error) {
dirs = []string{}
found, err := findDirs(dir)
if err != nil {
return []string{}, err
}
groomed, err := FindPossibleGoFuncs(funcs, found, []string{})
if err != nil {
return []string{}, err
}
for _, dir := range groomed {
dirs = append(dirs, filepath.Dir(dir))
}
return dirs, nil
}
func FindGoFiles(dir string) (files []string, err kv.Error) {
files = []string{}
if stat, errGo := os.Stat(dir); errGo != nil {
return files, kv.Wrap(errGo).With("dir", dir).With("stack", stack.Trace().TrimRuntime())
} else {
if !stat.IsDir() {
if filepath.Ext(stat.Name()) == ".go" {
files = append(files, dir)
} else {
filepath.Walk(dir, func(path string, f os.FileInfo, errGo error) error {
if !f.IsDir() {
if filepath.Ext(f.Name()) == ".go" {
files = append(files, f.Name())
}
}
return nil
})
}
}
return files, nil
}
}
func GoFileTags(fn string, tags []string) (tagsSatisfied bool) {
file, errGo := os.Open(fn)
if errGo != nil {
return false
}
defer file.Close()
scan := bufio.NewScanner(file)
scan.Scan()
tokens := strings.Split(scan.Text(), " ")
if tokens[0] != "//" {
return true
}
fileTags := map[string]struct{}{}
for _, token := range tokens[1:] {
if len(token) != 0 {
fileTags[token] = struct{}{}
}
}
if _, isPresent := fileTags["+build"]; !isPresent {
return true
}
delete(fileTags, "+build")
for _, aTag := range tags {
delete(fileTags, aTag)
}
return len(fileTags) == 0
}
// FindGoFunc will locate a function or method within a directory of source files.
// Use "receiver.func" for methods, a function name without the dot for functions.
//
func FindGoFuncIn(funcName string, dir string, tags []string) (file string, err kv.Error) {
fs := token.NewFileSet()
pkgs, errGo := parser.ParseDir(fs, dir,
func(fi os.FileInfo) (isOK bool) {
return GoFileTags(filepath.Join(dir, fi.Name()), tags)
}, 0)
if errGo != nil {
return file, kv.Wrap(errGo).With("dir", dir).With("stack", stack.Trace().TrimRuntime())
}
for _, pkg := range pkgs {
ast.Inspect(pkg, func(n ast.Node) bool {
if fun, ok := n.(*ast.FuncDecl); ok {
name := fun.Name.Name
if fun.Recv != nil {
for _, v := range fun.Recv.List {
switch xv := v.Type.(type) {
case *ast.StarExpr:
if si, ok := xv.X.(*ast.Ident); ok {
name = fmt.Sprintf("%s.%s", si.Name, fun.Name)
}
case *ast.Ident:
name = fmt.Sprintf("%s.%s", xv.Name, fun.Name)
}
}
}
if name == funcName {
file = fs.Position(fun.Name.NamePos).Filename
return false
}
}
return true
})
}
return file, nil
}
// FindGoGenerateFiles is used to descend recursively into a directory to locate go generate
// files
//
func FindGoGenerateFiles(dir string, tags []string) (genFiles []string, err kv.Error) {
genFiles = []string{}
foundDirs := map[string]struct{}{}
dirs := []string{}
walker := func(path string, info os.FileInfo, err error) error {
if info == nil || !info.IsDir() {
return nil
}
dirs = append(dirs, path)
return nil
}
if errGo := filepath.Walk(dir, walker); errGo != nil {
return genFiles, kv.Wrap(errGo).With("dir", dir).With("stack", stack.Trace().TrimRuntime())
}
for _, dir := range dirs {
// Avoid adding duplicates
if _, isPresent := foundDirs[dir]; isPresent {
continue
}
fs := token.NewFileSet()
pkgs, errGo := parser.ParseDir(fs, dir,
func(fi os.FileInfo) (isOK bool) {
return GoFileTags(filepath.Join(dir, fi.Name()), tags)
}, parser.ParseComments)
if errGo != nil {
return genFiles, kv.Wrap(errGo).With("dir", dir).With("stack", stack.Trace().TrimRuntime())
}
func() {
for _, pkg := range pkgs {
for fn, f := range pkg.Files {
for _, group := range f.Comments {
for _, comment := range group.List {
pos := fs.PositionFor(comment.Pos(), true)
if pos.Line == 1 && pos.Column == 1 {
if strings.HasPrefix(comment.Text, "go:generate") || strings.HasPrefix(comment.Text, "//go:generate") {
genFiles = append(genFiles, fn)
}
}
}
}
}
}
}()
}
return genFiles, nil
}
// FindGoGenerateDirs is used to locate and directories that contain
// go files that have well formed fo generate directives. The directories
func FindGoGenerateDirs(dirs []string, tags []string) (genFiles []string, err kv.Error) {
genFiles = []string{}
foundFiles := map[string]struct{}{}
for _, dir := range dirs {
files, err := FindGoGenerateFiles(dir, tags)
if err != nil {
return nil, err
}
if len(files) == 0 {
continue
}
for _, file := range files {
// Avoid duplicates
if _, isPresent := foundFiles[file]; !isPresent {
foundFiles[file] = struct{}{}
genFiles = append(genFiles, file)
}
}
}
return genFiles, nil
}
// FindPossibleGoFuncs can be used to hunt down directories where there was a function found
// that matches one of the specifications of the user, or if as a result of an error during
// checking we might not be sure that the function does not exist
//
func FindPossibleGoFuncs(names []string, dirs []string, tags []string) (possibles []string, err kv.Error) {
possibles = []string{}
files := map[string]struct{}{}
for _, dir := range dirs {
for _, name := range names {
// Some what inefficent as we are scanning the dir
// potentially multiple times
file, err := FindGoFuncIn(name, dir, tags)
if err == nil && len(file) == 0 {
continue
}
// Avoid duplicates
if _, isPresent := files[file]; !isPresent {
files[file] = struct{}{}
possibles = append(possibles, file)
}
}
}
return possibles, nil
}
func (md *MetaData) GoBuild(tags []string, opts []string, outputDir string, outputSuffix string, versionBump bool) (outputs []string, err kv.Error) {
// Dont do any version manipulation if we are just preparing images
// As we begin the build determine if we are using a pre-released version
// and if so automatically bump the pre-release version to reflect a development
// step
if versionBump {
if len(md.SemVer.Prerelease()) != 0 {
if _, err = md.BumpPrerelease(); err != nil {
return outputs, err
}
}
}
if outputs, err = md.GoSimpleBuild(tags, opts, outputDir, outputSuffix); err != nil {
return []string{}, err
}
return outputs, nil
}
func runCMD(cmds []string, logOut io.Writer, logErr io.Writer) (err kv.Error) {
cmd := exec.Command("bash", "-c", strings.Join(cmds, " && "))
cmd.Stdout = logOut
cmd.Stderr = logErr
if errGo := cmd.Start(); errGo != nil {
dir, _ := os.Getwd()
fmt.Fprintln(os.Stderr, kv.Wrap(errGo, "unable to run the compiler").
With("stack", stack.Trace().TrimRuntime()).With("cmds", strings.Join(cmds, "¶ ")).
With("dir", dir).Error())
os.Exit(-3)
}
if errGo := cmd.Wait(); errGo != nil {
return kv.Wrap(errGo, "unable to run the compiler").
With("stack", stack.Trace().TrimRuntime()).With("cmds", strings.Join(cmds, "¶ "))
}
return nil
}
// GoGenerate will invoke the go generator. This method must resort to using the
// command line exec as there is no public library within the go code base that
// exposes the go generator
//
func (md *MetaData) GoGenerate(file string, env map[string]string, tags []string, opts []string) (outputs []string, err kv.Error) {
outputs = []string{}
buildEnv := make([]string, 0, len(env))
for k, v := range env {
buildEnv = append(buildEnv, fmt.Sprintf("%s='%s'", k, v))
}
tagOption := ""
if len(tags) > 0 {
tagOption = fmt.Sprintf(" -tags \"%s\" ", strings.Join(tags, " "))
}
cmds := []string{
goModTidy,
fmt.Sprintf("%s go generate %s %s %s",
strings.Join(buildEnv, " "), file, strings.Join(opts, " "), tagOption),
}
outBuf := &strings.Builder{}
if err = runCMD(cmds, io.MultiWriter(os.Stdout, outBuf), io.MultiWriter(os.Stderr, outBuf)); err != nil {
outputs = append(outputs, strings.Split(outBuf.String(), "\n")...)
return outputs, err.With("module", md.Module).With("file", file)
}
return outputs, nil
}
func (md *MetaData) GoSimpleBuild(tags []string, opts []string, outputDir string, outputSuffix string) (outputs []string, err kv.Error) {
outputs = []string{}
// Copy the compiled file into the GOPATH bin directory
goPath := os.Getenv("GOPATH")
if len(goPath) == 0 {
return outputs, kv.NewError("unable to determine the compiler bin output dir, env var GOPATH might be missing or empty").With("stack", stack.Trace().TrimRuntime())
}
if len(outputDir) == 0 {
outputDir = "./bin"
}
if outputs, err = md.GoCompile(map[string]string{}, tags, opts, outputDir, outputSuffix); err != nil {
return outputs, err
}
// Any executable binaries are copied into your $GOPATH/bin automatically
if errGo := os.MkdirAll(filepath.Join(goPath, "bin"), os.ModePerm); errGo != nil {
if !os.IsExist(errGo) {
return outputs, kv.Wrap(errGo, "unable to create the $GOPATH/bin directory").With("stack", stack.Trace().TrimRuntime())
}
}
// Find any executables we have and copy them to the gopath bin directory as well
binPath, errGo := filepath.Abs(filepath.Join(".", "bin"))
if errGo != nil {
return outputs, kv.Wrap(errGo, "unable to copy binary files from the ./bin directory").With("stack", stack.Trace().TrimRuntime())
}
errGo = filepath.Walk(binPath, func(path string, f os.FileInfo, err error) error {
if f.IsDir() {
return nil
}
// Is the file executable at all ?
if f.Mode()&0111 != 0 {
src := filepath.Join("bin", f.Name())
dst := filepath.Join(goPath, "bin", filepath.Base(f.Name()))
if err := fileio.CopyFile(src, dst); err != nil {
return err
}
outputs = append(outputs, dst)
}
return nil
})
if errGo == nil {
return outputs, nil
}
return outputs, errGo.(kv.Error)
}
func (md *MetaData) GoFetchBuilt() (outputs []string, err kv.Error) {
outputs = []string{}
binPath, errGo := filepath.Abs(filepath.Join(".", "bin"))
if errGo != nil {
return outputs, kv.Wrap(errGo, "unable to find binary files").With("stack", stack.Trace().TrimRuntime())
}
// Nothing to be found which is a valid condition
if fi, err := os.Stat(binPath); err != nil || !fi.IsDir() {
return outputs, nil
}
errGo = filepath.Walk(binPath, func(path string, f os.FileInfo, err error) error {
if f.IsDir() {
return nil
}
// Is the file executable at all ?
if f.Mode()&0111 != 0 {
outputs = append(outputs, filepath.Join(binPath, f.Name()))
}
return nil
})
if errGo == nil {
return outputs, nil
}
return outputs, errGo.(kv.Error)
}
func (md *MetaData) GoCompile(env map[string]string, tags []string, opts []string, outputDir string, outputSuffix string) (outputs []string, err kv.Error) {
if errGo := os.Mkdir("bin", os.ModePerm); errGo != nil {
if !os.IsExist(errGo) {
return outputs, kv.Wrap(errGo, "unable to create the bin directory").With("stack", stack.Trace().TrimRuntime())
}
}
// prepare flags and options needed for the actual build
ldFlags := []string{}
ldFlags = append(ldFlags, fmt.Sprintf("-X github.com/karlmutch/duat/version.BuildTime=%s", time.Now().Format("2006-01-02_15:04:04-0700")))
ldFlags = append(ldFlags, fmt.Sprintf("-X github.com/karlmutch/duat/version.GitHash=%s", md.Git.Hash))
ldFlags = append(ldFlags, fmt.Sprintf("-X github.com/karlmutch/duat/version.SemVer=\"%s\"", md.SemVer.String()))
buildOS, hasOS := os.LookupEnv("GOOS")
arch, hasArch := os.LookupEnv("GOARCH")
if !hasOS {
buildOS = runtime.GOOS
}
if !hasArch {
arch = runtime.GOARCH
}
if arch == "arm" {
if arm, isPresent := os.LookupEnv("GOARM"); isPresent {
arch += arm
}
}
buildEnv := []string{"GO_ENABLED=0"}
// If the LD_LIBRARY_PATH is present bring it into the build automatically
if ldPath, hasLdPath := os.LookupEnv("LD_LIBRARY_PATH"); hasLdPath {
buildEnv = append(buildEnv, fmt.Sprintf("LD_LIBRARY_PATH=%s", ldPath))
}
for k, v := range env {
buildEnv = append(buildEnv, fmt.Sprintf("%s='%s'", k, v))
switch k {
case "GOOS":
buildOS = v
case "GOARCH":
arch = v
}
}
goPath, isPresent := os.LookupEnv("GOPATH")
if !isPresent {
goPath, _ = os.LookupEnv("HOME")
}
trimpath := ""
if len(goPath) != 0 {
trimpath = "-gcflags \"all=-trimpath=" + goPath + "\""
}
output := md.Module + "-" + buildOS + "-" + arch
if len(outputSuffix) != 0 {
output += "-" + outputSuffix
}
output = filepath.Join(outputDir, output)
tagOption := ""
if len(tags) > 0 {
tagOption = fmt.Sprintf(" -tags \"%s\" ", strings.Join(tags, " "))
}
cmds := []string{
goModTidy,
fmt.Sprintf(("%s go build %s %s %s -ldflags \"" + strings.Join(ldFlags, " ") + "\" -o " + output + " ."),
strings.Join(buildEnv, " "), strings.Join(opts, " "), trimpath, tagOption),
}
outBuf := &strings.Builder{}
if err = runCMD(cmds, io.MultiWriter(os.Stdout, outBuf), io.MultiWriter(os.Stderr, outBuf)); err != nil {
outputs = append(outputs, strings.Split(outBuf.String(), "\n")...)
return outputs, err.With("module", md.Module)
}
return outputs, nil
}
func (md *MetaData) GoTest(env map[string]string, tags []string, opts []string) (err kv.Error) {
// prepare flags and options needed for the actual build
ldFlags := []string{}
ldFlags = append(ldFlags, fmt.Sprintf("-X github.com/karlmutch/duat/version.BuildTime=%s", time.Now().Format("2006-01-02_15:04:04-0700")))
ldFlags = append(ldFlags, fmt.Sprintf("-X github.com/karlmutch/duat/version.GitHash=%s", md.Git.Hash))
ldFlags = append(ldFlags, fmt.Sprintf("-X github.com/karlmutch/duat/version.SemVer=\"%s\"", md.SemVer.String()))
buildEnv := []string{"GO_ENABLED=0"}
// If the LD_LIBRARY_PATH is present bring it into the build automatically
if ldPath, hasLdPath := os.LookupEnv("LD_LIBRARY_PATH"); hasLdPath {
buildEnv = append(buildEnv, fmt.Sprintf("LD_LIBRARY_PATH=%s", ldPath))
}
for k, v := range env {
buildEnv = append(buildEnv, fmt.Sprintf("%s='%s'", k, v))
}
tagOption := ""
if len(tags) > 0 {
tagOption = fmt.Sprintf(" -tags \"%s\" ", strings.Join(tags, " "))
}
cmds := []string{
goModTidy,
fmt.Sprintf(("%s go test %s -ldflags \"" + strings.Join(ldFlags, " ") + "\" %s ."),
strings.Join(buildEnv, " "), tagOption, strings.Join(opts, " ")),
}
cmd := exec.Command("bash", "-c", strings.Join(cmds, " && "))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if errGo := cmd.Start(); errGo != nil {
dir, _ := os.Getwd()
fmt.Fprintln(os.Stderr, kv.Wrap(errGo, "unable to run the test").With("module", md.Module).
With("stack", stack.Trace().TrimRuntime()).With("cmds", strings.Join(cmds, "¶ ")).
With("dir", dir).Error())
os.Exit(-3)
}
if errGo := cmd.Wait(); errGo != nil {
return kv.Wrap(errGo, "unable to run the compiler").
With("stack", stack.Trace().TrimRuntime()).With("cmds", strings.Join(cmds, "¶ "))
}
return nil
}