-
Notifications
You must be signed in to change notification settings - Fork 1
/
standard.go
397 lines (335 loc) · 10.9 KB
/
standard.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
package build
import (
"flag"
"fmt"
"os"
"path/filepath"
"slices"
"strings"
"github.com/goyek/goyek/v2"
_ "github.com/goyek/x/boot" // define flags to override
"github.com/goyek/x/cmd"
"golang.org/x/mod/modfile"
)
// DefineTasks defines common tasks for Go projects.
func DefineTasks(opts ...Option) {
// Override the goyek verbosity default to true since it's generally better.
// -v=false can still be used to disable it.
_ = flag.Lookup("v").Value.Set("true")
command := flag.String("cmd", "", "Command to execute with runall.")
conf := config{
artifactsPath: "out",
}
for _, o := range opts {
o.apply(&conf)
}
if conf.verActionlint == "" {
conf.verActionlint = verActionlint
}
if conf.verGolangCILint == "" {
conf.verGolangCILint = verGolangCILint
}
if conf.verGoPrettier == "" {
conf.verGoPrettier = verGoPrettier
}
if conf.verGoShellcheck == "" {
conf.verGoShellcheck = verGoShellcheck
}
if conf.verGoYamllint == "" {
conf.verGoYamllint = verGoYamllint
}
golangciTargets := []string{"./..."}
// Uses of go-build will very commonly have a build folder, if it is also a module,
// then let's automatically run checks on it.
if fileExists(filepath.Join("build", "go.mod")) {
golangciTargets = append(golangciTargets, "./build")
}
root, target := pathRelativeToRoot()
if !conf.excluded("format-go") {
RegisterFormatTask(goyek.Define(goyek.Task{
Name: "format-go",
Usage: "Formats Go code.",
Parallel: true,
Action: func(a *goyek.A) {
cmd.Exec(a, fmt.Sprintf(`go run github.com/golangci/golangci-lint/cmd/golangci-lint@%s run --build-tags "%s" --fix --timeout=20m %s`, conf.verGolangCILint, strings.Join(conf.buildTags, ","), strings.Join(golangciTargets, " ")))
cmd.Exec(a, "go mod tidy")
},
}))
}
if !conf.excluded("lint-go") {
RegisterLintTask(goyek.Define(goyek.Task{
Name: "lint-go",
Usage: "Lints Go code.",
Parallel: true,
Action: func(a *goyek.A) {
cmd.Exec(a, fmt.Sprintf(`go run github.com/golangci/golangci-lint/cmd/golangci-lint@%s run --build-tags "%s" --timeout=20m %s`, conf.verGolangCILint, strings.Join(conf.buildTags, ","), strings.Join(golangciTargets, " ")))
goModTidyDiff(a)
},
}))
}
if !conf.excluded("format-markdown") {
RegisterFormatTask(goyek.Define(goyek.Task{
Name: "format-markdown",
Usage: "Formats Markdown code.",
Parallel: true,
Action: func(a *goyek.A) {
cmd.Exec(a, fmt.Sprintf("go run github.com/wasilibs/go-prettier/v3/cmd/prettier@%s --no-error-on-unmatched-pattern --write '**/*.md'", conf.verGoPrettier))
},
}))
}
if !conf.excluded("lint-markdown") {
RegisterLintTask(goyek.Define(goyek.Task{
Name: "lint-markdown",
Usage: "Lints Markdown code.",
Parallel: true,
Action: func(a *goyek.A) {
cmd.Exec(a, fmt.Sprintf("go run github.com/wasilibs/go-prettier/v3/cmd/prettier@%s --no-error-on-unmatched-pattern --check '**/*.md'", conf.verGoPrettier))
},
}))
}
if !conf.excluded("format-shell") {
RegisterFormatTask(goyek.Define(goyek.Task{
Name: "format-shell",
Usage: "Formats shell-like code, including Dockerfile, ignore, dotenv.",
Parallel: true,
Action: func(a *goyek.A) {
cmd.Exec(a, fmt.Sprintf("go run github.com/wasilibs/go-prettier/v3/cmd/prettier@%s --no-error-on-unmatched-pattern --write '**/*.sh' '**/*.bash' '**/Dockerfile' '**/*.dockerfile' '**/.*ignore' '**/.env*'", conf.verGoPrettier))
},
}))
}
if !conf.excluded("lint-shell") {
RegisterLintTask(goyek.Define(goyek.Task{
Name: "lint-shell",
Usage: "Lints shell-like code, including Dockerfile, ignore, dotenv.",
Parallel: true,
Action: func(a *goyek.A) {
cmd.Exec(a, fmt.Sprintf("go run github.com/wasilibs/go-prettier/v3/cmd/prettier@%s --no-error-on-unmatched-pattern --check '**/*.sh' '**/*.bash' '**/Dockerfile' '**/*.dockerfile' '**/.*ignore' '**/.env*'", conf.verGoPrettier))
},
}))
}
if !conf.excluded("format-yaml") {
RegisterFormatTask(goyek.Define(goyek.Task{
Name: "format-yaml",
Usage: "Formats YAML code.",
Parallel: true,
Action: func(a *goyek.A) {
cmd.Exec(a, fmt.Sprintf("go run github.com/wasilibs/go-prettier/v3/cmd/prettier@%s --no-error-on-unmatched-pattern --write '**/*.yaml' '**/*.yml'", conf.verGoPrettier))
},
}))
}
if !conf.excluded("lint-yaml") {
RegisterLintTask(goyek.Define(goyek.Task{
Name: "lint-yaml",
Usage: "Lints YAML code.",
Parallel: true,
Action: func(a *goyek.A) {
cmd.Exec(a, fmt.Sprintf("go run github.com/wasilibs/go-prettier/v3/cmd/prettier@%s --no-error-on-unmatched-pattern --check '**/*.yaml' '**/*.yml'", conf.verGoPrettier))
if root == "" {
cmd.Exec(a, fmt.Sprintf("go run github.com/wasilibs/go-yamllint/cmd/yamllint@%s .", conf.verGoYamllint))
} else {
cmd.Exec(a, fmt.Sprintf("go run github.com/wasilibs/go-yamllint/cmd/yamllint@%s %s", conf.verGoYamllint, target), cmd.Dir(root))
}
},
}))
}
if !conf.excluded("test-go") {
RegisterTestTask(goyek.Define(goyek.Task{
Name: "test-go",
Usage: "Runs Go unit tests.",
Action: func(a *goyek.A) {
if err := os.MkdirAll(conf.artifactsPath, 0o755); err != nil {
a.Errorf("failed to create out directory: %v", err)
return
}
cmd.Exec(a, fmt.Sprintf("go test -coverprofile=%s -covermode=atomic -v -timeout=20m ./...", filepath.Join(conf.artifactsPath, "coverage.txt")))
},
}))
}
if !conf.excluded("runall") && fileExists("go.work") {
RegisterGenerateTask(goyek.Define(goyek.Task{
Name: "runall",
Usage: "Runs a command in each module in the workspace.",
Action: func(a *goyek.A) {
if *command == "" {
a.Error("missing -cmd flag required for runall")
return
}
content, err := os.ReadFile("go.work")
if err != nil {
a.Errorf("failed to read go.work: %v", err)
return
}
wf, err := modfile.ParseWork("go.work", content, nil)
if err != nil {
a.Errorf("failed to parse go.work: %v", err)
return
}
for _, u := range wf.Use {
cmd.Exec(a, *command, cmd.Dir(filepath.Join(".", u.Path)))
}
},
}))
}
if !conf.excluded("lint-github") && fileExists(".github") {
RegisterLintTask(goyek.Define(goyek.Task{
Name: "lint-github",
Usage: "Lints GitHub Actions workflows.",
Parallel: true,
Action: func(a *goyek.A) {
cmd.Exec(a, fmt.Sprintf(`go run github.com/rhysd/actionlint/cmd/actionlint@%s -shellcheck="go run github.com/wasilibs/go-shellcheck/cmd/shellcheck@%s"`, conf.verActionlint, conf.verGoShellcheck))
},
}))
}
goyek.Define(goyek.Task{
Name: "format",
Usage: "Format code in various languages.",
Deps: formatTasks,
})
lint := goyek.Define(goyek.Task{
Name: "lint",
Usage: "Lints code in various languages.",
Deps: lintTasks,
})
goyek.Define(goyek.Task{
Name: "generate",
Usage: "Generates code.",
Deps: generateTasks,
})
test := goyek.Define(goyek.Task{
Name: "test",
Usage: "Runs tests.",
Deps: testTasks,
})
goyek.Define(goyek.Task{
Name: "check",
Usage: "Runs all checks.",
Deps: goyek.Deps{lint, test},
})
}
type config struct {
artifactsPath string
excludeTasks []string
buildTags []string
verActionlint string
verGolangCILint string
verGoPrettier string
verGoYamllint string
verGoShellcheck string
}
func (c *config) excluded(task string) bool {
return slices.Contains(c.excludeTasks, task)
}
// Option is a configuration option for DefineTasks.
type Option interface {
apply(conf *config)
}
// ArtifactPath returns an Option to indicate the path to write temporary build artifacts to,
// for example coverage reports. If not provided, the default is "out".
func ArtifactsPath(path string) Option {
return artifactsPath(path)
}
type artifactsPath string
func (a artifactsPath) apply(c *config) {
c.artifactsPath = string(a)
}
// ExcludeTasks returns an Option to exclude tasks normally added by default. This can be used
// to avoid unneeded tasks, for example to disable linting of Markdown while still keeping the
// ability to manually autoformat it, or to redefine a task with a different implementation.
func ExcludeTasks(task ...string) Option {
return excludeTasks{tasks: task}
}
type excludeTasks struct {
tasks []string
}
func (e excludeTasks) apply(c *config) {
c.excludeTasks = append(c.excludeTasks, e.tasks...)
}
func pathRelativeToRoot() (string, string) {
dir, err := filepath.Abs(".")
if err != nil {
return "", ""
}
base := dir
for {
if anyFileExists(base, ".git", "go.work") {
target, _ := filepath.Rel(base, dir)
return base, target
}
parent := filepath.Dir(base)
if parent == dir || parent == "" {
break
}
base = parent
}
return "", ""
}
func anyFileExists(dir string, files ...string) bool {
for _, f := range files {
if e := fileExists(filepath.Join(dir, f)); e {
return true
}
}
return false
}
func fileExists(p string) bool {
if _, err := os.Stat(p); err == nil {
return true
}
return false
}
// Tags returns an Option to add build tags to Go lint tasks. If any code is guarded by a build tag
// from default compilation, it should be added here to ensure it is linted.
func Tags(tags ...string) Option {
return buildTags{tags: tags}
}
type buildTags struct {
tags []string
}
func (b buildTags) apply(c *config) {
c.buildTags = append(c.buildTags, b.tags...)
}
// VersionActionlint returns an Option to set the version of actionlint to use. If unset,
// a default version is used which may not be the latest.
func VersionActionlint(version string) Option {
return versionActionlint(version)
}
type versionActionlint string
func (v versionActionlint) apply(c *config) {
c.verActionlint = string(v)
}
// VersionGolangCILint returns an Option to set the version of golangci-lint to use. If unset,
// a default version is used which may not be the latest.
func VersionGolangCILint(version string) Option {
return versionGolangCILint(version)
}
type versionGolangCILint string
func (v versionGolangCILint) apply(c *config) {
c.verGolangCILint = string(v)
}
// VersionGoPrettier returns an Option to set the version of go-prettier to use. If unset,
// a default version is used which may not be the latest.
func VersionGoPrettier(version string) Option {
return versionGoPrettier(version)
}
type versionGoPrettier string
func (v versionGoPrettier) apply(c *config) {
c.verGoPrettier = string(v)
}
// VersionGoShellcheck returns an Option to set the version of go-shellcheck to use. If unset,
// a default version is used which may not be the latest.
func VersionGoShellcheck(version string) Option {
return versionGoShellcheck(version)
}
type versionGoShellcheck string
func (v versionGoShellcheck) apply(c *config) {
c.verGoShellcheck = string(v)
}
// VersionGoYamllint returns an Option to set the version of go-yamllint to use. If unset,
// a default version is used which may not be the latest.
func VersionGoYamllint(version string) Option {
return versionGoYamllint(version)
}
type versionGoYamllint string
func (v versionGoYamllint) apply(c *config) {
c.verGoYamllint = string(v)
}