-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdoctor.go
More file actions
77 lines (67 loc) · 1.57 KB
/
Copy pathdoctor.go
File metadata and controls
77 lines (67 loc) · 1.57 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
package doctor
import "context"
// Status represents the result status of a check item.
type Status int
const (
StatusPass Status = iota
StatusWarn
StatusFail
)
func (s Status) String() string {
switch s {
case StatusPass:
return "pass"
case StatusWarn:
return "warn"
case StatusFail:
return "fail"
default:
return "unknown"
}
}
// CheckItem represents a single line item within a check result.
type CheckItem struct {
Label string `json:"label"`
Status Status `json:"-"`
Detail string `json:"detail,omitempty"`
// For JSON output
StatusStr string `json:"status"`
}
// Result represents the outcome of a check containing multiple items.
type Result struct {
Name string `json:"name"`
Items []CheckItem `json:"items"`
}
// Check defines the interface for a doctor check.
type Check interface {
Name() string
Run(ctx context.Context) Result
}
// RunAll executes all checks and returns their results.
func RunAll(ctx context.Context, checks []Check) []Result {
results := make([]Result, 0, len(checks))
for _, check := range checks {
result := check.Run(ctx)
for i := range result.Items {
result.Items[i].StatusStr = result.Items[i].Status.String()
}
results = append(results, result)
}
return results
}
// Summary returns counts of passed, warned, and failed items across all results.
func Summary(results []Result) (passed, warned, failed int) {
for _, r := range results {
for _, item := range r.Items {
switch item.Status {
case StatusPass:
passed++
case StatusWarn:
warned++
case StatusFail:
failed++
}
}
}
return
}