/
common.go
391 lines (305 loc) · 7.51 KB
/
common.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Copyright 2019 Aporeto Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package magetask
import (
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"github.com/magefile/mage/sh"
"golang.org/x/sync/errgroup"
)
var projectName string
func init() {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
projectName = path.Base(wd)
}
// SetProjectName sets the name of the project.
func SetProjectName(name string) {
projectName = name
}
// WriteVersion creates the version file if needed.
func WriteVersion() error {
return WriteVersionIn("")
}
// GetSemver gets the semantic version of the repository
func GetSemver() (sver string, err error) {
if sver = os.Getenv("PROJECT_VERSION"); sver == "" {
return "", fmt.Errorf("unable to find project version")
}
return
}
// WriteVersionIn creates the version file if needed in the given folder.
func WriteVersionIn(out string) (err error) {
projectSha := ""
projectVersion := ""
if projectSha = os.Getenv("PROJECT_SHA"); projectSha == "" {
return fmt.Errorf("unable to find project SHA")
}
projectVersion, err = GetSemver()
if err != nil {
return err
}
if _, err := os.Stat("./Gopkg.toml"); err == nil {
if err := makeVersionFromDep("", out, projectVersion, projectSha); err != nil {
return err
}
}
fmt.Println("complete: versions file generation")
return nil
}
// Lint runs the linters.
func Lint() error {
if err := run(
nil,
"gometalinter",
"--exclude",
"bindata.go",
"--exclude",
"vendor",
"--vendor",
"--disable-all",
"--enable",
"vet",
"--enable",
"vetshadow",
"--enable",
"golint",
"--enable",
"ineffassign",
"--enable",
"goconst",
"--enable",
"errcheck",
"--enable",
"varcheck",
"--enable",
"structcheck",
"--enable",
"gosimple",
"--enable",
"misspell",
"--enable",
"deadcode",
"--enable",
"staticcheck",
"--deadline",
"30m",
"--tests",
"./...",
); err != nil {
return err
}
fmt.Println("complete: linters")
return nil
}
// Test runs unit tests.
func Test() error {
return TestWith(true, true)
}
// Build builds the project for the current machine.
func Build() error {
env := map[string]string{
"CGO_ENABLED": "0",
}
if err := run(env, "go", "build", "-a", "-installsuffix", "cgo"); err != nil {
return err
}
fmt.Println("complete: default build")
return nil
}
// BuildLinux builds the project for Linux.
func BuildLinux() error {
env := map[string]string{
"CGO_ENABLED": "0",
"GOOS": "linux",
"GOARCH": "amd64",
}
if err := run(env, "go", "build", "-a", "-installsuffix", "cgo"); err != nil {
return err
}
fmt.Println("complete: linux build")
return nil
}
// BuildWindows builds the project for Windows.
func BuildWindows() error {
env := map[string]string{
"CGO_ENABLED": "0",
"GOOS": "windows",
"GOARCH": "amd64",
}
if err := run(env, "go", "build", "-a", "-installsuffix", "cgo"); err != nil {
return err
}
fmt.Println("complete: windows build")
return nil
}
// BuildDarwin builds the project for macOS.
func BuildDarwin() error {
env := map[string]string{
"CGO_ENABLED": "0",
"GOOS": "darwin",
"GOARCH": "amd64",
}
if err := run(env, "go", "build", "-a", "-installsuffix", "cgo"); err != nil {
return err
}
fmt.Println("complete: darwin build")
return nil
}
// BuildFor builds a a cli for the given platform.
func BuildFor(platform string, buildFunc func() error) error {
if err := os.MkdirAll("./build/"+platform, 0755); err != nil {
return err
}
if err := buildFunc(); err != nil {
return err
}
return sh.Run("mv", projectName, "build/"+platform+"/"+projectName)
}
// Package packages the project for docker build.
func Package() error {
return PackageFrom(projectName)
}
// PackageFrom packages the given binary for docker build
func PackageFrom(path string) error {
if err := os.MkdirAll("docker/app", 0755); err != nil {
return err
}
if err := run(nil, "cp", "-a", path, "docker/app"); err != nil {
return err
}
fmt.Println("complete: docker packaging")
return nil
}
// Container creates the docker container.
func Container() error {
image := fmt.Sprintf("gcr.io/aporetodev/%s", projectName)
if tag := os.Getenv("DOMINGO_DOCKER_TAG"); tag != "" {
image = image + ":" + tag
}
if err := os.MkdirAll("docker/app", 0755); err != nil {
return err
}
if err := os.Chdir("docker"); err != nil {
return err
}
defer os.Chdir("..") // nolint
out, err := sh.Output("docker", "build", "-t", image, ".")
if err != nil {
fmt.Println(out)
return err
}
fmt.Println("complete: docker build", image)
return nil
}
// PackageCACerts retrieves the package the CA for docker.
func PackageCACerts() error {
if err := run(nil, "go", "get", "-u", "github.com/agl/extract-nss-root-certs"); err != nil {
return err
}
if err := run(nil, "curl", "-s", "https://hg.mozilla.org/mozilla-central/raw-file/tip/security/nss/lib/ckfw/builtins/certdata.txt", "-o", "certdata.txt"); err != nil {
return err
}
if err := os.MkdirAll("docker/app", 0755); err != nil {
return err
}
out, err := sh.Output("extract-nss-root-certs")
if err != nil {
return nil
}
if err := ioutil.WriteFile("docker/app/ca-certificates.pem", []byte(out), 0644); err != nil {
return err
}
if err := os.Remove("certdata.txt"); err != nil {
return err
}
fmt.Println("complete: ca packaging")
return nil
}
func run(env map[string]string, cmd string, args ...string) error {
out, err := sh.OutputWith(env, cmd, args...)
if out != "" {
fmt.Println(out)
}
if err != nil {
return fmt.Errorf("unable to run command `%s %s`: %s", cmd, strings.Join(args, " "), err)
}
return nil
}
// collectCoverages collects all coverage reports.
func collectCoverages() error {
f, err := os.OpenFile("coverage.txt", os.O_CREATE|os.O_RDWR, 0644)
defer f.Close() // nolint
if err != nil {
return err
}
cfiles, err := filepath.Glob("*.cover")
if err != nil {
return err
}
for _, cfile := range cfiles {
cf, err := os.Open(cfile)
if err != nil {
return err
}
defer cf.Close() // nolint
if _, err := io.Copy(f, cf); err != nil {
return err
}
os.Remove(cfile) // nolint
}
return nil
}
// getTestArgs provides arguments based on test options.
func getTestArgs(race bool, cover bool, p string) []string {
args := []string{"test"}
if race {
args = append(args, "-race")
}
if cover {
args = append(args, "-cover", "-coverprofile=cov-"+path.Base(p)+".cover", "-covermode=atomic")
}
return append(args, p)
}
// TestWith runs unit tests without race.
func TestWith(race bool, cover bool) error {
out, err := sh.Output("go", "list", "./...")
if err != nil {
return err
}
var g errgroup.Group
packages := strings.Split(out, "\n")
for _, p := range packages {
g.Go(func(p string) func() error {
return func() error {
args := getTestArgs(race, cover, p)
return run(nil, "go", args...)
}
}(p))
}
if err = g.Wait(); err != nil {
return err
}
if cover {
if err := collectCoverages(); err != nil {
return err
}
}
fmt.Println("complete: tests")
return nil
}