-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagefile.go
224 lines (189 loc) · 5.46 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
//go:build mage
// +build mage
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/magefile/mage/mg"
)
const (
repo = "github.com/rtrox/lybbr.io"
name = "lybbrio"
buildEntrypoint = "./cmd/lybbrio/main.go"
)
var (
VersionNumber = "dev"
Revision = "unknown"
LdFlags = ""
BuildTime = ""
RootPath = ""
// Aliases are mage aliases of targets
Aliases = map[string]interface{}{
"build": Build.Build,
"clean": Build.Clean,
"generate": Build.Generate,
"test": Check.Test,
"lint": Check.Lint,
"new-ent": Build.NewEnt,
"check": Check.All,
"check:lint": Check.Lint,
"check:lint-fix": Check.LintFix,
"check:test": Check.Test,
"check:vet": Check.Vet,
"check:fmt": Check.Fmt,
"check:mod-tidy": Check.ModTidy,
"check:all": Check.All,
}
Default = Build.Build
)
func runCmdWithOutput(name string, arg ...string) (output []byte, err error) {
cmd := exec.Command(name, arg...)
cmd.Env = os.Environ()
cmd.Dir = RootPath
output, err = cmd.Output()
if err != nil {
if ee, is := err.(*exec.ExitError); is {
return nil, fmt.Errorf("error running command: %s, %s", string(ee.Stderr), err)
}
return nil, fmt.Errorf("error running command: %s", err)
}
return output, nil
}
func runAndStreamOutput(cmd string, args ...string) {
c := exec.Command(cmd, args...)
c.Env = os.Environ()
c.Dir = RootPath
fmt.Printf("%s\n\n", c.String())
stdout, _ := c.StdoutPipe()
stderr, _ := c.StderrPipe()
err := c.Start()
if err != nil {
fmt.Printf("Could not start: %s\n", err)
os.Exit(1)
}
go func() {
reader := bufio.NewReader(stdout)
line, err := reader.ReadString('\n')
for err == nil {
fmt.Print(line)
line, err = reader.ReadString('\n')
}
}()
reader2 := bufio.NewReader(stderr)
line, err := reader2.ReadString('\n')
for err == nil {
fmt.Print(line)
line, err = reader2.ReadString('\n')
}
if err := c.Wait(); err != nil {
os.Exit(1)
}
}
func setVersion() {
version, err := runCmdWithOutput("git", "describe", "--tags", "--always", "--abbrev=10")
if err != nil {
fmt.Printf("Error getting version: %s\n", err)
os.Exit(1)
}
VersionNumber = strings.Trim(string(version), "\n")
VersionNumber = strings.Replace(VersionNumber, "-", "+", 1)
VersionNumber = strings.Replace(VersionNumber, "-g", "-", 1)
revision := os.Getenv("GITHUB_SHA")
if revision == "" {
rev2, err := runCmdWithOutput("git", "rev-parse", "--short=10", "HEAD")
revision = strings.Trim(string(rev2), "\n")
if err != nil {
fmt.Printf("Error getting revision: %s\n", err)
os.Exit(1)
}
}
Revision = strings.Trim(string(revision), "\n")
}
func setRootPath() {
pwd, err := os.Getwd()
if err != nil {
fmt.Printf("Error getting root path: %s\n", err)
os.Exit(1)
}
RootPath = pwd
}
func initVars() {
setVersion()
setRootPath()
if BuildTime == "" {
BuildTime = time.Now().Format(time.RFC3339)
}
LdFlags = fmt.Sprintf("-s -w -X main.version=%s -X main.revision=%s -X main.buildTime=%s", VersionNumber, Revision, BuildTime)
}
func Run() {
mg.Deps(initVars)
fmt.Printf("Reminder: Mage does not yet support targets with variadic arguments, so you must use a env vars with mage run.\n")
fmt.Printf("Running %s version %s, revision %s\n", name, VersionNumber, Revision)
runAndStreamOutput("go", "run", "-ldflags", LdFlags, buildEntrypoint)
}
type Build mg.Namespace
func (Build) Build() {
mg.Deps(initVars)
fmt.Printf("Building %s version %s, revision %s\n", name, VersionNumber, Revision)
runAndStreamOutput("go", "build", "-ldflags", LdFlags, "-o", name, buildEntrypoint)
}
func (Build) Clean() {
mg.Deps(initVars)
if err := exec.Command("go", "clean", "./...").Run(); err != nil {
fmt.Printf("Error cleaning: %s\n", err)
os.Exit(1)
}
}
func (Build) Generate() {
mg.Deps(initVars)
fmt.Printf("Generating %s version %s, revision %s\n", name, VersionNumber, Revision)
runAndStreamOutput("go", "generate", ".")
}
func (Build) NewEnt(entName string) {
mg.Deps(initVars)
fmt.Printf("Creating new ent %s version %s, revision %s\n", name, VersionNumber, Revision)
runAndStreamOutput("go", "run", "-mod=mod", "entgo.io/ent/cmd/ent", "new", entName, "--target", "internal/ent/schema")
}
type Check mg.Namespace
func (Check) Lint() {
mg.Deps(initVars)
fmt.Printf("Linting %s version %s, revision %s\n", name, VersionNumber, Revision)
runAndStreamOutput("golangci-lint", "run", "--config", ".github/lint/golangci.yaml")
}
func (Check) LintFix() {
mg.Deps(initVars)
fmt.Printf("Linting %s version %s, revision %s\n", name, VersionNumber, Revision)
runAndStreamOutput("golangci-lint", "run", "--fix")
}
func (Check) Test() {
mg.Deps(initVars)
fmt.Printf("Testing %s version %s, revision %s\n", name, VersionNumber, Revision)
runAndStreamOutput("go", "test", "-cover", "./...")
}
func (Check) Vet() {
mg.Deps(initVars)
fmt.Printf("Vetting %s version %s, revision %s\n", name, VersionNumber, Revision)
runAndStreamOutput("go", "vet", "./...")
}
func (Check) Fmt() {
mg.Deps(initVars)
fmt.Printf("Formatting %s version %s, revision %s\n", name, VersionNumber, Revision)
runAndStreamOutput("go", "fmt", "./...")
}
func (Check) ModTidy() {
mg.Deps(initVars)
fmt.Printf("Tidying %s version %s, revision %s\n", name, VersionNumber, Revision)
runAndStreamOutput("go", "mod", "tidy")
}
func (Check) All() {
mg.Deps(initVars)
mg.Deps(Check.ModTidy)
mg.Deps(Check.Fmt)
mg.Deps(Check.Vet)
mg.Deps(Check.Lint)
mg.Deps(Check.Test)
}