-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolang.go
86 lines (66 loc) · 1.93 KB
/
golang.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
package mage
import (
"context"
"fmt"
"path/filepath"
"github.com/dosquad/mage/dyndep"
"github.com/dosquad/mage/helper"
"github.com/magefile/mage/mg"
"github.com/princjef/mageutil/shellcmd"
)
// Golang namespace is defined to group Golang functions.
type Golang mg.Namespace
// InstallGovulncheck installs govulncheck.
func (Golang) installGovulncheck(_ context.Context) error {
return helper.BinGovulncheck().Ensure()
}
// Vulncheck runs govulncheck.
func (Golang) Vulncheck(ctx context.Context) error {
mg.CtxDeps(ctx, Golang.installGovulncheck)
return helper.BinGovulncheck().Command("./...").Run()
}
// Test run test suite and save coverage report.
func (Golang) Test(ctx context.Context) error {
dyndep.CtxDeps(ctx, dyndep.Golang)
dyndep.CtxDeps(ctx, dyndep.Test)
coverPath := helper.MustGetArtifactPath("coverage")
helper.MustMakeDir(coverPath, 0)
raceArg := ""
if v := helper.GoEnv("CGO_ENABLED", "0"); v == "1" {
raceArg = "-race"
}
cmd := fmt.Sprintf(""+
"go test "+
"%s "+
"-covermode=atomic "+
"-coverprofile=\"%s/cover.out\" "+
"\"./...\"",
raceArg,
coverPath)
if err := shellcmd.Command(cmd).Run(); err != nil {
return err
}
return helper.FilterCoverageOutput(filepath.Join(coverPath, "cover.out"))
}
// Generate runs go generate.
func (Golang) Generate(ctx context.Context) error {
dyndep.CtxDeps(ctx, dyndep.Golang)
return shellcmd.Command(`go generate ./...`).Run()
}
// Lint run golangci-lint.
func (Golang) Lint(ctx context.Context) error {
dyndep.CtxDeps(ctx, dyndep.Golang)
dyndep.CtxDeps(ctx, dyndep.Lint)
if err := helper.BinGolangCILint().Ensure(); err != nil {
return err
}
return helper.BinGolangCILint().Command("run ./... --sort-results --max-same-issues 0 --max-issues-per-linter 0").Run()
}
// Fmt run go fmt.
func (Golang) Fmt() error {
return shellcmd.Command(`go fmt ./...`).Run()
}
// Vet run go vet.
func (Golang) Vet() error {
return shellcmd.Command(`go vet ./...`).Run()
}