-
Notifications
You must be signed in to change notification settings - Fork 4
/
check.go
67 lines (55 loc) · 1.56 KB
/
check.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
package doctor
import (
"context"
"runtime"
"github.com/samber/lo"
"projectforge.dev/projectforge/app/lib/telemetry"
"projectforge.dev/projectforge/app/util"
)
type (
CheckFn func(ctx context.Context, r *Result, logger util.Logger) *Result
SolveFn func(ctx context.Context, r *Result, logger util.Logger) *Result
)
type Check struct {
Key string `json:"key"`
Section string `json:"section"`
Title string `json:"title"`
Summary string `json:"summary,omitempty"`
URL string `json:"url,omitempty"`
UsedBy string `json:"usedBy,omitempty"`
Modules []string `json:"modules,omitempty"`
Platforms []string `json:"platforms,omitempty"`
Core bool `json:"core,omitempty"`
Fn CheckFn `json:"-"`
Solve SolveFn `json:"-"`
}
func (c *Check) Check(ctx context.Context, logger util.Logger) *Result {
_, span, logger := telemetry.StartSpan(ctx, "doctor:check:"+c.Key, logger)
defer span.Complete()
if !c.Applies() {
return nil
}
r := NewResult(c, c.Key, c.Title, c.Summary)
timer := util.TimerStart()
r = c.Fn(ctx, r, logger)
r.Duration = timer.End()
r = c.Solve(ctx, r, logger)
return r
}
func (c *Check) Applies() bool {
if len(c.Platforms) == 0 {
return true
}
return lo.Contains(c.Platforms, runtime.GOOS) && (!lo.Contains(c.Platforms, "!"+runtime.GOOS))
}
type Checks []*Check
func (c Checks) Get(key string) *Check {
return lo.FindOrElse(c, nil, func(x *Check) bool {
return x.Key == key
})
}
func (c Checks) Keys() []string {
return lo.Map(c, func(c *Check, _ int) string {
return c.Key
})
}