/
targets.go
287 lines (252 loc) · 7.46 KB
/
targets.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
package targets
import (
"context"
"fmt"
"github.com/magefile/mage/mg"
)
var (
// MageTargetsRepo is the repository used for importing mage targets.
MageTargetsRepo = "github.com/LUSHDigital/core-mage"
// InstallVolume is used to set what volume to use during the `mage install` target.
InstallVolume string = "${PWD}:/repo"
// InstallWorkDir is used to set what work directory to use during the `mage install` target.
InstallWorkDir string = "/repo"
)
// Setup performs setup of the project according to your magefile configuration.
func Setup(ctx context.Context) error {
if err := setupReadme(); err != nil {
return err
}
if err := setupDocker(); err != nil {
return err
}
if err := setupGitlab(); err != nil {
return err
}
if err := setupInfra(); err != nil {
return err
}
if err := setupGit(); err != nil {
return err
}
if err := setupService(); err != nil {
return err
}
return nil
}
// Dev is the namespace for actions related to the development environment.
type Dev mg.Namespace
// Start starts the development environment in docker compose
func (Dev) Start(ctx context.Context) error {
arg := BuildDockerComposeArgs(ProjectName, ProjectType, "dev", DockerComposeDevFile)
arg = append(arg, "up", "-d")
arg = append(arg, DockerComposeDevDependencies...)
return Exec(ComposeBin, arg...)
}
// Stop stops the development environment in docker compose
func (Dev) Stop(ctx context.Context) error {
arg := BuildDockerComposeArgs(ProjectName, ProjectType, "dev", DockerComposeDevFile)
arg = append(arg, "stop")
arg = append(arg, DockerComposeDevDependencies...)
return Exec(ComposeBin, arg...)
}
// Restart restarts the development environment in docker compose
func (Dev) Restart(ctx context.Context) {
mg.SerialCtxDeps(ctx,
Dev.Stop,
Dev.Start,
)
}
// Reset returns the development environment to its orginal state
func (Dev) Reset(ctx context.Context) error {
arg := BuildDockerComposeArgs(ProjectName, ProjectType, "test", DockerComposeDevFile)
arg = append(arg, "down")
return Exec(ComposeBin, arg...)
}
// Run runs the service inside docker compose
func (Dev) Run(ctx context.Context) error {
arg := BuildDockerComposeArgs(ProjectName, ProjectType, "dev", DockerComposeDevFile)
arg = append(arg, "run")
arg = append(arg,
"--rm",
"--service-ports",
"--use-aliases",
)
arg = append(arg, "app", "go", "run", "-mod=vendor", "./service")
return Exec(ComposeBin, arg...)
}
// Build compiles the project inside a docker container
func Build(ctx context.Context) error {
return Exec(DockerBin, "build", "-q", "--pull", ".")
}
// Tests is the namespace for actions related to the test environment.
type Tests mg.Namespace
// Start starts the test environment in docker compose
func (Tests) Start(ctx context.Context) error {
arg := BuildDockerComposeArgs(ProjectName, ProjectType, "test", DockerComposeTestFile)
arg = append(arg, "up", "-d")
arg = append(arg, DockerComposeTestDependencies...)
return Exec(ComposeBin, arg...)
}
// Stop stops the test environment in docker compose
func (Tests) Stop(ctx context.Context) error {
arg := BuildDockerComposeArgs(ProjectName, ProjectType, "test", DockerComposeTestFile)
arg = append(arg, "stop")
arg = append(arg, DockerComposeTestDependencies...)
return Exec(ComposeBin, arg...)
}
// Restart restarts the test environment in docker compose
func (Tests) Restart(ctx context.Context) {
mg.SerialCtxDeps(ctx,
Tests.Stop,
Tests.Start,
)
}
// Reset returns the testing environment to its orginal state
func (Tests) Reset(ctx context.Context) error {
arg := BuildDockerComposeArgs(ProjectName, ProjectType, "test", DockerComposeTestFile)
arg = append(arg, "down")
return Exec(ComposeBin, arg...)
}
// Run runs the project tests inside docker compose
func (Tests) Run(ctx context.Context) error {
arg := BuildDockerComposeArgs(ProjectName, ProjectType, "test", DockerComposeTestFile)
arg = append(arg, "run")
arg = append(arg,
"--rm",
"--use-aliases",
)
arg = append(arg, "app", "go", "test", "-mod=vendor", "-v", "-cover")
if err := Exec(ComposeBin, append(arg, "./service")...); err != nil {
return err
}
if err := Exec(ComposeBin, append(arg, "./...")...); err != nil {
return err
}
return nil
}
// Protos is the namespace for actions related to generating protobuffers.
type Protos mg.Namespace
// Add adds the protos submodule to the repository
func (Protos) Add(ctx context.Context) error {
if err := addProtosSubmodule(); err != nil {
return err
}
return nil
}
// Remove removes the protos submodule from the repository
func (Protos) Remove(ctx context.Context) error {
if err := removeProtosSubmodule(); err != nil {
return err
}
return nil
}
// Update updates the protos submodule in the repository
func (Protos) Update(ctx context.Context) error {
if err := updateProtosSubmodule(); err != nil {
return err
}
mg.CtxDeps(ctx, Protos.Generate)
return nil
}
// Generate generates codes from the protos submodule into the repository
func (Protos) Generate(ctx context.Context) error {
if err := genProtos(); err != nil {
return err
}
return nil
}
// Mod is the namespace for actions related to installing modules.
type Mod mg.Namespace
// Core installs or upgrades all core packages
func (Mod) Core() error {
libs := []string{
"github.com/LUSHDigital/core-lush",
"github.com/LUSHDigital/core",
"github.com/LUSHDigital/spew",
}
return goget(libs...)
}
// Uuid installs or upgrades the uuid package
func (Mod) Uuid() error {
libs := []string{
"github.com/LUSHDigital/core",
"github.com/LUSHDigital/spew",
}
return goget(libs...)
}
// Mysql installs or upgrades the mysql packages
func (Mod) Mysql() error {
libs := []string{
"github.com/LUSHDigital/core-sql",
"github.com/LUSHDigital/scan",
"github.com/go-sql-driver/mysql",
}
return goget(libs...)
}
// Postgres installs or upgrades the postgres packages
func (Mod) Postgres() error {
libs := []string{
"github.com/LUSHDigital/core-sql",
"github.com/LUSHDigital/scan",
"github.com/lib/pq",
}
return goget(libs...)
}
// Cockroach installs or upgrades the cockroach packages
func (Mod) Cockroach() error {
libs := []string{
"github.com/LUSHDigital/core-sql",
"github.com/LUSHDigital/scan",
"github.com/lib/pq",
}
return goget(libs...)
}
// Rabbit installs or upgrades the rabbit packages
func (Mod) Rabbit() error {
libs := []string{
"github.com/streadway/amqp",
}
return goget(libs...)
}
// Redis installs or upgrades the redis packages
func (Mod) Redis() error {
libs := []string{
"github.com/LUSHDigital/core-redis",
"github.com/go-redis/redis",
"github.com/alicebob/miniredis",
"github.com/elliotchance/redismock",
}
return goget(libs...)
}
// Test runs the project tests inside docker compose
func Test(ctx context.Context) {
mg.CtxDeps(ctx, Tests.Run)
}
// Install installs the go module dependencies in the vendor directory
func Install(ctx context.Context) error {
args := []string{
"run", "--rm",
}
if proxy := Environment.GoProxy(); proxy != "" {
args = append(args, "-e", fmt.Sprintf("GOPROXY=%s", proxy))
}
if modpath := Environment.GoModPath(); modpath != "" {
args = append(args, "-v", fmt.Sprintf("%s:/go/pkg/mod", modpath))
}
args = append(args,
"-v", InstallVolume,
"-w", InstallWorkDir,
DockerBuildImage,
)
return Exec(DockerBin, append(args,
"go", "mod", "vendor",
)...)
}
// Upgrade installs the latest version of the mage targets
func Upgrade(ctx context.Context) error {
if err := Exec(GoBin, "get", "-u", MageTargetsRepo); err != nil {
return err
}
return Exec(GoBin, "mod", "tidy")
}