-
Notifications
You must be signed in to change notification settings - Fork 464
/
doctorcmd.go
411 lines (369 loc) · 10.4 KB
/
doctorcmd.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package cmd
import (
"errors"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"text/tabwriter"
"github.com/coreos/go-semver/semver"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/twpayne/go-shell"
"github.com/twpayne/go-vfs/v3"
"github.com/twpayne/chezmoi/v2/internal/chezmoi"
"github.com/twpayne/chezmoi/v2/internal/chezmoilog"
)
// A checkResult is the result of a check.
type checkResult int
const (
checkSkipped checkResult = -1 // The check was skipped.
checkOK checkResult = 0 // The check completed and did not find any problems.
checkInfo checkResult = 1 // The check completed and found something interesting, but not a problem.
checkWarning checkResult = 2 // The check completed and found something that might indicate a problem.
checkError checkResult = 3 // The check completed and found a definite problem.
checkFailed checkResult = 4 // The check could not be completed.
)
// A check is an individual check.
type check interface {
Name() string // Name returns the check's name.
Run() (checkResult, string) // Run runs the check.
}
var checkResultStr = map[checkResult]string{
checkSkipped: "skipped",
checkOK: "ok",
checkInfo: "info",
checkWarning: "warning",
checkError: "error",
checkFailed: "failed",
}
// A binaryCheck checks that a binary called name is installed and optionally at
// least version minVersion.
type binaryCheck struct {
name string
binaryname string
ifNotSet checkResult
ifNotExist checkResult
versionArgs []string
versionRx *regexp.Regexp
minVersion *semver.Version
}
// A dirCheck checks that a directory exists.
type dirCheck struct {
name string
dirname string
}
// An executableCheck checks the executable.
type executableCheck struct{}
// A fileCheck checks that a file exists.
type fileCheck struct {
name string
filename string
ifNotSet checkResult
ifNotExist checkResult
}
// An osArchCheck checks that runtime.GOOS and runtime.GOARCH are supported.
type osArchCheck struct{}
// A suspiciousEntriesCheck checks that a source directory does not contain any
// suspicious files.
type suspiciousEntriesCheck struct {
dirname string
}
// A versionCheck checks the version information.
type versionCheck struct {
versionInfo VersionInfo
versionStr string
}
func (c *Config) newDoctorCmd() *cobra.Command {
doctorCmd := &cobra.Command{
Args: cobra.NoArgs,
Use: "doctor",
Short: "Check your system for potential problems",
Example: example("doctor"),
Long: mustLongHelp("doctor"),
RunE: c.runDoctorCmd,
Annotations: map[string]string{
doesNotRequireValidConfig: "true",
runsCommands: "true",
},
}
return doctorCmd
}
func (c *Config) runDoctorCmd(cmd *cobra.Command, args []string) error {
shell, _ := shell.CurrentUserShell()
editor, _ := c.editor()
checks := []check{
&versionCheck{
versionInfo: c.versionInfo,
versionStr: c.versionStr,
},
&osArchCheck{},
&executableCheck{},
&fileCheck{
name: "config-file",
filename: string(c.configFileAbsPath),
ifNotExist: checkInfo,
},
&dirCheck{
name: "source-dir",
dirname: string(c.SourceDirAbsPath),
},
&suspiciousEntriesCheck{
dirname: string(c.SourceDirAbsPath),
},
&dirCheck{
name: "dest-dir",
dirname: string(c.DestDirAbsPath),
},
&binaryCheck{
name: "shell",
binaryname: shell,
ifNotSet: checkError,
},
&binaryCheck{
name: "editor",
binaryname: editor,
ifNotSet: checkWarning,
},
&binaryCheck{
name: "git-cli",
binaryname: c.Git.Command,
ifNotSet: checkWarning,
ifNotExist: checkWarning,
versionArgs: []string{"--version"},
versionRx: regexp.MustCompile(`^git\s+version\s+(\d+\.\d+\.\d+)`),
},
&binaryCheck{
name: "merge-cli",
binaryname: c.Merge.Command,
ifNotSet: checkWarning,
ifNotExist: checkWarning,
},
&binaryCheck{
name: "age-cli",
binaryname: "age",
versionArgs: []string{"-version"},
versionRx: regexp.MustCompile(`v(\d+\.\d+\.\d+\S*)`),
ifNotSet: checkWarning,
},
&binaryCheck{
name: "gnupg-cli",
binaryname: "gpg",
versionArgs: []string{"--version"},
versionRx: regexp.MustCompile(`^gpg\s+\(.*?\)\s+(\d+\.\d+\.\d+)`),
ifNotSet: checkWarning,
},
&binaryCheck{
name: "1password-cli",
binaryname: c.Onepassword.Command,
ifNotSet: checkWarning,
ifNotExist: checkInfo,
versionArgs: []string{"--version"},
versionRx: regexp.MustCompile(`^(\d+\.\d+\.\d+)`),
},
&binaryCheck{
name: "bitwarden-cli",
binaryname: c.Bitwarden.Command,
ifNotSet: checkWarning,
ifNotExist: checkInfo,
versionArgs: []string{"--version"},
versionRx: regexp.MustCompile(`^(\d+\.\d+\.\d+)`),
},
&binaryCheck{
name: "gopass-cli",
binaryname: c.Gopass.Command,
ifNotSet: checkWarning,
ifNotExist: checkInfo,
versionArgs: gopassVersionArgs,
versionRx: gopassVersionRx,
minVersion: &gopassMinVersion,
},
&binaryCheck{
name: "keepassxc-cli",
binaryname: c.Keepassxc.Command,
ifNotSet: checkWarning,
ifNotExist: checkInfo,
versionArgs: []string{"--version"},
versionRx: regexp.MustCompile(`^(\d+\.\d+\.\d+)`),
},
&fileCheck{
name: "keepassxc-db",
filename: c.Keepassxc.Database,
ifNotSet: checkInfo,
ifNotExist: checkInfo,
},
&binaryCheck{
name: "lastpass-cli",
binaryname: c.Lastpass.Command,
ifNotSet: checkWarning,
ifNotExist: checkInfo,
versionArgs: lastpassVersionArgs,
versionRx: lastpassVersionRx,
minVersion: &lastpassMinVersion,
},
&binaryCheck{
name: "pass-cli",
binaryname: c.Pass.Command,
ifNotSet: checkWarning,
ifNotExist: checkInfo,
versionArgs: []string{"version"},
versionRx: regexp.MustCompile(`(?m)=\s*v(\d+\.\d+\.\d+)`),
},
&binaryCheck{
name: "vault-cli",
binaryname: c.Vault.Command,
ifNotSet: checkWarning,
ifNotExist: checkInfo,
versionArgs: []string{"version"},
versionRx: regexp.MustCompile(`^Vault\s+v(\d+\.\d+\.\d+)`),
},
&binaryCheck{
name: "secret-cli",
binaryname: c.Secret.Command,
ifNotSet: checkInfo,
ifNotExist: checkInfo,
},
}
worstResult := checkOK
resultWriter := tabwriter.NewWriter(c.stdout, 3, 0, 3, ' ', 0)
fmt.Fprint(resultWriter, "RESULT\tCHECK\tMESSAGE\n")
for _, check := range checks {
checkResult, message := check.Run()
fmt.Fprintf(resultWriter, "%s\t%s\t%s\n", checkResultStr[checkResult], check.Name(), message)
if checkResult > worstResult {
worstResult = checkResult
}
}
resultWriter.Flush()
if worstResult > checkWarning {
return ErrExitCode(1)
}
return nil
}
func (c *binaryCheck) Name() string {
return c.name
}
func (c *binaryCheck) Run() (checkResult, string) {
if c.binaryname == "" {
return c.ifNotSet, "not set"
}
path, err := exec.LookPath(c.binaryname)
switch {
case errors.Is(err, exec.ErrNotFound):
return c.ifNotExist, fmt.Sprintf("%s not found in $PATH", c.binaryname)
case err != nil:
return checkFailed, err.Error()
}
if c.versionArgs == nil {
return checkOK, fmt.Sprintf("found %s", path)
}
cmd := exec.Command(path, c.versionArgs...)
output, err := chezmoilog.LogCmdCombinedOutput(log.Logger, cmd)
if err != nil {
return checkFailed, err.Error()
}
versionBytes := output
if c.versionRx != nil {
match := c.versionRx.FindSubmatch(versionBytes)
if len(match) != 2 {
return checkFailed, fmt.Sprintf("found %s, could not parse version from %s", path, versionBytes)
}
versionBytes = match[1]
}
version, err := semver.NewVersion(string(versionBytes))
if err != nil {
return checkFailed, err.Error()
}
if c.minVersion != nil && version.LessThan(*c.minVersion) {
return checkError, fmt.Sprintf("found %s, version %s, need %s", path, version, c.minVersion)
}
return checkOK, fmt.Sprintf("found %s, version %s", path, version)
}
func (c *dirCheck) Name() string {
return c.name
}
func (c *dirCheck) Run() (checkResult, string) {
if _, err := os.ReadDir(c.dirname); err != nil {
return checkError, fmt.Sprintf("%s: %v", c.dirname, err)
}
return checkOK, fmt.Sprintf("%s is a directory", c.dirname)
}
func (c *executableCheck) Name() string {
return "executable"
}
func (c *executableCheck) Run() (checkResult, string) {
executable, err := os.Executable()
if err != nil {
return checkError, err.Error()
}
return checkOK, executable
}
func (c *fileCheck) Name() string {
return c.name
}
func (c *fileCheck) Run() (checkResult, string) {
if c.filename == "" {
return c.ifNotSet, "not set"
}
_, err := os.ReadFile(c.filename)
switch {
case errors.Is(err, fs.ErrNotExist):
return c.ifNotExist, fmt.Sprintf("%s does not exist", c.filename)
case err != nil:
return checkError, fmt.Sprintf("%s: %v", c.filename, err)
default:
return checkOK, fmt.Sprintf("%s is a file", c.filename)
}
}
func (osArchCheck) Name() string {
return "os-arch"
}
func (osArchCheck) Run() (checkResult, string) {
fields := []string{runtime.GOOS + "/" + runtime.GOARCH}
if osRelease, err := chezmoi.OSRelease(vfs.OSFS); err == nil {
if name, ok := osRelease["NAME"].(string); ok {
if version, ok := osRelease["VERSION"].(string); ok {
fields = append(fields, "("+name+"/"+version+")")
}
}
}
return checkOK, strings.Join(fields, " ")
}
func (c *suspiciousEntriesCheck) Name() string {
return "suspicious-entries"
}
func (c *suspiciousEntriesCheck) Run() (checkResult, string) {
// FIXME check that config file templates are in root
var suspiciousEntries []string
if err := filepath.Walk(c.dirname, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if chezmoi.SuspiciousSourceDirEntry(filepath.Base(path), info) {
suspiciousEntries = append(suspiciousEntries, path)
}
return nil
}); err != nil {
return checkError, err.Error()
}
if len(suspiciousEntries) > 0 {
return checkWarning, fmt.Sprintf("found suspicious entries in %s: %s", c.dirname, strings.Join(suspiciousEntries, ", "))
}
return checkOK, fmt.Sprintf("no suspicious entries found in %s", c.dirname)
}
func (c *versionCheck) Name() string {
return "version"
}
func (c *versionCheck) Run() (checkResult, string) {
if c.versionInfo.Version == "" ||
c.versionInfo.Commit == "" ||
c.versionInfo.Date == "" ||
c.versionInfo.BuiltBy == "" {
return checkWarning, c.versionStr
}
return checkOK, c.versionStr
}