-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
300 lines (235 loc) · 7.42 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
// +build mage
package main
import (
"fmt"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/common-nighthawk/go-figure"
"github.com/fatih/color"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
var (
Default = Build
goFiles = getGoFiles()
goSrcFiles = getGoSrcFiles()
)
var curDir = func() string {
name, _ := os.Getwd()
return name
}()
// Calculate file paths
var toolsBinDir = normalizePath(path.Join(curDir, "tools", "bin"))
func init() {
time.Local = time.UTC
// Add local bin in PATH
err := os.Setenv("PATH", fmt.Sprintf("%s:%s", toolsBinDir, os.Getenv("PATH")))
if err != nil {
panic(err)
}
}
func Build() {
banner := figure.NewFigure("Kingdom", "", true)
banner.Print()
fmt.Println("")
color.Red("# Build Info ---------------------------------------------------------------")
fmt.Printf("Go version : %s\n", runtime.Version())
fmt.Printf("Git revision : %s\n", hash())
fmt.Printf("Git branch : %s\n", branch())
fmt.Printf("Tag : %s\n", tag())
fmt.Println("")
color.Red("# Core packages ------------------------------------------------------------")
mg.SerialDeps(Go.Deps, Go.License, Go.Generate, Go.Format, Go.Import, Go.Lint, Go.Test)
fmt.Println("")
color.Red("# Artifacts ----------------------------------------------------------------")
mg.Deps(Bin.Kingdom)
}
// -----------------------------------------------------------------------------
type Ci mg.Namespace
// Validate circleci configuration file (circleci/config.yml).
func (Ci) Validate() error {
return sh.RunV("circleci-cli", "config", "validate")
}
// execute circleci job build on local.
func (ci Ci) Build() error {
return ci.localExecute("build")
}
func (ci Ci) localExecute(job string) error {
return sh.RunV("circleci-cli", "local", "execute", "--job", job)
}
// -----------------------------------------------------------------------------
type Gen mg.Namespace
// Generate initializers
func (Gen) Wire() {
color.Blue("### Wiring dispatchers")
mustGoGenerate("gRPC", "go.zenithar.org/kingdom/cli/kingdom/internal/dispatchers/grpc")
}
// Generate mocks for tests
func (Gen) Mocks() {
color.Blue("### Mocks")
mustGoGenerate("Repositories", "go.zenithar.org/kingdom/internal/repositories")
mustGoGenerate("Services", "go.zenithar.org/kingdom/internal/services/pkg/...")
}
// Generate mocks for tests
func (Gen) Decorators() {
color.Blue("### Decorators")
mustGoGenerate("Repositories", "go.zenithar.org/kingdom/internal/repositories/pkg/...")
mustGoGenerate("Services", "go.zenithar.org/kingdom/internal/services/pkg/...")
}
// Generate initializers
func (Gen) Migrations() {
color.Blue("### Database migrations")
mustGoGenerate("PostgreSQL", "go.zenithar.org/kingdom/internal/repositories/pkg/postgresql")
}
// Generate protobuf
func (Gen) Protobuf() error {
color.Blue("### Protobuf")
return sh.RunV("prototool", "all", "--fix", "pkg/protocol")
}
// -----------------------------------------------------------------------------
type Go mg.Namespace
// Generate go code
func (Go) Generate() error {
color.Cyan("## Generate code")
mg.SerialDeps(Gen.Protobuf, Gen.Mocks, Gen.Migrations, Gen.Decorators, Gen.Wire)
mustGoGenerate("Stringer", "go.zenithar.org/kingdom/internal/services/internal/...")
return nil
}
// Test run go test
func (Go) Test() error {
color.Cyan("## Running unit tests")
sh.Run("mkdir", "-p", "test-results/junit")
return sh.RunV("gotestsum", "--junitfile", "test-results/junit/unit-tests.xml", "--", "-short", "-race", "-cover", "./...")
}
// Test run go test
func (Go) IntegrationTest() {
color.Cyan("## Running integration tests")
sh.Run("mkdir", "-p", "test-results/junit")
}
// Tidy add/remove depenedencies.
func (Go) Tidy() error {
fmt.Println("## Cleaning go modules")
return sh.RunV("go", "mod", "tidy", "-v")
}
// Deps install dependency tools.
func (Go) Deps() error {
color.Cyan("## Vendoring dependencies")
return sh.RunV("go", "mod", "vendor")
}
// Deps install dependency tools.
func (Go) License() error {
color.Cyan("## Check license")
return sh.RunV("wwhrd", "check")
}
// Format runs gofmt on everything
func (Go) Format() error {
color.Cyan("## Format everything")
args := []string{"-s", "-w"}
args = append(args, goFiles...)
return sh.RunV("gofumpt", args...)
}
// Import runs goimports on everything
func (Go) Import() error {
color.Cyan("## Process imports")
args := []string{"-w"}
args = append(args, goSrcFiles...)
return sh.RunV("goreturns", args...)
}
// Lint run linter.
func (Go) Lint() error {
mg.Deps(Go.Format)
color.Cyan("## Lint go code")
return sh.RunV("golangci-lint", "run")
}
// -----------------------------------------------------------------------------
type Bin mg.Namespace
// Build kingdom microservice
func (Bin) Kingdom() error {
return goBuild("go.zenithar.org/kingdom/cli/kingdom", "kingdom")
}
func goBuild(packageName, out string) error {
fmt.Printf(" > Building %s [%s]\n", out, packageName)
varsSetByLinker := map[string]string{
"go.zenithar.org/kingdom/internal/version.Version": tag(),
"go.zenithar.org/kingdom/internal/version.Revision": hash(),
"go.zenithar.org/kingdom/internal/version.Branch": branch(),
"go.zenithar.org/kingdom/internal/version.BuildUser": os.Getenv("USER"),
"go.zenithar.org/kingdom/internal/version.BuildDate": time.Now().Format(time.RFC3339),
"go.zenithar.org/kingdom/internal/version.GoVersion": runtime.Version(),
}
var linkerArgs string
for name, value := range varsSetByLinker {
linkerArgs += fmt.Sprintf(" -X %s=%s", name, value)
}
linkerArgs = fmt.Sprintf("-s -w %s -extldflags=-Wl,-z,now,-z,relro", linkerArgs)
return sh.Run("go", "build", "-buildmode=pie", "-ldflags", linkerArgs, "-o", fmt.Sprintf("bin/%s", out), packageName)
}
// -----------------------------------------------------------------------------
func getGoFiles() []string {
var goFiles []string
filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if strings.Contains(path, "vendor/") {
return filepath.SkipDir
}
if !strings.HasSuffix(path, ".go") {
return nil
}
goFiles = append(goFiles, path)
return nil
})
return goFiles
}
func getGoSrcFiles() []string {
var goSrcFiles []string
for _, path := range goFiles {
if !strings.HasSuffix(path, "_test.go") {
continue
}
goSrcFiles = append(goSrcFiles, path)
}
return goSrcFiles
}
// tag returns the git tag for the current branch or "" if none.
func tag() string {
s, _ := sh.Output("git", "describe", "--tags")
return s
}
// hash returns the git hash for the current repo or "" if none.
func hash() string {
hash, _ := sh.Output("git", "rev-parse", "--short", "HEAD")
return hash
}
// branch returns the git branch for current repo
func branch() string {
hash, _ := sh.Output("git", "rev-parse", "--abbrev-ref", "HEAD")
return hash
}
func mustStr(r string, err error) string {
if err != nil {
panic(err)
}
return r
}
func mustGoGenerate(txt, name string) {
fmt.Printf(" > %s [%s]\n", txt, name)
err := sh.RunV("go", "generate", name)
if err != nil {
panic(err)
}
}
func runIntegrationTest(txt, name string) {
fmt.Printf(" > %s [%s]\n", txt, name)
err := sh.RunV("gotestsum", "--junitfile", fmt.Sprintf("test-results/junit/integration-%s.xml", strings.ToLower(txt)), name, "--", "-tags=integration", "-race")
if err != nil {
panic(err)
}
}
// normalizePath turns a path into an absolute path and removes symlinks
func normalizePath(name string) string {
absPath := mustStr(filepath.Abs(name))
return absPath
}