-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
116 lines (97 loc) · 2.46 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
//go:build mage
package main
import (
"os"
"github.com/imdario/mergo"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
const (
binPath = "./bin"
projectName = "deckr"
ldFlags = "-s -w"
tags = "jsoniter"
opts = "-trimpath"
)
var defaultEnvs = map[string]string{
"CGO_ENABLED": "0",
}
func init() {
os.Setenv("GO111MODULE", "on")
}
// Run tests
func Test() error {
return sh.Run("ginkgo", "run", "./...")
}
// Run tests with race detector
func TestRace() error {
return sh.Run("ginkgo", "run", "--race", "./...")
}
// Run go mod tidy
func Tidy() error {
return sh.Run("go", "mod", "tidy")
}
// Run golangci linters
func Lint() error {
return sh.Run("golangci-lint", "run", "./...", "--fast")
}
// Generate stubs from proto files
func Proto() error {
return sh.Run("buf", "generate")
}
type Build mg.Namespace
// Builds for all supported popular OS/Arch
func (b Build) All() error {
mg.Deps(
b.LinuxAmd64,
b.LinuxArm64,
b.MacOSAmd64,
b.MacOSArm64,
b.WinAmd64,
)
return nil
}
// Builds for Linux 64bit
func (Build) LinuxAmd64() error {
env := map[string]string{
"GOOS": "linux",
"GOARCH": "amd64",
}
return sh.RunWith(flagEnvs(env), "go", "build", opts, "-tags", tags, "-ldflags", ldFlags, "-o", binPath+"/"+projectName+"-linux-amd64")
}
// Builds for Linux ARM 64bit
func (Build) LinuxArm64() error {
env := map[string]string{
"GOOS": "linux",
"GOARCH": "arm64",
}
return sh.RunWith(flagEnvs(env), "go", "build", opts, "-tags", tags, "-ldflags", ldFlags, "-o", binPath+"/"+projectName+"-linux-arm64")
}
// Builds for MacOS 64bit
func (Build) MacOSAmd64() error {
env := map[string]string{
"GOOS": "darwin",
"GOARCH": "amd64",
}
return sh.RunWith(flagEnvs(env), "go", "build", opts, "-tags", tags, "-ldflags", ldFlags, "-o", binPath+"/"+projectName+"-macos-amd64")
}
// Builds for MacOS M1
func (Build) MacOSArm64() error {
env := map[string]string{
"GOOS": "darwin",
"GOARCH": "arm64",
}
return sh.RunWith(flagEnvs(env), "go", "build", opts, "-tags", tags, "-ldflags", ldFlags, "-o", binPath+"/"+projectName+"-macos-arm64")
}
// Builds for Windows 64bit
func (Build) WinAmd64() error {
env := map[string]string{
"GOOS": "windows",
"GOARCH": "amd64",
}
return sh.RunWith(flagEnvs(env), "go", "build", opts, "-tags", tags, "-ldflags", ldFlags, "-o", binPath+"/"+projectName+"-win-amd64.exe")
}
func flagEnvs(env map[string]string) map[string]string {
mergo.Merge(&env, defaultEnvs)
return env
}