This repository has been archived by the owner on Jan 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmagefile.go
107 lines (87 loc) · 2.2 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
//go:build mage
// +build mage
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/mt-sre/devkube/magedeps"
)
// Directories
var (
// Working directory of the project.
workDir string
// Cache directory for temporary build files.
cacheDir string
// Dependency directory.
depsDir magedeps.DependencyDirectory
)
func init() {
var err error
// Directories
workDir, err = os.Getwd()
if err != nil {
panic(fmt.Errorf("getting work dir: %w", err))
}
depsDir = magedeps.DependencyDirectory(filepath.Join(workDir, ".deps"))
cacheDir = filepath.Join(workDir, ".cache")
// Path
os.Setenv("PATH", depsDir.Bin()+":"+os.Getenv("PATH"))
}
// Runs go mod tidy in all go modules of the repository.
func Tidy() error {
if err := sh.Run("go", "mod", "tidy"); err != nil {
return fmt.Errorf("tidy main module: %w", err)
}
return nil
}
// Testing and Linting
// -------------------
type Test mg.Namespace
// Runs unittests.
func (Test) Unit() error {
return sh.RunWithV(
map[string]string{"CGO_ENABLED": "1"},
"go", "test", "-v",
fmt.Sprintf("-coverprofile=%s", filepath.Join(cacheDir, "cov.out")), "-race",
"./dev/...", "./cmd/...", "./magedeps/...",
)
}
// Lints the source code.
func (Test) Lint() error {
mg.Deps(Dependency.GolangciLint)
for _, cmd := range [][]string{
{"go", "fmt", "./..."},
{"golangci-lint", "run", "./...", "--deadline=15m"},
} {
if err := sh.RunV(cmd[0], cmd[1:]...); err != nil {
return fmt.Errorf("running %q: %w", strings.Join(cmd, " "), err)
}
}
return nil
}
// Runs integration tests
func (Test) Integration() error {
return sh.RunWithV(map[string]string{
"CGO_ENABLED": "1",
}, "go", "test", "-cover", "-v", "-race", "./integration/...")
}
// Dependencies
// ------------
// Versions
const (
goimportsVersion = "0.11.1"
golangciLintVersion = "1.53.3"
)
type Dependency mg.Namespace
func (Dependency) Goimports() error {
return depsDir.GoInstall("go-imports",
"golang.org/x/tools/cmd/goimports", goimportsVersion)
}
func (Dependency) GolangciLint() error {
return depsDir.GoInstall("golangci-lint",
"github.com/golangci/golangci-lint/cmd/golangci-lint", golangciLintVersion)
}