forked from flipt-io/flipt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
268 lines (211 loc) · 5.53 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
//go:build mage
// +build mage
package main
import (
"context"
"fmt"
"os"
"path"
"dagger.io/dagger"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"go.flipt.io/flipt/build/internal"
"go.flipt.io/flipt/build/internal/publish"
"go.flipt.io/flipt/build/release"
"go.flipt.io/flipt/build/testing"
"golang.org/x/mod/modfile"
"golang.org/x/sync/errgroup"
)
// Build is a collection of targets which build Flipt into target Docker images.
type Build mg.Namespace
// Flipt builds a development version of Flipt as a Docker image and loads it into a local Docker instance.
func (b Build) Flipt(ctx context.Context) error {
client, err := daggerClient(ctx)
if err != nil {
return err
}
defer client.Close()
platform, err := client.DefaultPlatform(ctx)
if err != nil {
return err
}
req, err := newRequest(ctx, client, platform)
if err != nil {
return err
}
base, err := internal.Base(ctx, client, req)
if err != nil {
return err
}
flipt, err := internal.Package(ctx, client, base, req)
if err != nil {
return err
}
out, err := sh.Output("git", "rev-parse", "HEAD")
if err != nil {
return err
}
ref, err := publish.Publish(ctx, publish.PublishSpec{
TargetType: publish.LocalTargetType,
Target: "flipt:dev-" + out[:7],
}, client, publish.Variants{
platform: flipt,
})
if err != nil {
return err
}
fmt.Println("Successfully Built Flipt:", ref)
return nil
}
// Base builds Flipts base image via Dagger and buildkit.
// This can be used for debugging or cache warming.
// There is no resulting artefact (only buildkit cache state).
func (b Build) Base(ctx context.Context) error {
client, err := daggerClient(ctx)
if err != nil {
return err
}
defer client.Close()
platform, err := client.DefaultPlatform(ctx)
if err != nil {
return err
}
req, err := newRequest(ctx, client, platform)
if err != nil {
return err
}
base, err := internal.Base(ctx, client, req)
if err != nil {
return err
}
_, err = base.ExitCode(ctx)
return err
}
// Test contains all the targets used to test the Flipt base container.
type Test mg.Namespace
// All runs Flipt's unit test suite against all the databases Flipt supports.
func (t Test) All(ctx context.Context) error {
var g errgroup.Group
for db := range testing.All {
db := db
g.Go(func() error {
return t.Database(ctx, db)
})
}
return g.Wait()
}
// Unit runs the base suite of tests for all of Flipt.
// It uses SQLite as the default database.
func (t Test) Unit(ctx context.Context) error {
client, err := daggerClient(ctx)
if err != nil {
return err
}
defer client.Close()
platform, err := client.DefaultPlatform(ctx)
if err != nil {
return err
}
req, err := newRequest(ctx, client, platform)
if err != nil {
return err
}
base, err := internal.Base(ctx, client, req)
if err != nil {
return err
}
return testing.Unit(ctx, client, base)
}
// Database runs the unit test suite against the desired database (one of ["sqlite" "postgres" "mysql" "cockroach"]).
func (t Test) Database(ctx context.Context, db string) error {
client, err := daggerClient(ctx)
if err != nil {
return err
}
defer client.Close()
platform, err := client.DefaultPlatform(ctx)
if err != nil {
return err
}
req, err := newRequest(ctx, client, platform)
if err != nil {
return err
}
base, err := internal.Base(ctx, client, req)
if err != nil {
return err
}
return testing.Unit(testing.All[db](ctx, client, base))
}
// Integration runs the entire integration test suite.
// The suite runs a number of operations via the Go SDK against Flipt
// in various configurations using both HTTP and GRPC.
func (t Test) Integration(ctx context.Context) error {
client, err := daggerClient(ctx)
if err != nil {
return err
}
defer client.Close()
platform, err := client.DefaultPlatform(ctx)
if err != nil {
return err
}
req, err := newRequest(ctx, client, platform)
if err != nil {
return err
}
base, err := internal.Base(ctx, client, req)
if err != nil {
return err
}
flipt, err := internal.Package(ctx, client, base, req)
if err != nil {
return err
}
return testing.Integration(ctx, client, base, flipt)
}
type Release mg.Namespace
func (r Release) Next(ctx context.Context, module, versionParts string) error {
return release.Next(module, versionParts)
}
func (r Release) Latest(ctx context.Context, module string) error {
return release.Latest(module, false)
}
func (r Release) LatestRC(ctx context.Context, module string) error {
return release.Latest(module, true)
}
func (r Release) Changelog(ctx context.Context, module, version string) error {
return release.UpdateChangelog(module, version)
}
func (r Release) Tag(ctx context.Context, module, version string) error {
return release.Tag(ctx, module, version)
}
func daggerClient(ctx context.Context) (*dagger.Client, error) {
return dagger.Connect(ctx,
dagger.WithWorkdir(workDir()),
dagger.WithLogOutput(os.Stdout),
)
}
func newRequest(ctx context.Context, client *dagger.Client, platform dagger.Platform) (internal.FliptRequest, error) {
ui, err := internal.UI(ctx, client)
if err != nil {
return internal.FliptRequest{}, err
}
// write contents of container dist/ directory to the host
dist := ui.Directory("./dist")
return internal.NewFliptRequest(dist, platform, internal.WithWorkDir(workDir())), nil
}
func workDir() string {
curDir, err := os.Getwd()
if err != nil {
panic(err)
}
mod, err := os.ReadFile(path.Join(curDir, "go.mod"))
if err != nil {
panic(err)
}
if modfile.ModulePath(mod) == "go.flipt.io/flipt/build" {
return ".."
}
return "."
}