-
Notifications
You must be signed in to change notification settings - Fork 218
/
magefile.go
368 lines (298 loc) · 8.76 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
//go:build mage
// +build mage
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/fatih/color"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
var (
tools = []string{
"github.com/bufbuild/buf/cmd/buf",
"github.com/bufbuild/buf/cmd/protoc-gen-buf-breaking",
"github.com/bufbuild/buf/cmd/protoc-gen-buf-lint",
"github.com/golangci/golangci-lint/cmd/golangci-lint",
"github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway",
"github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2",
"golang.org/x/tools/cmd/goimports",
"github.com/rakyll/gotest",
"google.golang.org/grpc/cmd/protoc-gen-go-grpc",
"google.golang.org/protobuf/cmd/protoc-gen-go",
"../internal/cmd/protoc-gen-go-flipt-sdk/...",
}
Default = Build
)
// Installs tools required for development
func Bootstrap() error {
fmt.Println("Bootstrapping tools...")
if err := os.MkdirAll("_tools", 0755); err != nil {
return fmt.Errorf("creating dir: %w", err)
}
// create module if go.mod doesnt exist
if _, err := os.Stat("_tools/go.mod"); os.IsNotExist(err) {
cmd := exec.Command("go", "mod", "init", "tools")
cmd.Dir = "_tools"
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err
}
}
install := []string{"install", "-v"}
install = append(install, tools...)
cmd := exec.Command("go", install...)
cmd.Dir = "_tools"
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
// Builds the project similar to a release build
func Build() error {
mg.Deps(Prep)
fmt.Println("Building...")
if err := build(buildModeProd); err != nil {
return err
}
fmt.Println("Done.")
fmt.Printf("\nRun the following to start Flipt:\n")
fmt.Printf("\n%v\n", color.CyanString(`./bin/flipt [--config config/local.yml]`))
return nil
}
// Cleans up built files
func Clean() error {
fmt.Println("Cleaning...")
if err := sh.RunV("go", "mod", "tidy"); err != nil {
return fmt.Errorf("tidying go.mod: %w", err)
}
clean := []string{"dist/*", "pkg/*", "bin/*", "ui/dist"}
for _, dir := range clean {
if err := os.RemoveAll(dir); err != nil {
return fmt.Errorf("removing dir %q: %w", dir, err)
}
}
return nil
}
// Prepares the project for building
func Prep() error {
fmt.Println("Preparing...")
mg.Deps(Clean)
mg.Deps(UI.Build)
return nil
}
type buildMode uint8
const (
// buildModeDev builds the project for development, without bundling assets
buildModeDev buildMode = iota
// BuildModeProd builds the project similar to a release build
buildModeProd
)
func build(mode buildMode) error {
buildDate := time.Now().UTC().Format(time.RFC3339)
buildArgs := make([]string, 0)
switch mode {
case buildModeProd:
buildArgs = append(buildArgs, "-tags", "assets")
}
gitCommit, err := sh.Output("git", "rev-parse", "HEAD")
if err != nil {
return fmt.Errorf("getting git commit: %w", err)
}
buildArgs = append([]string{"build", "-trimpath", "-ldflags", fmt.Sprintf("-X main.commit=%s -X main.date=%s", gitCommit, buildDate)}, buildArgs...)
buildArgs = append(buildArgs, "-o", "./bin/flipt", "./cmd/flipt/")
return sh.RunV("go", buildArgs...)
}
type Go mg.Namespace
// Keeping these aliases for backwards compatibility for now
var Aliases = map[string]interface{}{
"dev": Go.Run,
"test": Go.Test,
"bench": Go.Bench,
"lint": Go.Lint,
"fmt": Go.Fmt,
"proto": Go.Proto,
"ui:dev": UI.Run,
}
// Runs Go benchmarking tests
func (g Go) Bench() error {
fmt.Println("Running benchmarks...")
if err := sh.RunV("go", "test", "-run", "XXX", "-bench", ".", "-benchmem", "-short", "./..."); err != nil {
return err
}
fmt.Println("Done.")
return nil
}
// Runs the Go server in development mode using the local config, without bundling assets
func (g Go) Run() error {
return sh.RunV("go", "run", "./cmd/flipt/...", "--config", "config/local.yml")
}
// Builds the Go server for development, without bundling assets
func (g Go) Build() error {
mg.Deps(Clean)
fmt.Println("Building...")
if err := build(buildModeDev); err != nil {
return err
}
fmt.Println("Done.")
fmt.Printf("\nRun the following to start Flipt server:\n")
fmt.Printf("\n%v\n", color.CyanString(`./bin/flipt [--config config/local.yml]`))
fmt.Printf("\nIn another shell, run the following to start the UI in dev mode:\n")
fmt.Printf("\n%v\n", color.CyanString(`cd ui && npm run dev`))
return nil
}
// Runs the Go tests and generates a coverage report
func (g Go) Cover() error {
mg.Deps(Go.Test)
fmt.Println("Running coverage...")
return sh.RunV("go", "tool", "cover", "-html=coverage.txt")
}
var ignoreFmt = []string{"rpc/", "sdk/"}
// Formats Go code
func (g Go) Fmt() error {
fmt.Println("Formatting...")
files, err := findFilesRecursive(func(path string, _ os.FileInfo) bool {
// only go files, ignoring generated files
for _, dir := range ignoreFmt {
if filepath.HasPrefix(path, dir) {
return false
}
}
return filepath.Ext(path) == ".go"
})
if err != nil {
return fmt.Errorf("finding files: %w", err)
}
args := append([]string{"-w"}, files...)
return sh.RunV("goimports", args...)
}
// Runs the Go linters
func (g Go) Lint() error {
fmt.Println("Linting...")
if err := sh.RunV("golangci-lint", "run"); err != nil {
return fmt.Errorf("linting: %w", err)
}
return sh.RunV("buf", "lint")
}
// Generates the Go protobuf files and gRPC stubs
func (g Go) Proto() error {
mg.Deps(Bootstrap)
fmt.Println("Generating proto files...")
return sh.RunV("buf", "generate")
}
// Runs the Go unit tests
func (g Go) Test() error {
fmt.Println("Testing...")
env := map[string]string{
"FLIPT_TEST_DATABASE_PROTOCOL": "sqlite3",
}
if os.Getenv("FLIPT_TEST_DATABASE_PROTOCOL") != "" {
env["FLIPT_TEST_DATABASE_PROTOCOL"] = os.Getenv("FLIPT_TEST_DATABASE_PROTOCOL")
}
var (
testCmd = "gotest"
testArgs = []string{}
)
// check if gotest is on path and use that instead for better output
// https://github.com/rakyll/gotest
if _, err := exec.LookPath("gotest"); err != nil {
testCmd = "go"
testArgs = append(testArgs, "test")
}
testArgs = append(testArgs, []string{"-v", "-covermode=atomic", "-count=1", "-coverprofile=coverage.txt", "-timeout=60s", "-coverpkg=./..."}...)
if os.Getenv("FLIPT_TEST_SHORT") != "" {
testArgs = append(testArgs, "-short")
}
testArgs = append(testArgs, "./...")
return sh.RunWithV(env, testCmd, testArgs...)
}
type UI mg.Namespace
// Installs UI dependencies
func (u UI) Deps() error {
fmt.Println("Installing UI deps...")
// check if node_modules exists, if not run npm ci and return
if _, err := os.Stat("ui/node_modules"); err != nil && os.IsNotExist(err) {
cmd := exec.Command("npm", "ci")
cmd.Dir = "ui"
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
// only run if deps have changed
// uses: https://github.com/thdk/package-changed
cmd := exec.Command("npx", "--no", "package-changed", "install", "--ci")
cmd.Dir = "ui"
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
// Runs the UI in development mode
func (u UI) Run() error {
mg.Deps(u.Deps)
fmt.Println("Starting UI...")
cmd := exec.Command("npm", "run", "dev")
cmd.Dir = "ui"
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
// Formats UI code
func (u UI) Fmt() error {
fmt.Println("Formatting UI...")
cmd := exec.Command("npm", "run", "format")
cmd.Dir = "ui"
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
// Runs the UI linters
func (u UI) Lint() error {
fmt.Println("Linting UI...")
cmd := exec.Command("npm", "run", "lint")
cmd.Dir = "ui"
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
// Builds all UI assets for release distribution
func (u UI) Build() error {
mg.Deps(u.Deps)
fmt.Println("Generating assets...")
cmd := exec.Command("npm", "run", "build")
cmd.Dir = "ui"
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
type Dagger mg.Namespace
func (d Dagger) Call(command, args string) error {
return sh.RunV("dagger", append([]string{"call", command, "--source=."}, strings.Split(args, " ")...)...)
}
// findFilesRecursive recursively traverses from the CWD and invokes the given
// match function on each regular file to determine if the given path should be
// returned as a match. It ignores files in .git directories.
func findFilesRecursive(match func(path string, info os.FileInfo) bool) ([]string, error) {
var matches []string
err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Don't look for files in git directories
if info.Mode().IsDir() && filepath.Base(path) == ".git" {
return filepath.SkipDir
}
if !info.Mode().IsRegular() {
// continue
return nil
}
if match(filepath.ToSlash(path), info) {
matches = append(matches, path)
}
return nil
})
return matches, err
}