-
Notifications
You must be signed in to change notification settings - Fork 12
/
magefile.go
595 lines (468 loc) · 12.6 KB
/
magefile.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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
//go:build mage
// +build mage
package main
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/mt-sre/go-ci/command"
"github.com/mt-sre/go-ci/container"
"github.com/mt-sre/go-ci/file"
"github.com/mt-sre/go-ci/git"
"github.com/mt-sre/go-ci/web"
"go.uber.org/multierr"
)
var _depBin = filepath.Join(_dependencyDir, "bin")
var _dependencyDir = func() string {
if dir, ok := os.LookupEnv("DEPENDENCY_DIR"); ok {
return dir
}
return filepath.Join(_projectRoot, ".cache", "dependencies")
}()
var _projectRoot = func() string {
if root, ok := os.LookupEnv("PROJECT_ROOT"); ok {
return root
}
topLevel, err := git.RevParse(context.Background(),
git.WithRevParseFormat(git.RevParseFormatTopLevel),
)
if err != nil {
panic(err)
}
return topLevel
}()
var _tag = func() string {
short, err := git.RevParse(context.Background(),
git.WithRevParseFormat(git.RevParseFormatShort),
)
if err != nil {
panic(err)
}
return short
}()
var Aliases = map[string]interface{}{
"check": All.Check,
"clean": All.Clean,
"release": Release.CLI,
"run-hooks": Hooks.Run,
"test": All.Test,
}
type All mg.Namespace
func (All) Check(ctx context.Context) {
mg.SerialCtxDeps(
ctx,
Check.Tidy,
Check.Verify,
Check.Lint,
)
}
func (All) Clean(ctx context.Context) {
mg.CtxDeps(
ctx,
Build.CleanCLI,
Test.Clean,
)
}
func (All) Test(ctx context.Context) {
mg.CtxDeps(
ctx,
Test.Unit,
Test.Integration,
)
}
type Check mg.Namespace
func (Check) Tidy(ctx context.Context) error {
tidy := gocmd(
command.WithArgs{"mod", "tidy"},
command.WithConsoleOut(mg.Verbose()),
command.WithContext{Context: ctx},
)
if err := tidy.Run(); err != nil {
return fmt.Errorf("starting tidy: %w", err)
}
if tidy.Success() {
return nil
}
return fmt.Errorf("running tidy: %w", tidy.Error())
}
func (Check) Verify(ctx context.Context) error {
verify := gocmd(
command.WithArgs{"mod", "verify"},
command.WithConsoleOut(mg.Verbose()),
command.WithContext{Context: ctx},
)
if err := verify.Run(); err != nil {
return fmt.Errorf("starting verification: %w", err)
}
if verify.Success() {
return nil
}
return fmt.Errorf("running verification: %w", verify.Error())
}
func (Check) Lint(ctx context.Context) error {
mg.CtxDeps(ctx, Deps.UpdateGolangCILint)
lint := golangci(
command.WithArgs{"run",
"--timeout=10m",
"-E", "unused,gofmt,goimports,gosimple,staticcheck",
"--skip-dirs-use-default",
"--verbose",
},
command.WithContext{Context: ctx},
)
if err := lint.Run(); err != nil {
return fmt.Errorf("starting linter: %w", err)
}
fmt.Fprint(os.Stdout, lint.CombinedOutput())
if lint.Success() {
return nil
}
return fmt.Errorf("running linter: %w", lint.Error())
}
var golangci = command.NewCommandAlias(filepath.Join(_depBin, "golangci-lint"))
type Test mg.Namespace
func (Test) Unit(ctx context.Context) error {
test := gocmd(
command.WithCurrentEnv(true),
command.WithEnv{
"CGO_CFLAGS": "-DSQLITE_ENABLE_JSON1",
},
command.WithArgs{
"test", "-v", "-tags=unit",
"-cover", "-count=1", "-race", "-timeout", "15m", "./...",
},
command.WithConsoleOut(mg.Verbose()),
command.WithContext{Context: ctx},
)
if err := test.Run(); err != nil {
return fmt.Errorf("starting unit tests: %w", err)
}
if test.Success() {
return nil
}
return fmt.Errorf("running unit tests: %w", test.Error())
}
// Runs integration tests.
func (Test) Integration(ctx context.Context) error {
mg.CtxDeps(
ctx,
Deps.UpdateGinkgo,
)
test := ginkgo(
command.WithArgs{
"-r",
"--randomize-all",
"--randomize-suites",
"--fail-on-pending",
"--keep-going",
"--race",
"--trace",
},
command.WithArgs{"-v", "integration"},
command.WithConsoleOut(mg.Verbose()),
command.WithContext{Context: ctx},
)
if err := test.Run(); err != nil {
return fmt.Errorf("starting integration tests: %w", err)
}
if test.Success() {
return nil
}
return fmt.Errorf("running integration tests: %w", test.Error())
}
var ginkgo = command.NewCommandAlias(filepath.Join(_depBin, "ginkgo"))
func (Test) Benchmark(ctx context.Context) error {
benchmark := gocmd(
command.WithCurrentEnv(true),
command.WithArgs{
"test", "-bench=.", "-count=5",
"-run", `"Benchmark*"`, "./...",
},
command.WithConsoleOut(mg.Verbose()),
command.WithContext{Context: ctx},
)
if err := benchmark.Run(); err != nil {
return fmt.Errorf("starting benchmark tests: %w", err)
}
if benchmark.Success() {
return nil
}
return fmt.Errorf("running benchmark tests: %w", benchmark.Error())
}
func (Test) Clean() error {
files, err := file.Find(_projectRoot,
file.WithEntType(file.EntTypeDir),
file.WithName("index_tmp_*"),
)
if err != nil {
return err
}
var errCollector error
for _, f := range files {
multierr.AppendInto(&errCollector, sh.Rm(f))
}
return errCollector
}
const repository = "quay.io/mtsre/addon-metadata-operator"
type Build mg.Namespace
func (Build) CLI(ctx context.Context) error {
build := gocmd(
command.WithCurrentEnv(true),
command.WithEnv{
"CGO_ENABLED": "1",
"CGO_CFLAGS": "-DSQLITE_ENABLE_JSON1",
},
command.WithArgs{
"build", "-a",
"-o", filepath.Join(_projectRoot, "bin", "mtcli"),
filepath.Join("cmd", "mtcli", "main.go"),
},
command.WithConsoleOut(mg.Verbose()),
command.WithContext{Context: ctx},
)
if err := build.Run(); err != nil {
return fmt.Errorf("starting to build mtcli: %w", err)
}
if build.Success() {
return nil
}
return fmt.Errorf("building mtcli: %w", build.Error())
}
func (Build) CleanCLI() error {
return sh.Rm(filepath.Join(_projectRoot, "bin", "mtcli"))
}
var gocmd = command.NewCommandAlias(mg.GoCmd())
type Generate mg.Namespace
func (Generate) Boilerplate(ctx context.Context) error {
mg.CtxDeps(ctx,
Deps.UpdateControllerGen,
Generate.Clean,
)
generate := controllergen(
command.WithArgs{
`object:headerFile="hack/boilerplate.go.txt"`,
`paths="./api/..."`,
`paths="./pkg/..."`,
},
command.WithConsoleOut(mg.Verbose()),
command.WithContext{Context: ctx},
)
if err := generate.Run(); err != nil {
return fmt.Errorf("starting to generate boilerplate: %w", err)
}
if generate.Success() {
return nil
}
return fmt.Errorf("generating boilerplate: %w", generate.Error())
}
var controllergen = command.NewCommandAlias(filepath.Join(_depBin, "controller-gen"))
func (Generate) Clean(ctx context.Context) error {
genFiles, err := file.Find(_projectRoot,
file.WithEntType(file.EntTypeFile),
file.WithName("zz_generated.deepcopy.go"),
)
if err != nil {
return fmt.Errorf("finding generated files: %w", err)
}
var finalErr error
for _, f := range genFiles {
if err := sh.Rm(f); err != nil {
multierr.AppendInto(&finalErr, fmt.Errorf("removing file %q: %w", f, err))
}
}
return finalErr
}
type Release mg.Namespace
func (Release) CLI(ctx context.Context) error {
return runGoreleaser(ctx)
}
func (Release) CLISnapshot(ctx context.Context) error {
return runGoreleaser(ctx, "--snapshot")
}
func runGoreleaser(ctx context.Context, args ...string) error {
mg.CtxDeps(
ctx,
Release.container,
)
runtime, ok := container.Runtime()
if !ok {
return errors.New("could not find container runtime")
}
run := command.NewCommand(runtime,
command.WithArgs{
"run", "--rm",
"-e", "CGO_ENABLED=1",
"-e", "CGO_CFLAGS=-DSQLITE_ENABLE_JSON1",
"-e", fmt.Sprintf("GITHUB_TOKEN=%s", os.Getenv("GITHUB_TOKEN")),
fmt.Sprintf("amo-release:%s", _tag), "release",
},
command.WithArgs(args),
command.WithConsoleOut(mg.Verbose()),
command.WithContext{Context: ctx},
)
if err := run.Run(); err != nil {
return fmt.Errorf("starting to run goreleaser: %w", err)
}
if run.Success() {
return nil
}
return fmt.Errorf("running goreleaser: %w", run.Error())
}
func (Release) container(ctx context.Context) error {
runtime, ok := container.Runtime()
if !ok {
return errors.New("could not find container runtime")
}
build := command.NewCommand(runtime,
command.WithArgs{
"build",
"-t", fmt.Sprintf("amo-release:%s", _tag),
"-f", "Dockerfile.release", _projectRoot,
},
command.WithConsoleOut(mg.Verbose()),
command.WithContext{Context: ctx},
)
if err := build.Run(); err != nil {
return fmt.Errorf("starting to build goreleaser: %w", err)
}
if build.Success() {
return nil
}
return fmt.Errorf("building goreleaser: %w", build.Error())
}
type Deps mg.Namespace
func (Deps) UpdateControllerGen(ctx context.Context) {
mg.CtxDeps(ctx, mg.F(Deps.updateGoDependency, "sigs.k8s.io/controller-tools/cmd/controller-gen"))
}
func (Deps) UpdateGolangCILint(ctx context.Context) {
mg.CtxDeps(ctx, mg.F(Deps.updateGoDependency, "github.com/golangci/golangci-lint/cmd/golangci-lint"))
}
func (Deps) UpdateGinkgo(ctx context.Context) {
mg.CtxDeps(ctx, mg.F(Deps.updateGoDependency, "github.com/onsi/ginkgo/v2/ginkgo"))
}
func (Deps) updateGoDependency(ctx context.Context, src string) error {
if err := setupDepsBin(); err != nil {
return fmt.Errorf("creating dependencies bin directory: %w", err)
}
toolsDir := filepath.Join(_projectRoot, "tools")
tidy := gocmd(
command.WithArgs{"mod", "tidy"},
command.WithWorkingDirectory(toolsDir),
command.WithConsoleOut(mg.Verbose()),
command.WithContext{Context: ctx},
)
if err := tidy.Run(); err != nil {
return fmt.Errorf("starting to tidy tools dir: %w", err)
}
if !tidy.Success() {
return fmt.Errorf("tidying tools dir: %w", tidy.Error())
}
install := gocmd(
command.WithArgs{"install", src},
command.WithWorkingDirectory(toolsDir),
command.WithCurrentEnv(true),
command.WithEnv{"GOBIN": _depBin},
command.WithConsoleOut(mg.Verbose()),
command.WithContext{Context: ctx},
)
if err := install.Run(); err != nil {
return fmt.Errorf("starting to install command from source %q: %w", src, err)
}
if !install.Success() {
return fmt.Errorf("installing command from source %q: %w", src, install.Error())
}
return nil
}
func (Deps) UpdatePreCommit(ctx context.Context) error {
if err := setupDepsBin(); err != nil {
return fmt.Errorf("creating dependencies bin directory: %w", err)
}
const urlPrefix = "https://github.com/pre-commit/pre-commit/releases/download"
// pinning to version 2.17.0 since 2.18.0+ requires python>=3.7
const version = "2.17.0"
out := filepath.Join(_depBin, "pre-commit")
if _, err := os.Stat(out); err != nil {
if !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("inspecting output location %q: %w", out, err)
}
if err := web.DownloadFile(ctx, urlPrefix+fmt.Sprintf("/v%s/pre-commit-%s.pyz", version, version), out); err != nil {
return fmt.Errorf("downloading pre-commit: %w", err)
}
}
return os.Chmod(out, 0775)
}
func setupDepsBin() error {
return os.MkdirAll(_depBin, 0o774)
}
type Hooks mg.Namespace
func (Hooks) Enable(ctx context.Context) error {
mg.CtxDeps(ctx, Deps.UpdatePreCommit)
install := precommit(
command.WithArgs{"install"},
command.WithConsoleOut(mg.Verbose()),
command.WithContext{Context: ctx},
)
if err := install.Run(); err != nil {
return fmt.Errorf("starting to enable hooks: %w", err)
}
if install.Success() {
return nil
}
return fmt.Errorf("enabling hooks: %w", install.Error())
}
func (Hooks) Disable(ctx context.Context) error {
mg.CtxDeps(ctx, Deps.UpdatePreCommit)
uninstall := precommit(
command.WithArgs{"uninstall"},
command.WithConsoleOut(mg.Verbose()),
command.WithContext{Context: ctx},
)
if err := uninstall.Run(); err != nil {
return fmt.Errorf("starting to disable hooks: %w", err)
}
if uninstall.Success() {
return nil
}
return fmt.Errorf("disabling hooks: %w", uninstall.Error())
}
func (Hooks) Run(ctx context.Context) error {
mg.CtxDeps(ctx, Deps.UpdatePreCommit)
run := precommit(
command.WithArgs{
"run",
"--show-diff-on-failure",
"--from-ref", "origin/master", "--to-ref", "HEAD",
},
command.WithConsoleOut(mg.Verbose()),
command.WithContext{Context: ctx},
)
if err := run.Run(); err != nil {
return fmt.Errorf("starting to run hooks: %w", err)
}
if run.Success() {
return nil
}
return fmt.Errorf("running hooks: %w", run.Error())
}
func (Hooks) RunAllFiles(ctx context.Context) error {
mg.CtxDeps(ctx, Deps.UpdatePreCommit)
runAll := precommit(
command.WithArgs{
"run", "--all-files",
},
command.WithConsoleOut(mg.Verbose()),
command.WithContext{Context: ctx},
)
if err := runAll.Run(); err != nil {
return fmt.Errorf("starting to run hooks for all files: %w", err)
}
if runAll.Success() {
return nil
}
return fmt.Errorf("running hooks for all files: %w", runAll.Error())
}
var precommit = command.NewCommandAlias(filepath.Join(_depBin, "pre-commit"))