Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add --no-network flag to doctor command #3831

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions assets/chezmoi.io/docs/reference/commands/doctor.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Check for potential problems.

## `--no-network`

Do not use any network connections.

!!! example

```console
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ type Config struct {
archive archiveCmdConfig
chattr chattrCmdConfig
destroy destroyCmdConfig
doctor doctorCmdConfig
dump dumpCmdConfig
executeTemplate executeTemplateCmdConfig
ignored ignoredCmdConfig
Expand Down
31 changes: 22 additions & 9 deletions internal/cmd/doctorcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -60,6 +61,7 @@ type check interface {
}

var checkResultStr = map[checkResult]string{
checkResultOmitted: "omitted",
checkResultFailed: "failed",
checkResultSkipped: "skipped",
checkResultOK: "ok",
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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,
Expand All @@ -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
}

Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}

Expand Down Expand Up @@ -704,12 +717,12 @@ func (osArchCheck) Run(system chezmoi.System, homeDirAbsPath chezmoi.AbsPath) (c
return checkResultOK, strings.Join(fields, " ")
}

func (skippedCheck) Name() string {
return "skipped"
func (omittedCheck) Name() string {
return "omitted"
}

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 {
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/doctorcmd_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

type (
systeminfoCheck struct{ skippedCheck }
systeminfoCheck struct{ omittedCheck }
umaskCheck struct{}
unameCheck struct{}
)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/doctorcmd_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

type (
systeminfoCheck struct{}
umaskCheck struct{ skippedCheck }
unameCheck struct{ skippedCheck }
umaskCheck struct{ omittedCheck }
unameCheck struct{ omittedCheck }
)

func (systeminfoCheck) Name() string {
Expand Down
Loading