-
Notifications
You must be signed in to change notification settings - Fork 0
/
checks.go
117 lines (92 loc) · 2.52 KB
/
checks.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
package k6lint
import (
"context"
"fmt"
"os"
)
type checkFunc func(ctx context.Context, dir string) *checkResult
type checkResult struct {
passed bool
details string
}
func checkFailed(details string) *checkResult {
return &checkResult{passed: false, details: fmt.Sprint(details)}
}
func checkPassed(details string, args ...any) *checkResult {
return &checkResult{passed: true, details: fmt.Sprintf(details, args...)}
}
func checkError(err error) *checkResult {
return &checkResult{passed: false, details: "error: " + err.Error()}
}
type checkDefinition struct {
id Checker
fn checkFunc
score int
}
func checkDefinitions(official bool) []checkDefinition {
modCheck := newModuleChecker()
gitCheck := newGitChecker()
defs := []checkDefinition{
{id: CheckerModule, score: 2, fn: modCheck.hasGoModule},
{id: CheckerReplace, score: 2, fn: modCheck.hasNoReplace},
{id: CheckerReadme, score: 5, fn: checkerReadme},
{id: CheckerLicense, score: 5, fn: checkerLicense},
{id: CheckerGit, score: 1, fn: gitCheck.isWorkDir},
{id: CheckerVersions, score: 5, fn: gitCheck.hasVersions},
{id: CheckerBuild, score: 5, fn: modCheck.canBuild},
{id: CheckerSmoke, score: 2, fn: modCheck.smoke},
{id: CheckerExamples, score: 2, fn: modCheck.examples},
{id: CheckerTypes, score: 2, fn: modCheck.types},
}
if !official {
return defs
}
extra := []checkDefinition{
{id: CheckerCodeowners, score: 2, fn: checkerCodeowners},
}
defs = append(defs, extra...)
return defs
}
func runChecks(ctx context.Context, dir string, opts *Options) ([]Check, int) {
checkDefs := checkDefinitions(opts.Official)
results := make([]Check, 0, len(checkDefs))
passed := passedChecks(opts.Passed)
var total, sum float64
for _, checker := range checkDefs {
total += float64(checker.score)
var check Check
if c, found := passed[checker.id]; found {
check = c
} else {
res := checker.fn(ctx, dir)
check.ID = checker.id
check.Passed = res.passed
check.Details = res.details
}
if check.Passed {
sum += float64(checker.score)
}
results = append(results, check)
}
return results, int((sum / total) * 100.0)
}
// ParseChecker parses checker name from string.
func ParseChecker(val string) (Checker, error) {
v := Checker(val)
switch v {
case
CheckerModule,
CheckerReplace,
CheckerReadme,
CheckerExamples,
CheckerLicense,
CheckerGit,
CheckerVersions,
CheckerBuild,
CheckerSmoke,
CheckerCodeowners:
return v, nil
default:
return "", fmt.Errorf("%w: %s", os.ErrInvalid, val) //nolint:forbidigo
}
}