-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
173 lines (134 loc) · 3.53 KB
/
build.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
package k6exec
import (
"context"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
"syscall"
"github.com/grafana/k6build"
"github.com/grafana/k6build/pkg/client"
"github.com/grafana/k6build/pkg/local"
"github.com/grafana/k6deps"
)
const (
platform = runtime.GOOS + "/" + runtime.GOARCH
k6module = "k6"
)
func depsConvert(deps k6deps.Dependencies) (string, []k6build.Dependency) {
bdeps := make([]k6build.Dependency, 0, len(deps))
k6constraint := "*"
for _, dep := range deps {
if dep.Name == k6module {
k6constraint = dep.GetConstraints().String()
continue
}
bdeps = append(bdeps, k6build.Dependency{Name: dep.Name, Constraints: dep.GetConstraints().String()})
}
return k6constraint, bdeps
}
func build(ctx context.Context, deps k6deps.Dependencies, opts *Options) (*url.URL, error) {
svc, err := newBuildService(ctx, opts)
if err != nil {
return nil, err
}
k6constraints, bdeps := depsConvert(deps)
artifact, err := svc.Build(ctx, platform, k6constraints, bdeps)
if err != nil {
return nil, err
}
return url.Parse(artifact.URL)
}
func newBuildService(ctx context.Context, opts *Options) (k6build.BuildService, error) {
if opts != nil && opts.BuildServiceURL != nil {
return newBuildServiceClient(opts)
}
return newLocalBuildService(ctx, opts)
}
func newLocalBuildService(ctx context.Context, opts *Options) (k6build.BuildService, error) {
statedir, err := opts.stateSubdir()
if err != nil {
return nil, err
}
catfile := filepath.Join(statedir, "catalog.json")
client, err := opts.client()
if err != nil {
return nil, err
}
if err := download(ctx, opts.extensionCatalogURL(), catfile, client); err != nil {
return nil, err
}
cachedir, err := opts.cacheDir()
if err != nil {
return nil, err
}
conf := local.BuildServiceConfig{
BuildEnv: map[string]string{"GOWORK": "off"},
Catalog: catfile,
CopyGoEnv: true,
CacheDir: filepath.Join(cachedir, "build"),
Verbose: slog.Default().Enabled(ctx, slog.LevelDebug),
}
return local.NewBuildService(ctx, conf)
}
func newBuildServiceClient(opts *Options) (k6build.BuildService, error) {
return client.NewBuildServiceClient(client.BuildServiceClientConfig{
URL: opts.BuildServiceURL.String(),
})
}
//nolint:forbidigo
func download(ctx context.Context, from *url.URL, dest string, client *http.Client) error {
tmp, err := os.CreateTemp(filepath.Dir(dest), filepath.Base(dest)+"-*")
if err != nil {
return err
}
if from.Scheme == "file" {
err = fileDownload(from, tmp)
} else {
err = httpDownload(ctx, from, tmp, client)
}
if err != nil {
_ = tmp.Close()
_ = os.Remove(tmp.Name())
return err
}
if err = tmp.Close(); err != nil {
return err
}
err = os.Chmod(tmp.Name(), syscall.S_IRUSR|syscall.S_IXUSR|syscall.S_IWUSR)
if err != nil {
return err
}
return os.Rename(tmp.Name(), dest)
}
//nolint:forbidigo
func fileDownload(from *url.URL, dest *os.File) error {
src, err := os.Open(from.Path)
if err != nil {
return err
}
defer src.Close() //nolint:errcheck
_, err = io.Copy(dest, src)
return err
}
//nolint:forbidigo
func httpDownload(ctx context.Context, from *url.URL, dest *os.File, client *http.Client) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, from.String(), nil)
if err != nil {
return err
}
resp, err := client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("%w: %s", os.ErrNotExist, resp.Status)
}
defer resp.Body.Close() //nolint:errcheck
_, err = io.Copy(dest, resp.Body)
return err
}