Repository navigation
Expand file tree
/
Copy pathsysreq.go
More file actions
83 lines (69 loc) · 2.05 KB
/
Copy pathsysreq.go
File metadata and controls
83 lines (69 loc) · 2.05 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
// Package sysreq implements checking for Sourcegraph system requirements.
package sysreq
import (
"strings"
"context"
)
// Status describes the status of a system requirement.
type Status struct {
Name string // the required component
Problem string // if non-empty, a description of the problem
Fix string // if non-empty, how to fix the problem
Err error // if non-nil, the error encountered
Skipped bool // if true, indicates this check was skipped
}
// OK is whether the component is present, has no errors, and was not
// skipped.
func (s *Status) OK() bool {
return s.Problem == "" && s.Fix == "" && s.Err == nil && !s.Skipped
}
func (s *Status) Failed() bool { return s.Problem != "" || s.Err != nil }
// Check checks for the presence of system requirements, such as
// Docker and Git. The skip list contains case-insensitive names of
// requirement checks (such as "Docker" and "Git") that should be
// skipped.
func Check(ctx context.Context, skip []string) []Status {
shouldSkip := func(name string) bool {
for _, v := range skip {
if strings.EqualFold(name, v) {
return true
}
}
return false
}
statuses := make([]Status, len(checks))
for i, c := range checks {
statuses[i].Name = c.Name
if shouldSkip(c.Name) {
statuses[i].Skipped = true
continue
}
problem, fix, err := c.Check(ctx)
if err != nil {
statuses[i].Err = err
}
statuses[i].Problem = problem
statuses[i].Fix = fix
}
return statuses
}
type check struct {
Name string
Check CheckFunc
}
// CheckFunc is a function that checks for a system requirement. If
// any of problem, fix, or err are non-zero, then the system
// requirement check is deemed to have failed.
type CheckFunc func(context.Context) (problem, fix string, err error)
// AddCheck adds a new check that will be run when this package's
// Check func is called. It is used by other packages to specify
// system requirements.
func AddCheck(name string, fn CheckFunc) {
checks = append(checks, check{name, fn})
}
var checks = []check{
{
Name: "Rlimit",
Check: rlimitCheck,
},
}