-
Notifications
You must be signed in to change notification settings - Fork 3
/
magefile.go
170 lines (142 loc) · 3.18 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
//go:build mage
package main
import (
"bytes"
"fmt"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"os"
"os/exec"
"strings"
)
const (
commandName = "full-house"
commandPackage = "fullhouse/cmd/fullhouse"
testCmd = "gotestsum"
lintCmd = "golangci-lint"
)
var (
Default = Deps
)
func Test() error {
return sh.RunV("go", "test", "-v", "./pkg/...", "-coverprofile", "cover.out")
}
func TestCi() error {
return sh.RunV(testCmd, "--format", "testname", "--junitfile", "test_results.xml", "--", "-v", "./pkg/...", "-coverprofile", "cover.out")
}
func Lint() error {
return lint(false)
}
func LintCi() error {
return lint(true)
}
func lint(writeToFile bool) error {
if !verify() {
mg.Deps(Deps)
}
args := []string{
"run",
"-v",
}
if writeToFile {
args = append(args, "--out-format=junit-xml")
}
output, _ := sh.Output(lintCmd, args...)
if writeToFile {
return writeFile("linter_results.xml", output)
} else {
fmt.Println(output)
}
return nil
}
func Build() error {
return build(make(map[string]string))
}
func BuildForDocker() error {
env := map[string]string{
"CGO_ENABLED": "0",
"GOOS": "linux",
}
return build(env)
}
func Docker() error {
mg.Deps(BuildForDocker)
fmt.Println("Building container image")
return sh.RunV("docker", "build", "-t", "philmtd/full-house", ".")
}
func build(env map[string]string) error {
fmt.Println("Building project")
buildflags := []string{
"-installsuffix", "cgo", "--tags", "release",
}
arguments := []string{
"build",
"-o",
commandName,
"-a",
}
arguments = append(arguments, buildflags...)
arguments = append(arguments, "-ldflags="+ldflags(), commandPackage)
return sh.RunWithV(env, "go", arguments...)
}
func ldflags() string {
return fmt.Sprintf(`-extldflags -static -s -w -X=main.GitTag=%s -X=main.GitCommit=%s`, gitTag(), gitCommitHash())
}
func gitCommitHash() string {
hash, _ := sh.Output("git", "rev-parse", "--short", "HEAD")
return strings.TrimSpace(hash)
}
func gitTag() string {
tag, err := sh.Output("git", "tag", "--points-at", "HEAD")
if err != nil {
return "dev"
}
return strings.TrimSpace(tag)
}
func Outdated() error {
modOutdated := exec.Command("go-mod-outdated", "-update", "-direct")
modList, err := sh.Output("go", "list", "-u", "-m", "-json", "all")
if err != nil {
return err
}
modOutdated.Stdin = bytes.NewBufferString(modList)
modOutdated.Stdout = os.Stdout
return modOutdated.Run()
}
func verify() bool {
return sh.CmdRan(sh.RunV("go", "mod", "verify"))
}
func Deps() {
fmt.Println("Running go mod download and go mod tidy...")
mg.SerialDeps(ModDownload, ModTidy)
}
func ModDownload() error {
return sh.RunV("go", "mod", "download")
}
func ModTidy() error {
return sh.RunV("go", "mod", "tidy")
}
func Clean() error {
fmt.Println("Cleaning...")
filesToRemove := []string{
commandName,
"cover.out",
"linter_results.xml",
"test_results.xml",
}
for _, file := range filesToRemove {
if err := sh.Rm(file); err != nil {
return err
}
}
return nil
}
func writeFile(filename, content string) error {
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
_, err = file.WriteString(content)
return err
}