-
Notifications
You must be signed in to change notification settings - Fork 198
/
ensure.go
108 lines (88 loc) · 2.82 KB
/
ensure.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
package tools
import (
"bytes"
"context"
"errors"
"fmt"
"log"
osexec "os/exec"
)
// missingToolErrors wraps a set of errors discovered when
// probing for tools and implements the Error interface to pretty
// print the underlying errors. We use this instead of the existing
// `multierr` package we use elsewhere, because we want to control
// the error string (the default one produced by multierr is not
// as nice as what we do here).
type missingToolErrors struct {
errs []error
}
func (m *missingToolErrors) Error() string {
buf := bytes.Buffer{}
fmt.Fprintf(&buf, "required external tools are missing:")
for _, err := range m.errs {
fmt.Fprintf(&buf, "\n - %s", err.Error())
}
return buf.String()
}
// EnsureInstalled checks that all tools are installed, returning an
// error if one or more tools are not.
func EnsureInstalled(ctx context.Context, tools ...ExternalTool) error {
var allErrors []error
errorsEncountered := map[string]struct{}{}
confirmedTools := make(map[string]struct{})
if fromCtx, ok := ctx.Value(installedCheckCacheKey).(map[string]struct{}); ok && fromCtx != nil {
confirmedTools = fromCtx
}
for _, tool := range tools {
_, ok := confirmedTools[tool.Name()]
if ok {
log.Printf("Skipping install check for '%s'. It was previously confirmed.", tool.Name())
continue
}
err := tool.CheckInstalled(ctx)
var errSem *ErrSemver
if errors.As(err, &errSem) {
errorMsg := err.Error()
if _, hasV := errorsEncountered[errorMsg]; !hasV {
allErrors = append(allErrors, err)
errorsEncountered[errorMsg] = struct{}{}
}
} else if errors.Is(err, osexec.ErrNotFound) {
allErrors = append(
allErrors, fmt.Errorf("%s is not installed, see %s to install", tool.Name(), tool.InstallUrl()))
} else if err != nil {
errorMsg := err.Error()
if _, hasV := errorsEncountered[errorMsg]; !hasV {
allErrors = append(allErrors, fmt.Errorf("error checking for external tool %s: %w", tool.Name(), err))
errorsEncountered[errorMsg] = struct{}{}
}
}
// Mark the current tool as confirmed
confirmedTools[tool.Name()] = struct{}{}
}
if len(allErrors) > 0 {
return &missingToolErrors{errs: allErrors}
}
return nil
}
// Unique filters a slice of tools such that a tool with a
// given name only appears once.
func Unique(tools []ExternalTool) []ExternalTool {
uniqueToolsMap := make(map[string]struct{})
var uniqueTools []ExternalTool
for _, tool := range tools {
name := tool.Name()
if _, has := uniqueToolsMap[name]; !has {
uniqueToolsMap[name] = struct{}{}
uniqueTools = append(uniqueTools, tool)
}
}
return uniqueTools
}
type confirmCacheKey string
const (
installedCheckCacheKey confirmCacheKey = "checkCache"
)
func WithInstalledCheckCache(ctx context.Context) context.Context {
return context.WithValue(ctx, installedCheckCacheKey, make(map[string]struct{}))
}