From 4dac91f0d97a3f699394443a4697d16cf5dcc85e Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Tue, 25 Jun 2024 22:23:23 +0200 Subject: [PATCH] feat: Add --no-network flag to doctor command --- .../docs/reference/commands/doctor.md | 4 +++ internal/cmd/config.go | 1 + internal/cmd/doctorcmd.go | 29 ++++++++++++++----- internal/cmd/doctorcmd_unix.go | 4 +-- internal/cmd/doctorcmd_windows.go | 4 +-- 5 files changed, 30 insertions(+), 12 deletions(-) diff --git a/assets/chezmoi.io/docs/reference/commands/doctor.md b/assets/chezmoi.io/docs/reference/commands/doctor.md index caafbae7305..6b29f04f0ba 100644 --- a/assets/chezmoi.io/docs/reference/commands/doctor.md +++ b/assets/chezmoi.io/docs/reference/commands/doctor.md @@ -2,6 +2,10 @@ Check for potential problems. +## `--no-network` + +Do not use any network connections. + !!! example ```console diff --git a/internal/cmd/config.go b/internal/cmd/config.go index 88d92c73250..f39a6a03cbb 100644 --- a/internal/cmd/config.go +++ b/internal/cmd/config.go @@ -193,6 +193,7 @@ type Config struct { archive archiveCmdConfig chattr chattrCmdConfig destroy destroyCmdConfig + doctor doctorCmdConfig dump dumpCmdConfig executeTemplate executeTemplateCmdConfig ignored ignoredCmdConfig diff --git a/internal/cmd/doctorcmd.go b/internal/cmd/doctorcmd.go index cbce95a106b..dd92aa984f2 100644 --- a/internal/cmd/doctorcmd.go +++ b/internal/cmd/doctorcmd.go @@ -35,6 +35,7 @@ import ( type checkResult int const ( + checkResultOmitted checkResult = -3 // The check was omitted. checkResultFailed checkResult = -2 // The check could not be completed. checkResultSkipped checkResult = -1 // The check was skipped. checkResultOK checkResult = 0 // The check completed and did not find any problems. @@ -60,6 +61,7 @@ type check interface { } var checkResultStr = map[checkResult]string{ + checkResultOmitted: "omitted", checkResultFailed: "failed", checkResultSkipped: "skipped", checkResultOK: "ok", @@ -117,6 +119,7 @@ type goVersionCheck struct{} // A latestVersionCheck checks the latest version. type latestVersionCheck struct { + network bool httpClient *http.Client httpClientErr error version semver.Version @@ -125,8 +128,8 @@ type latestVersionCheck struct { // An osArchCheck checks that runtime.GOOS and runtime.GOARCH are supported. type osArchCheck struct{} -// A skippedCheck is a check that is skipped. -type skippedCheck struct{} +// A omittedCheck is a check that is omitted. +type omittedCheck struct{} // A suspiciousEntriesCheck checks that a source directory does not contain any // suspicious files. @@ -143,6 +146,10 @@ type versionCheck struct { versionStr string } +type doctorCmdConfig struct { + noNetwork bool +} + func (c *Config) newDoctorCmd() *cobra.Command { doctorCmd := &cobra.Command{ Args: cobra.NoArgs, @@ -157,6 +164,8 @@ func (c *Config) newDoctorCmd() *cobra.Command { ), } + doctorCmd.PersistentFlags().BoolVar(&c.doctor.noNetwork, "no-network", c.doctor.noNetwork, "do not use network connection") + return doctorCmd } @@ -176,6 +185,7 @@ func (c *Config) runDoctorCmd(cmd *cobra.Command, args []string) error { versionStr: c.versionStr, }, &latestVersionCheck{ + network: !c.doctor.noNetwork, httpClient: httpClient, httpClientErr: httpClientErr, version: c.version, @@ -422,7 +432,7 @@ func (c *Config) runDoctorCmd(cmd *cobra.Command, args []string) error { fmt.Fprint(resultWriter, "RESULT\tCHECK\tMESSAGE\n") for _, check := range checks { checkResult, message := check.Run(c.baseSystem, homeDirAbsPath) - if checkResult == checkResultSkipped { + if checkResult == checkResultOmitted { continue } // Conceal the user's actual home directory in the message as the @@ -653,7 +663,10 @@ func (c *latestVersionCheck) Name() string { } func (c *latestVersionCheck) Run(system chezmoi.System, homeDirAbsPath chezmoi.AbsPath) (checkResult, string) { - if c.httpClientErr != nil { + switch { + case !c.network: + return checkResultSkipped, "no network" + case c.httpClientErr != nil: return checkResultFailed, c.httpClientErr.Error() } @@ -704,12 +717,12 @@ func (osArchCheck) Run(system chezmoi.System, homeDirAbsPath chezmoi.AbsPath) (c return checkResultOK, strings.Join(fields, " ") } -func (skippedCheck) Name() string { +func (omittedCheck) Name() string { return "skipped" } -func (skippedCheck) Run(system chezmoi.System, homeDirAbsPath chezmoi.AbsPath) (checkResult, string) { - return checkResultSkipped, "" +func (omittedCheck) Run(system chezmoi.System, homeDirAbsPath chezmoi.AbsPath) (checkResult, string) { + return checkResultOmitted, "" } func (c *suspiciousEntriesCheck) Name() string { @@ -759,7 +772,7 @@ func (upgradeMethodCheck) Run(system chezmoi.System, homeDirAbsPath chezmoi.AbsP return checkResultFailed, err.Error() } if method == "" { - return checkResultSkipped, "" + return checkResultOmitted, "" } return checkResultOK, method } diff --git a/internal/cmd/doctorcmd_unix.go b/internal/cmd/doctorcmd_unix.go index 79765bcc2bc..e3d7e28d7a1 100644 --- a/internal/cmd/doctorcmd_unix.go +++ b/internal/cmd/doctorcmd_unix.go @@ -17,7 +17,7 @@ import ( ) type ( - systeminfoCheck struct{ skippedCheck } + systeminfoCheck struct{ omittedCheck } umaskCheck struct{} unameCheck struct{} ) @@ -42,7 +42,7 @@ func (unameCheck) Name() string { func (unameCheck) Run(system chezmoi.System, homeDirAbsPath chezmoi.AbsPath) (checkResult, string) { if runtime.GOOS == "windows" { - return checkResultSkipped, "" + return checkResultOmitted, "" } cmd := exec.Command("uname", "-a") cmd.Stderr = os.Stderr diff --git a/internal/cmd/doctorcmd_windows.go b/internal/cmd/doctorcmd_windows.go index 92dcc4b16df..cffba14c199 100644 --- a/internal/cmd/doctorcmd_windows.go +++ b/internal/cmd/doctorcmd_windows.go @@ -14,8 +14,8 @@ import ( type ( systeminfoCheck struct{} - umaskCheck struct{ skippedCheck } - unameCheck struct{ skippedCheck } + umaskCheck struct{ omittedCheck } + unameCheck struct{ omittedCheck } ) func (systeminfoCheck) Name() string {