-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathmagefile.go
633 lines (563 loc) · 16 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
//go:build mage
// +build mage
// lint:file-ignore U1000 Ignore all code, it's only for development
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/magefile/mage/mg" // mg contains helpful utility functions, like Deps
"github.com/magefile/mage/sh" // mg contains helpful utility functions, like Deps
"github.com/mholt/archiver"
spartamage "github.com/mweagle/Sparta/v3/magefile"
"github.com/otiai10/copy"
"github.com/pkg/browser"
"github.com/pkg/errors"
)
const (
localWorkDir = "./.sparta"
hugoVersion = "0.89.2"
archIconsRootPath = "resources/describe/AWS-Architecture-Icons_PNG"
archIconsTreePath = "resources/describe/AWS-Architecture-Icons.tree.txt"
urlCloudFormationSchema = "https://d201a2mn26r7lk.cloudfront.net/latest/gzip/CloudFormationResourceSpecification.json"
)
func xplatPath(pathParts ...string) string {
return filepath.Join(pathParts...)
}
var (
ignoreSubdirectoryPaths = []string{
xplatPath(".vendor"),
xplatPath(".sparta"),
xplatPath(".vscode"),
xplatPath("resources", "describe"),
xplatPath("docs_source", "themes"),
}
hugoDocsSourcePath = xplatPath(".", "docs_source")
hugoDocsPaths = []string{
hugoDocsSourcePath,
xplatPath(".", "docs"),
}
hugoPath = filepath.Join(localWorkDir, "hugo")
header = strings.Repeat("-", 80)
)
// Default target to run when none is specified
// If not set, running mage will list available targets
// var Default = Build
func markdownSourceApply(commandParts ...string) error {
return spartamage.ApplyToSource("md", ignoreSubdirectoryPaths, commandParts...)
}
func goSourceApply(commandParts ...string) error {
return spartamage.ApplyToSource("go", ignoreSubdirectoryPaths, commandParts...)
}
func goFilteredSourceApply(ignorePatterns []string, commandParts ...string) error {
ignorePatterns = append(ignorePatterns, ignoreSubdirectoryPaths...)
return spartamage.ApplyToSource("go", ignorePatterns, commandParts...)
}
func gitCommit(shortVersion bool) (string, error) {
args := []string{
"rev-parse",
}
if shortVersion {
args = append(args, "--short")
}
args = append(args, "HEAD")
val, valErr := sh.Output("git", args...)
return strings.TrimSpace(val), valErr
}
// EnsureCleanTree ensures that the git tree is clean
func EnsureCleanTree() error {
cleanTreeScript := [][]string{
// No dirty trees
{"git", "diff", "--exit-code"},
}
return spartamage.Script(cleanTreeScript)
}
////////////////////////////////////////////////////////////////////////////////
// START - DOCUMENTATION
////////////////////////////////////////////////////////////////////////////////
// ensureWorkDir ensures that the scratch directory exists
func ensureWorkDir() error {
return os.MkdirAll(localWorkDir, os.ModePerm)
}
func runHugoCommand(hugoCommandArgs ...string) error {
absHugoPath, absHugoPathErr := filepath.Abs(hugoPath)
if absHugoPathErr != nil {
return absHugoPathErr
}
// Get the git short value
gitSHA, gitSHAErr := gitCommit(true)
if gitSHAErr != nil {
return gitSHAErr
}
workDir, workDirErr := filepath.Abs(hugoDocsSourcePath)
if workDirErr != nil {
return workDirErr
}
var output io.Writer
if mg.Verbose() {
output = os.Stdout
}
cmd := exec.Command(absHugoPath, hugoCommandArgs...)
cmd.Env = append(os.Environ(), fmt.Sprintf("GIT_HEAD_COMMIT=%s", gitSHA))
cmd.Stderr = os.Stderr
cmd.Stdout = output
cmd.Dir = workDir
return cmd.Run()
}
func docsCopySourceTemplatesToDocs() error {
outputDir := filepath.Join(".",
"docs_source",
"static",
"source",
"resources",
"provision",
"apigateway")
rmErr := os.RemoveAll(outputDir)
if rmErr != nil {
return rmErr
}
// Create the directory
createErr := os.MkdirAll(outputDir, os.ModePerm)
if createErr != nil {
return createErr
}
inputDir := filepath.Join(".", "resources", "provision", "apigateway")
return copy.Copy(inputDir, outputDir)
}
// DocsInstallRequirements installs the required Hugo version
func DocsInstallRequirements() error {
mg.SerialDeps(ensureWorkDir)
// Is hugo already installed?
spartamage.Log("Checking for Hugo version: %s", hugoVersion)
hugoOutput, hugoOutputErr := sh.Output(hugoPath, "version")
if hugoOutputErr == nil && strings.Contains(hugoOutput, hugoVersion) {
spartamage.Log("Hugo version %s already installed at %s", hugoVersion, hugoPath)
return nil
}
hugoArchiveName := ""
switch runtime.GOOS {
case "darwin":
hugoArchiveName = "macOS-64bit.tar.gz"
case "linux":
hugoArchiveName = "Linux-64bit.tar.gz"
default:
hugoArchiveName = fmt.Sprintf("UNSUPPORTED_%s", runtime.GOOS)
}
hugoURL := fmt.Sprintf("https://github.com/gohugoio/hugo/releases/download/v%s/hugo_extended_%s_%s",
hugoVersion,
hugoVersion,
hugoArchiveName)
spartamage.Log("Installing Hugo from source: %s", hugoURL)
outputArchive := filepath.Join(localWorkDir, "hugo.tar.gz")
outputFile, outputErr := os.Create(outputArchive)
if outputErr != nil {
return outputErr
}
hugoResp, hugoRespErr := http.Get(hugoURL)
if hugoRespErr != nil {
return hugoRespErr
}
defer hugoResp.Body.Close()
_, copyBytesErr := io.Copy(outputFile, hugoResp.Body)
if copyBytesErr != nil {
return copyBytesErr
}
// Great, go heads and untar it...
unarchiver := archiver.NewTarGz()
unarchiver.OverwriteExisting = true
untarErr := unarchiver.Unarchive(outputArchive, localWorkDir)
if untarErr != nil {
return untarErr
}
versionScript := [][]string{
{hugoPath, "version"},
}
return spartamage.Script(versionScript)
}
// DocsBuild builds the public documentation site in the /docs folder
func DocsBuild() error {
cleanDocsDirectory := func() error {
docsDir, docsDirErr := filepath.Abs("docs")
if docsDirErr != nil {
return docsDirErr
}
spartamage.Log("Cleaning output directory: %s", docsDir)
return os.RemoveAll(docsDir)
}
mg.SerialDeps(DocsInstallRequirements,
cleanDocsDirectory,
docsCopySourceTemplatesToDocs)
return runHugoCommand()
}
// DocsCommit builds and commits the current
// documentation with an autogenerated comment
func DocsCommit() error {
mg.SerialDeps(DocsBuild)
commitNoMessageScript := make([][]string, 0)
for _, eachPath := range hugoDocsPaths {
commitNoMessageScript = append(commitNoMessageScript,
[]string{"git", "add", "--all", eachPath},
)
}
commitNoMessageScript = append(commitNoMessageScript,
[]string{"git", "commit", "-m", `"Documentation updates"`},
)
return spartamage.Script(commitNoMessageScript)
}
// DocsEdit starts a Hugo server and hot reloads the documentation at http://localhost:1313
func DocsEdit() error {
mg.SerialDeps(DocsInstallRequirements,
docsCopySourceTemplatesToDocs)
editCommandArgs := []string{
"server",
"--disableFastRender",
"--watch",
"--forceSyncStatic",
"--verbose",
}
go func() {
spartamage.Log("Waiting for docs to build...")
time.Sleep(3 * time.Second)
browser.OpenURL("http://localhost:1313")
}()
return runHugoCommand(editCommandArgs...)
}
////////////////////////////////////////////////////////////////////////////////
// END - DOCUMENTATION
////////////////////////////////////////////////////////////////////////////////
// GenerateAutomaticCode is the handler that runs the codegen part of things
func GenerateAutomaticCode() error {
// First one is the embedded metric format
// https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format_Specification.html
args := []string{"aws/cloudwatch/emf.schema.json",
"--capitalization",
"AWS",
"--capitalization",
"emf",
"--output",
"aws/cloudwatch/emf.go",
"--package",
"cloudwatch",
}
if mg.Verbose() {
args = append(args, "--verbose")
}
return sh.Run("gojsonschema", args...)
}
// ViewDependencies creates an SVG output visualized with dot for all
// package dependencies
func ViewDependencies() error {
dotfile := "godepgraph.dot"
svgfile := fmt.Sprintf("%s.svg", dotfile)
generateCommands := [][]string{
{"gomod",
"graph",
"-o",
dotfile,
},
{"dot",
"-v",
"-Tsvg",
dotfile,
"-o",
svgfile,
},
{"open",
svgfile},
}
return spartamage.Script(generateCommands)
}
// GenerateBuildInfo creates the automatic buildinfo.go file so that we can
// stamp the SHA into the binaries we build...
func GenerateBuildInfo() error {
mg.SerialDeps(EnsureCleanTree)
// The first thing we need is the `git` SHA
gitSHA, gitSHAErr := gitCommit(false)
if gitSHAErr != nil {
return errors.Wrapf(gitSHAErr, "Failed to get git commit SHA")
}
// Super = update the buildinfo data
buildInfoTemplate := `package sparta
// THIS FILE IS AUTOMATICALLY GENERATED
// DO NOT EDIT
// CREATED: %s
// SpartaGitHash is the commit hash of this Sparta library
const SpartaGitHash = "%s"
// SpartaGitShortHash is the short version of SpartaGitHash
const SpartaGitShortHash = "%s"
`
updatedInfo := fmt.Sprintf(buildInfoTemplate,
time.Now().UTC(),
gitSHA,
gitSHA[0:7])
// Write it to the output location...
writeErr := ioutil.WriteFile("./buildinfo.go", []byte(updatedInfo), os.ModePerm)
if writeErr != nil {
return writeErr
}
commitGenerateCommands := [][]string{
{"git", "diff"},
{"git", "commit", "-a", "-m", `"Autogenerated build info"`},
}
return spartamage.Script(commitGenerateCommands)
}
// FetchCloudFormationSchema we have the latest CloudFormation schema as part of generating
// constants. To do this, we'll grab the JSON and put it into a local
// folder.
func FetchCloudFormationSchema() error {
// Get the data
httpResp, httpErr := http.Get(urlCloudFormationSchema)
if httpErr != nil {
return httpErr
}
defer httpResp.Body.Close()
// Create the file
outputFile, outputFileErr := os.Create("./aws/cloudformation/cloudformation-schema.json")
if outputFileErr != nil {
return outputFileErr
}
defer outputFile.Close()
// Write the body to file
_, copyErr := io.Copy(outputFile, httpResp.Body)
return copyErr
}
// GenerateConstants runs the set of commands that update the embedded CONSTANTS
// for both local and AWS Lambda execution
func GenerateConstants() error {
mg.SerialDeps(FetchCloudFormationSchema)
generateCommands := [][]string{
// Remove the tree output
{"rm",
"-fv",
xplatPath(archIconsTreePath),
},
// Create the tree output
{"tree",
"-Q",
"-o",
xplatPath(archIconsTreePath),
xplatPath(archIconsRootPath),
},
{"git",
"commit",
"-a",
"-m",
"Autogenerated constants"},
}
return spartamage.Script(generateCommands)
}
// EnsurePrealloc ensures that slices that could be preallocated are enforced
func EnsurePrealloc() error {
// Super run some commands
preallocCommand := [][]string{
{"prealloc", "-set_exit_status", "./..."},
}
return spartamage.Script(preallocCommand)
}
// CIBuild is the task to build in the context of CI pipeline
func CIBuild() error {
mg.SerialDeps(EnsureCIBuildEnvironment,
Build,
Test)
return nil
}
// EnsureMarkdownSpelling ensures that all *.MD files are checked for common
// spelling mistakes
func EnsureMarkdownSpelling() error {
return markdownSourceApply("misspell", "-error")
}
// EnsureSpelling ensures that there are no misspellings in the source
func EnsureSpelling() error {
ignoreFiles := []string{
"CONSTANTS*",
}
goSpelling := func() error {
return goFilteredSourceApply(ignoreFiles, "misspell", "-error")
}
mg.SerialDeps(
goSpelling,
EnsureMarkdownSpelling)
return nil
}
// EnsureVet ensures that the source has been `go vet`ted
func EnsureVet() error {
verboseFlag := ""
if mg.Verbose() {
verboseFlag = "-v"
}
vetCommand := [][]string{
{"go", "vet", verboseFlag, "./..."},
}
return spartamage.Script(vetCommand)
}
// EnsureLint ensures that the source is `golint`ed
func EnsureLint() error {
return goSourceApply("golint")
}
// EnsureGoFmt ensures that the source is `gofmt -s` is empty
func EnsureGoFmt() error {
ignoreGlobs := append(ignoreSubdirectoryPaths,
"CONSTANTS.go",
"CONSTANTS_AWSBINARY.go")
return spartamage.ApplyToSource("go", ignoreGlobs, "gofmt", "-s", "-d")
}
// EnsureFormatted ensures that the source code is formatted with goimports
func EnsureFormatted() error {
cmd := exec.Command("goimports", "-e", "-d", ".")
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return err
}
if stdout.String() != "" {
if mg.Verbose() {
log.Print(stdout.String())
}
return errors.New("`goimports -e -d .` found import errors. Run `goimports -e -w .` to fix them")
}
return nil
}
// EnsureStaticChecks ensures that the source code passes static code checks
func EnsureStaticChecks() error {
// https://staticcheck.io/
excludeChecks := "-exclude=G204,G505,G401,G404,G601"
staticCheckErr := sh.Run("staticcheck",
"github.com/mweagle/Sparta/v3/...")
if staticCheckErr != nil {
return staticCheckErr
}
// https://github.com/securego/gosec
if mg.Verbose() {
return sh.Run("gosec",
excludeChecks,
"./...")
}
return sh.Run("gosec",
excludeChecks,
"-quiet",
"./...")
}
// LogCodeMetrics ensures that the source code is formatted with goimports
func LogCodeMetrics() error {
return sh.Run("gocloc", ".")
}
// EnsureAllPreconditions ensures that the source passes *ALL* static `ensure*`
// precondition steps
func EnsureAllPreconditions() error {
mg.SerialDeps(
EnsureVet,
EnsureLint,
EnsureGoFmt,
EnsureFormatted,
EnsureStaticChecks,
EnsureSpelling,
EnsurePrealloc,
)
return nil
}
// EnsureCIBuildEnvironment is the command that sets up the CI
// environment to run the build.
func EnsureCIBuildEnvironment() error {
// Super run some commands
ciCommands := [][]string{
{"go", "version"},
}
return spartamage.Script(ciCommands)
}
// Build the application
func Build() error {
mg.Deps(EnsureAllPreconditions)
return sh.Run("go", "build", ".")
}
// Clean the working directory
func Clean() error {
cleanCommands := [][]string{
{"go", "clean", "."},
{"rm", "-rf", "./graph.html"},
{"rsync", "-a", "--quiet", "--remove-source-files", "./vendor/", "$GOPATH/src"},
}
return spartamage.Script(cleanCommands)
}
// Describe runs the `TestDescribe` test to generate a describe HTML output
// file at graph.html
func Describe() error {
describeCommands := [][]string{
{"rm", "-rf", "./graph.html"},
{"go", "test", "-v", "-run", "TestDescribe"},
}
return spartamage.Script(describeCommands)
}
// Publish the latest source
func Publish() error {
mg.SerialDeps(DocsBuild,
DocsCommit,
GenerateBuildInfo)
describeCommands := [][]string{
{"git", "push", "origin"},
}
return spartamage.Script(describeCommands)
}
// UnitTest only runs the unit tests
func UnitTest() error {
verboseFlag := ""
if mg.Verbose() {
verboseFlag = "-v"
}
testCommand := [][]string{
{"go", "test", verboseFlag, "-cover", "-race", "./..."},
}
return spartamage.Script(testCommand)
}
// Test runs the Sparta tests
func Test() error {
mg.SerialDeps(
EnsureAllPreconditions,
)
verboseFlag := ""
if mg.Verbose() {
verboseFlag = "-v"
}
testCommand := [][]string{
{"go", "test", verboseFlag, "-cover", "-race", "./..."},
}
return spartamage.Script(testCommand)
}
// TestCover runs the test and opens up the resulting report
func TestCover() error {
mg.SerialDeps(
EnsureAllPreconditions,
)
coverageReport := fmt.Sprintf("%s/cover.out", localWorkDir)
testCoverCommands := [][]string{
{"go", "test", fmt.Sprintf("-coverprofile=%s", coverageReport), "."},
{"go", "tool", "cover", fmt.Sprintf("-html=%s", coverageReport)},
{"rm", coverageReport},
{"open", fmt.Sprintf("%s/cover.html", localWorkDir)},
}
return spartamage.Script(testCoverCommands)
}
// CompareAgainstMasterBranch is a convenience function to show the comparisons
// of the current pushed branch against the master branch
func CompareAgainstMasterBranch() error {
// Get the current branch, open a browser
// to the change...
// The first thing we need is the `git` branch
gitInfo, gitInfoErr := sh.Output("git", "rev-parse", "--abbrev-ref", "HEAD")
if gitInfoErr != nil {
return gitInfoErr
}
stdOutResult := strings.TrimSpace(gitInfo)
githubURL := fmt.Sprintf("https://github.com/mweagle/Sparta/compare/master...%s", stdOutResult)
return browser.OpenURL(githubURL)
}