forked from paketo-buildpacks/go-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
go_build_process.go
142 lines (119 loc) · 3.3 KB
/
go_build_process.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
package gobuild
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"unicode"
"github.com/paketo-buildpacks/packit/v2/chronos"
"github.com/paketo-buildpacks/packit/v2/pexec"
"github.com/paketo-buildpacks/packit/v2/scribe"
)
//go:generate faux --interface Executable --output fakes/executable.go
type Executable interface {
Execute(pexec.Execution) (err error)
}
type GoBuildConfiguration struct {
Workspace string
Output string
GoPath string
GoCache string
Targets []string
Flags []string
DisableCGO bool
}
type GoBuildProcess struct {
executable Executable
logs scribe.Emitter
clock chronos.Clock
}
func NewGoBuildProcess(executable Executable, logs scribe.Emitter, clock chronos.Clock) GoBuildProcess {
return GoBuildProcess{
executable: executable,
logs: logs,
clock: clock,
}
}
func (p GoBuildProcess) Execute(config GoBuildConfiguration) ([]string, error) {
p.logs.Process("Executing build process")
err := os.MkdirAll(config.Output, os.ModePerm)
if err != nil {
return nil, fmt.Errorf("failed to create targets output directory: %w", err)
}
if !containsFlag(config.Flags, "-buildmode") {
config.Flags = append(config.Flags, "-buildmode", "pie")
}
if !containsFlag(config.Flags, "-trimpath") {
config.Flags = append(config.Flags, "-trimpath")
}
args := append([]string{"build", "-o", config.Output}, config.Flags...)
args = append(args, config.Targets...)
env := append(os.Environ(), fmt.Sprintf("GOCACHE=%s", config.GoCache))
if config.GoPath != "" {
env = append(env, fmt.Sprintf("GOPATH=%s", config.GoPath))
}
env = append(env, "GO111MODULE=auto")
if config.DisableCGO {
env = append(env, "CGO_ENABLED=0")
}
printedArgs := []string{"go"}
for _, arg := range args {
printedArgs = append(printedArgs, formatArg(arg))
}
p.logs.Subprocess("Running '%s'", strings.Join(printedArgs, " "))
duration, err := p.clock.Measure(func() error {
return p.executable.Execute(pexec.Execution{
Args: args,
Dir: config.Workspace,
Env: env,
Stdout: p.logs.ActionWriter,
Stderr: p.logs.ActionWriter,
})
})
if err != nil {
p.logs.Action("Failed after %s", duration.Round(time.Millisecond))
return nil, fmt.Errorf("failed to execute 'go build': %w", err)
}
p.logs.Action("Completed in %s", duration.Round(time.Millisecond))
p.logs.Break()
var paths []string
for _, target := range config.Targets {
buffer := bytes.NewBuffer(nil)
err := p.executable.Execute(pexec.Execution{
Args: []string{"list", "--json", target},
Dir: config.Workspace,
Env: env,
Stdout: buffer,
Stderr: buffer,
})
if err != nil {
p.logs.Detail(buffer.String())
return nil, fmt.Errorf("failed to execute 'go list': %w", err)
}
var list struct {
ImportPath string `json:"ImportPath"`
}
err = json.Unmarshal(buffer.Bytes(), &list)
if err != nil {
return nil, fmt.Errorf("failed to parse 'go list' output: %w", err)
}
paths = append(paths, filepath.Join(config.Output, filepath.Base(list.ImportPath)))
}
if len(paths) == 0 {
return nil, errors.New("failed to determine go executable start command")
}
return paths, nil
}
func formatArg(arg string) string {
for _, r := range arg {
if unicode.IsSpace(r) {
return strconv.Quote(arg)
}
}
return arg
}