-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
546 lines (438 loc) · 11.1 KB
/
main.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
// Go programming language module.
package main
import (
"dagger/go/internal/dagger"
"fmt"
"strings"
"github.com/containerd/platforms"
)
// defaultImageRepository is used when no image is specified.
const defaultImageRepository = "golang"
const workdir = "/work/src"
type Go struct {
Container *dagger.Container
}
func New(
// Version (image tag) to use from the official image repository as a base container.
//
// +optional
version string,
// Custom container to use as a base container.
//
// +optional
container *dagger.Container,
// Disable mounting cache volumes.
//
// +optional
disableCache bool,
) *Go {
if container == nil {
if version == "" {
version = "latest"
}
container = dag.Container().From(fmt.Sprintf("%s:%s", defaultImageRepository, version))
}
m := &Go{
Container: container,
}
if !disableCache {
m = m.
WithModuleCache(dag.CacheVolume("go-mod"), nil, "").
WithBuildCache(dag.CacheVolume("go-build"), nil, "")
}
return m
}
// Set an environment variable.
func (m *Go) WithEnvVariable(
// The name of the environment variable (e.g., "HOST").
name string,
// The value of the environment variable (e.g., "localhost").
value string,
// Replace `${VAR}` or $VAR in the value according to the current environment
// variables defined in the container (e.g., "/opt/bin:$PATH").
//
// +optional
expand bool,
) *Go {
m.Container = m.Container.WithEnvVariable(
name,
value,
dagger.ContainerWithEnvVariableOpts{
Expand: expand,
},
)
return m
}
// Establish a runtime dependency on a service.
func (m *Go) WithServiceBinding(
// A name that can be used to reach the service from the container.
alias string,
// Identifier of the service container.
service *dagger.Service,
) *Go {
m.Container = m.Container.WithServiceBinding(alias, service)
return m
}
// Set GOOS, GOARCH and GOARM environment variables.
func (m *Go) WithPlatform(
// Target platform in "[os]/[platform]/[version]" format (e.g., "darwin/arm64/v7", "windows/amd64", "linux/arm64").
platform dagger.Platform,
) *Go {
if platform == "" {
return m
}
p := platforms.MustParse(string(platform))
m.Container = m.Container.
WithEnvVariable("GOOS", p.OS).
WithEnvVariable("GOARCH", p.Architecture).
With(func(c *dagger.Container) *dagger.Container {
if p.Variant != "" {
return c.WithEnvVariable("GOARM", p.Variant)
}
return c
})
return m
}
// Set CGO_ENABLED environment variable to 1.
func (m *Go) WithCgoEnabled() *Go {
m.Container = m.Container.WithEnvVariable("CGO_ENABLED", "1")
return m
}
// Set CGO_ENABLED environment variable to 0.
func (m *Go) WithCgoDisabled() *Go {
m.Container = m.Container.WithEnvVariable("CGO_ENABLED", "0")
return m
}
// Mount a cache volume for Go module cache.
func (m *Go) WithModuleCache(
cache *dagger.CacheVolume,
// Identifier of the directory to use as the cache volume's root.
//
// +optional
source *dagger.Directory,
// Sharing mode of the cache volume.
//
// +optional
sharing dagger.CacheSharingMode,
) *Go {
m.Container = m.Container.WithMountedCache(
"/go/pkg/mod",
cache,
dagger.ContainerWithMountedCacheOpts{
Source: source,
Sharing: sharing,
},
)
return m
}
// Mount a cache volume for Go build cache.
func (m *Go) WithBuildCache(
cache *dagger.CacheVolume,
// Identifier of the directory to use as the cache volume's root.
//
// +optional
source *dagger.Directory,
// Sharing mode of the cache volume.
//
// +optional
sharing dagger.CacheSharingMode,
) *Go {
m.Container = m.Container.WithMountedCache(
"/root/.cache/go-build",
cache,
dagger.ContainerWithMountedCacheOpts{
Source: source,
Sharing: sharing,
},
)
return m
}
// Run a Go command.
func (m *Go) Exec(
// Arguments to pass to the Go command.
args []string,
// Source directory to mount.
//
// +optional
src *dagger.Directory,
// Target platform in "[os]/[platform]/[version]" format (e.g., "darwin/arm64/v7", "windows/amd64", "linux/arm64").
//
// +optional
platform dagger.Platform,
) *dagger.Container {
if platform != "" {
m = m.WithPlatform(platform)
}
if src != nil {
return m.WithSource(src).Exec(args, dagger.Platform(""))
}
return m.Container.WithExec(args)
}
// Run "go generate" command.
//
// Consult "go help generate" for more information.
func (m *Go) Generate(
// Source directory to mount.
source *dagger.Directory,
// Packages (or files) to run "go generate" on.
//
// +optional
packages []string,
// A regular expression to select directives whose full original source text (excluding any trailing spaces and final newline) matches the expression.
//
// +optional
run string,
// A regular expression to suppress directives whose full original source text (excluding any trailing spaces and final newline) matches the expression.
//
// +optional
skip string,
// TODO: add -v, -n and -x flags
) *dagger.Directory {
return m.WithSource(source).Generate(
packages,
run,
skip,
).Source
}
// Build a binary.
func (m *Go) Build(
// Source directory to mount.
source *dagger.Directory,
// Package to compile.
//
// +optional
pkg string,
// Enable data race detection.
//
// +optional
race bool,
// Arguments to pass on each go tool link invocation.
//
// +optional
ldflags []string,
// A list of additional build tags to consider satisfied during the build.
//
// +optional
tags []string,
// Remove all file system paths from the resulting executable.
//
// +optional
trimpath bool,
// Additional args to pass to the build command.
//
// +optional
rawArgs []string,
// Target platform in "[os]/[platform]/[version]" format (e.g., "darwin/arm64/v7", "windows/amd64", "linux/arm64").
//
// +optional
platform dagger.Platform,
) *dagger.File {
return m.WithSource(source).Build(
pkg,
race,
ldflags,
tags,
trimpath,
rawArgs,
platform,
)
}
// Mount a source directory.
func (m *Go) WithSource(
// Source directory to mount.
source *dagger.Directory,
) *WithSource {
return &WithSource{
Source: source,
Go: m,
}
}
type WithSource struct {
Source *dagger.Directory
// +private
Go *Go
}
func (m *WithSource) Container() *dagger.Container {
return m.Go.Container.
WithWorkdir(workdir).
WithMountedDirectory(workdir, m.Source)
}
// Set an environment variable.
func (m *WithSource) WithEnvVariable(
// The name of the environment variable (e.g., "HOST").
name string,
// The value of the environment variable (e.g., "localhost").
value string,
// Replace `${VAR}` or $VAR in the value according to the current environment
// variables defined in the container (e.g., "/opt/bin:$PATH").
//
// +optional
expand bool,
) *WithSource {
m.Go = m.Go.WithEnvVariable(name, value, expand)
return m
}
// Establish a runtime dependency on a service.
func (m *WithSource) WithServiceBinding(
// A name that can be used to reach the service from the container.
alias string,
// Identifier of the service container.
service *dagger.Service,
) *WithSource {
m.Go = m.Go.WithServiceBinding(alias, service)
return m
}
// Set GOOS, GOARCH and GOARM environment variables.
func (m *WithSource) WithPlatform(
// Target platform in "[os]/[platform]/[version]" format (e.g., "darwin/arm64/v7", "windows/amd64", "linux/arm64").
platform dagger.Platform,
) *WithSource {
m.Go = m.Go.WithPlatform(platform)
return m
}
// Set CGO_ENABLED environment variable to 1.
func (m *WithSource) WithCgoEnabled() *WithSource {
m.Go = m.Go.WithCgoEnabled()
return m
}
// Set CGO_ENABLED environment variable to 0.
func (m *WithSource) WithCgoDisabled() *WithSource {
m.Go = m.Go.WithCgoDisabled()
return m
}
// Mount a cache volume for Go module cache.
func (m *WithSource) WithModuleCache(
cache *dagger.CacheVolume,
// Identifier of the directory to use as the cache volume's root.
//
// +optional
source *dagger.Directory,
// Sharing mode of the cache volume.
//
// +optional
sharing dagger.CacheSharingMode,
) *WithSource {
m.Go = m.Go.WithModuleCache(cache, source, sharing)
return m
}
// Mount a cache volume for Go build cache.
func (m *WithSource) WithBuildCache(
cache *dagger.CacheVolume,
// Identifier of the directory to use as the cache volume's root.
//
// +optional
source *dagger.Directory,
// Sharing mode of the cache volume.
//
// +optional
sharing dagger.CacheSharingMode,
) *WithSource {
m.Go = m.Go.WithBuildCache(cache, source, sharing)
return m
}
// Run a Go command.
func (m *WithSource) WithExec(
// Arguments to pass to the Go command.
args []string,
// TODO: add back the platform argument, but make sure it's not persisted across calls
) *WithSource {
m.Source = m.Exec(args, dagger.Platform("")).Directory(workdir)
return m
}
// Run a Go command.
func (m *WithSource) Exec(
// Arguments to pass to the Go command.
args []string,
// Target platform in "[os]/[platform]/[version]" format (e.g., "darwin/arm64/v7", "windows/amd64", "linux/arm64").
//
// +optional
platform dagger.Platform,
) *dagger.Container {
if platform != "" {
m = m.WithPlatform(platform)
}
return m.Container().WithExec(args)
}
// Run "go generate" command.
//
// Consult "go help generate" for more information.
func (m *WithSource) Generate(
// Packages (or files) to run "go generate" on.
//
// +optional
packages []string,
// A regular expression to select directives whose full original source text (excluding any trailing spaces and final newline) matches the expression.
//
// +optional
run string,
// A regular expression to suppress directives whose full original source text (excluding any trailing spaces and final newline) matches the expression.
//
// +optional
skip string,
// TODO: add -v, -n and -x flags
) *WithSource {
args := []string{"go", "generate"}
if run != "" {
args = append(args, "-run", run)
}
if skip != "" {
args = append(args, "-skip", skip)
}
if len(packages) > 0 {
args = append(args, packages...)
}
return m.WithExec(args)
}
// Compile the packages into a binary.
func (m *WithSource) Build(
// Package to compile.
//
// +optional
pkg string,
// Enable data race detection.
//
// +optional
race bool,
// Arguments to pass on each go tool link invocation.
//
// +optional
ldflags []string,
// A list of additional build tags to consider satisfied during the build.
//
// +optional
tags []string,
// Remove all file system paths from the resulting executable.
//
// +optional
trimpath bool,
// Additional args to pass to the build command.
//
// +optional
rawArgs []string,
// Target platform in "[os]/[platform]/[version]" format (e.g., "darwin/arm64/v7", "windows/amd64", "linux/arm64").
//
// +optional
platform dagger.Platform,
) *dagger.File {
const binaryPath = "/work/out/binary"
args := []string{"go", "build", "-o", binaryPath}
if race {
args = append(args, "-race")
}
if len(ldflags) > 0 {
args = append(args, "-ldflags", strings.Join(ldflags, " "))
}
if len(tags) > 0 {
args = append(args, "-tags", strings.Join(tags, ","))
}
if trimpath {
args = append(args, "-trimpath")
}
if len(rawArgs) > 0 {
args = append(args, rawArgs...)
}
if pkg != "" {
args = append(args, pkg)
}
return m.Exec(args, platform).File(binaryPath)
}