-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.go
More file actions
375 lines (299 loc) · 7.36 KB
/
dev.go
File metadata and controls
375 lines (299 loc) · 7.36 KB
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
//go:build ignore
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"unicode"
)
var tags = struct {
build string
test string
}{
build: "",
test: "",
}
var opts struct {
pkg string
goos string
goarch string
tags string
testTags string
unoptimized bool
debug bool
race bool
verbose bool
}
func main() {
flag.StringVar(&opts.pkg, "pkg", "./cmd/...", "A specific package to build")
flag.StringVar(&opts.goos, "goos", "", "Sets the GOOS environment variable for the build")
flag.StringVar(&opts.goarch, "goarch", "", "Sets the GOARCH environment variable for the build")
flag.StringVar(&opts.tags, "tags", "", "Additional build tags")
flag.StringVar(&opts.testTags, "test-tags", "", "Additional test build tags")
flag.BoolVar(&opts.unoptimized, "unoptimized", false, "Disable optimizations/inlining")
flag.BoolVar(&opts.debug, "debug", false, "Enable symbol table/DWARF generation and disable optimizations/inlining")
flag.BoolVar(&opts.race, "race", false, "Enable data race detection in the final binary")
flag.BoolVar(&opts.verbose, "verbose", false, "Print the commands that are being run along with all command output")
flag.Parse()
if s := strings.TrimSpace(opts.tags); s != "" {
tags.build += " " + s
}
tags.build = strings.TrimSpace(tags.build)
if s := strings.TrimSpace(opts.testTags); s != "" {
tags.test += " " + s
}
tags.test = strings.TrimSpace(tags.build + " " + tags.test)
if opts.goos == "" {
opts.goos = runtime.GOOS
}
if opts.goarch == "" {
opts.goarch = runtime.GOARCH
}
commands := flag.Args()
if len(commands) == 0 {
commands = []string{"build"}
}
for _, command := range commands {
switch command {
case "build":
if err := build(); err != nil {
os.Exit(1)
}
case "generate":
if err := generate(); err != nil {
os.Exit(1)
}
case "vet":
if err := vet(); err != nil {
os.Exit(1)
}
case "test":
if err := test(""); err != nil {
os.Exit(1)
}
case "cover":
if err := cover(); err != nil {
os.Exit(1)
}
default:
fmt.Printf("Unknown command %q, please see help for details\n", command)
}
}
}
func command(program string, args ...string) (string, []string, string) {
messageValues := make([]any, len(args))
for i, arg := range args {
messageValues[i] = arg
}
verbs := make([]string, len(args))
for i, arg := range args {
if strings.IndexFunc(arg, unicode.IsSpace) >= 0 {
verbs[i] = "%q"
} else {
verbs[i] = "%v"
}
}
message := fmt.Sprintf("%v "+strings.Join(verbs, " "), append([]any{program}, messageValues...)...)
return program, args, message
}
func generate() error {
program, args, message := command("go", "generate", "./...")
if opts.verbose {
fmt.Printf("-> %v ... ", message)
} else {
fmt.Print("-> go generate ... ")
}
if out, err := exec.Command(program, args...).CombinedOutput(); err != nil {
fmt.Println("error")
if len(out) > 0 {
fmt.Println(string(out))
}
return err
} else {
fmt.Println("ok")
if len(out) > 0 && opts.verbose {
fmt.Println(string(out))
}
}
return nil
}
func test(coverfile string) error {
args := []string{"test", "-vet", "off", "-race"}
if coverfile != "" {
args = append(args, "-coverprofile", coverfile)
}
if len(tags.test) > 0 {
args = append(args, "-tags", tags.test)
}
args = append(args, "./...")
program, args, message := command("go", args...)
if opts.verbose {
fmt.Printf("-> %v ... ", message)
} else {
if coverfile != "" {
fmt.Print("-> go test (cover profile) ... ")
} else {
fmt.Print("-> go test ... ")
}
}
if out, err := exec.Command(program, args...).CombinedOutput(); err != nil {
fmt.Println("error")
if len(out) > 0 {
if !opts.verbose {
var filtered []string
lines := strings.Split(string(out), "\n")
for _, line := range lines {
if strings.HasPrefix(line, "?") || strings.HasPrefix(line, "ok") {
continue
}
if coverfile != "" && strings.Contains(line, "coverage:") {
continue
}
filtered = append(filtered, line)
}
out = []byte(strings.Join(filtered, "\n"))
}
fmt.Println(string(out))
}
return err
} else {
fmt.Println("ok")
if len(out) > 0 && opts.verbose {
fmt.Println(string(out))
}
}
return nil
}
func cover() error {
const coverfile = "_cover.out"
if err := test(coverfile); err != nil {
return err
}
program, args, message := command("go", "tool", "cover", "-html", coverfile)
if opts.verbose {
fmt.Printf("-> %v ... ", message)
} else {
fmt.Print("-> go tool cover ... ")
}
if out, err := exec.Command(program, args...).CombinedOutput(); err != nil {
fmt.Println("error")
if len(out) > 0 {
fmt.Println(string(out))
}
return err
} else {
fmt.Println("ok")
if len(out) > 0 && opts.verbose {
fmt.Println(string(out))
}
}
return nil
}
func vet() error {
args := []string{"vet"}
if len(tags.test) > 0 {
args = append(args, "-tags", tags.test)
}
args = append(args, "./...")
program, args, message := command("go", args...)
if opts.verbose {
fmt.Printf("-> %v ... ", message)
} else {
fmt.Print("-> go vet ... ")
}
if out, err := exec.Command(program, args...).CombinedOutput(); err != nil {
fmt.Println("error")
if len(out) > 0 {
fmt.Println(string(out))
}
return err
} else {
fmt.Println("ok")
if len(out) > 0 && opts.verbose {
fmt.Println(string(out))
}
}
return nil
}
func build() error {
var gcflags []string
var ldflags []string
args := []string{"build", "-o", "."}
if len(tags.build) > 0 {
args = append(args, "-tags", tags.build)
}
if opts.debug || opts.unoptimized {
// -N disables all optimizations
// -l disables inlining
// See: go tool compile --help
gcflags = append(gcflags, "all=-N -l")
}
if opts.debug {
if opts.goos == "windows" {
// This is required on Windows to view disassembly in things like pprof
args = append(args, "-buildmode", "exe")
}
ldflags = append(ldflags, "-X 'main.target=Debug'")
} else {
args = append(args, "-trimpath")
// -s disables the symbol table
// -w disables DWARF generation
// See: go tool link --help
ldflags = append(ldflags, "-s")
ldflags = append(ldflags, "-w")
ldflags = append(ldflags, "-X 'main.target=Release'")
}
if opts.race {
args = append(args, "-race")
}
if len(gcflags) > 0 {
args = append(args, "-gcflags", strings.Join(gcflags, " "))
}
if len(ldflags) > 0 {
args = append(args, "-ldflags", strings.Join(ldflags, " "))
}
args = append(args, opts.pkg)
var env []string
if opts.goos != "" {
env = append(env, "GOOS="+opts.goos)
}
if opts.goarch != "" {
env = append(env, "GOARCH="+opts.goarch)
}
program, args, message := command("go", args...)
if opts.verbose {
fmt.Printf("-> %v ... ", message)
} else {
fmt.Printf("-> go build %v ... ", opts.pkg)
}
cmd := exec.Command(program, args...)
if len(env) > 0 {
cmd.Env = append(os.Environ(), env...)
}
if out, err := cmd.CombinedOutput(); err != nil {
fmt.Println("error")
if len(out) > 0 {
fmt.Println(string(out))
}
return err
} else {
fmt.Print("ok ")
var info []string
if opts.debug {
info = append(info, "debug")
} else {
info = append(info, "release")
}
if opts.race {
info = append(info, "race")
}
fmt.Printf("(%v)\n", strings.Join(info, "/"))
if len(out) > 0 && opts.verbose {
fmt.Println(string(out))
}
}
return nil
}