Skip to content

Commit

Permalink
Make doctor command more friendly about missing binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Mar 22, 2021
1 parent c500b8a commit 1833b11
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
48 changes: 33 additions & 15 deletions cmd/doctorcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ type checkResult int
const (
checkSkipped checkResult = -1 // The check was skipped.
checkOK checkResult = 0 // The check completed and did not find any problems.
checkWarning checkResult = 1 // The check completed and found something that might indicate a problem.
checkError checkResult = 2 // The check completed and found a definite problem.
checkFailed checkResult = 3 // The check could not be completed.
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.
Expand All @@ -40,6 +41,7 @@ type check interface {
var checkResultStr = map[checkResult]string{
checkSkipped: "skipped",
checkOK: "ok",
checkInfo: "info",
checkWarning: "warning",
checkError: "error",
checkFailed: "failed",
Expand All @@ -50,6 +52,7 @@ var checkResultStr = map[checkResult]string{
type binaryCheck struct {
name string
binaryname string
ifNotSet checkResult
ifNotExist checkResult
versionArgs []string
versionRx *regexp.Regexp
Expand All @@ -66,6 +69,7 @@ type dirCheck struct {
type fileCheck struct {
name string
filename string
ifNotSet checkResult
ifNotExist checkResult
}

Expand Down Expand Up @@ -113,7 +117,7 @@ func (c *Config) runDoctorCmd(cmd *cobra.Command, args []string) error {
&fileCheck{
name: "config-file",
filename: string(c.configFileAbsPath),
ifNotExist: checkWarning,
ifNotExist: checkInfo,
},
&dirCheck{
name: "source-dir",
Expand All @@ -129,89 +133,103 @@ func (c *Config) runDoctorCmd(cmd *cobra.Command, args []string) error {
&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: "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,
ifNotExist: checkWarning,
ifNotSet: checkWarning,
ifNotExist: checkInfo,
versionArgs: []string{"--version"},
versionRx: regexp.MustCompile(`^(\d+\.\d+\.\d+)`),
},
&binaryCheck{
name: "bitwarden-cli",
binaryname: c.Bitwarden.Command,
ifNotExist: checkWarning,
ifNotSet: checkWarning,
ifNotExist: checkInfo,
versionArgs: []string{"--version"},
versionRx: regexp.MustCompile(`^(\d+\.\d+\.\d+)`),
},
&binaryCheck{
name: "gopass-cli",
binaryname: c.Gopass.Command,
ifNotExist: checkWarning,
ifNotSet: checkWarning,
ifNotExist: checkInfo,
versionArgs: gopassVersionArgs,
versionRx: gopassVersionRx,
minVersion: &gopassMinVersion,
},
&binaryCheck{
name: "keepassxc-cli",
binaryname: c.Keepassxc.Command,
ifNotExist: checkWarning,
ifNotSet: checkWarning,
ifNotExist: checkInfo,
versionArgs: []string{"--version"},
versionRx: regexp.MustCompile(`^(\d+\.\d+\.\d+)`),
},
&fileCheck{
name: "keepassxc-db",
filename: c.Keepassxc.Database,
ifNotExist: checkWarning,
ifNotSet: checkInfo,
ifNotExist: checkInfo,
},
&binaryCheck{
name: "lastpass-cli",
binaryname: c.Lastpass.Command,
ifNotExist: checkWarning,
ifNotSet: checkWarning,
ifNotExist: checkInfo,
versionArgs: lastpassVersionArgs,
versionRx: lastpassVersionRx,
minVersion: &lastpassMinVersion,
},
&binaryCheck{
name: "pass-cli",
binaryname: c.Pass.Command,
ifNotExist: checkWarning,
ifNotSet: checkWarning,
ifNotExist: checkInfo,
versionArgs: []string{"version"},
versionRx: regexp.MustCompile(`(?m)=\s*v(\d+\.\d+\.\d+)`),
},
&binaryCheck{
name: "vault-cli",
binaryname: c.Vault.Command,
ifNotExist: checkWarning,
ifNotSet: checkWarning,
ifNotExist: checkInfo,
versionArgs: []string{"version"},
versionRx: regexp.MustCompile(`^Vault\s+v(\d+\.\d+\.\d+)`),
},
&binaryCheck{
name: "secret-cli",
binaryname: c.Secret.Command,
ifNotExist: checkWarning,
ifNotSet: checkInfo,
ifNotExist: checkInfo,
},
}

Expand Down Expand Up @@ -241,7 +259,7 @@ func (c *binaryCheck) Name() string {

func (c *binaryCheck) Run() (checkResult, string) {
if c.binaryname == "" {
return checkWarning, "not set"
return c.ifNotSet, "not set"
}

path, err := exec.LookPath(c.binaryname)
Expand Down Expand Up @@ -299,7 +317,7 @@ func (c *fileCheck) Name() string {

func (c *fileCheck) Run() (checkResult, string) {
if c.filename == "" {
return checkWarning, "not set"
return c.ifNotSet, "not set"
}

_, err := os.ReadFile(c.filename)
Expand Down
2 changes: 1 addition & 1 deletion testdata/scripts/doctor.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ stdout 'ok\s+1password-cli\s+'
stdout 'ok\s+bitwarden-cli\s+'
stdout 'ok\s+gopass-cli\s+'
stdout 'ok\s+keepassxc-cli\s+'
stdout 'warning\s+keepassxc-db\s+'
stdout 'info\s+keepassxc-db\s+'
stdout 'ok\s+lastpass-cli\s+'
stdout 'ok\s+pass-cli\s+'
stdout 'ok\s+vault-cli\s+'
Expand Down

0 comments on commit 1833b11

Please sign in to comment.