Skip to content

Commit

Permalink
fix: Fix doctor command when --config is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Jan 8, 2025
1 parent 82e5c67 commit 940cc30
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 113 deletions.
9 changes: 7 additions & 2 deletions internal/cmd/addcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ func (c *Config) runAddCmd(cmd *cobra.Command, args []string, sourceState *chezm
return err
}

configFileAbsPath, err := c.getConfigFileAbsPath()
if err != nil {
return err
}

return sourceState.Add(
c.sourceSystem,
c.persistentState,
Expand All @@ -220,11 +225,11 @@ func (c *Config) runAddCmd(cmd *cobra.Command, args []string, sourceState *chezm
Filter: c.Add.filter,
OnIgnoreFunc: c.defaultOnIgnoreFunc,
PreAddFunc: c.defaultPreAddFunc,
ConfigFileAbsPath: c.getConfigFileAbsPath(),
ConfigFileAbsPath: configFileAbsPath,
ProtectedAbsPaths: []chezmoi.AbsPath{
c.CacheDirAbsPath,
c.WorkingTreeAbsPath,
c.getConfigFileAbsPath().Dir(),
configFileAbsPath.Dir(),
persistentStateFileAbsPath,
c.sourceDirAbsPath,
chezmoi.NewAbsPath(executable),
Expand Down
6 changes: 5 additions & 1 deletion internal/cmd/catconfigcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ func (c *Config) newCatConfigCmd() *cobra.Command {
}

func (c *Config) runCatConfigCmd(cmd *cobra.Command, args []string) error {
data, err := c.baseSystem.ReadFile(c.getConfigFileAbsPath())
configFileAbsPath, err := c.getConfigFileAbsPath()
if err != nil {
return err
}
data, err := c.baseSystem.ReadFile(configFileAbsPath)
if err != nil {
return err
}
Expand Down
59 changes: 28 additions & 31 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,11 +555,11 @@ func newConfig(options ...configOption) (*Config, error) {
return c, nil
}

func (c *Config) getConfigFileAbsPath() chezmoi.AbsPath {
func (c *Config) getConfigFileAbsPath() (chezmoi.AbsPath, error) {
if c.customConfigFileAbsPath.Empty() {
return c.defaultConfigFileAbsPath
return c.defaultConfigFileAbsPath, c.defaultConfigFileAbsPathErr
}
return c.customConfigFileAbsPath
return c.customConfigFileAbsPath, nil
}

// Close closes resources associated with c.
Expand Down Expand Up @@ -1894,15 +1894,19 @@ func (c *Config) persistentPostRunRootE(cmd *cobra.Command, args []string) error

// Verify modified config.
if annotations.hasTag(modifiesConfigFile) {
configFileContents, err := c.baseSystem.ReadFile(c.getConfigFileAbsPath())
configFileAbsPath, err := c.getConfigFileAbsPath()
if err != nil {
return err
}
configFileContents, err := c.baseSystem.ReadFile(configFileAbsPath)
switch {
case errors.Is(err, fs.ErrNotExist):
err = nil
case err != nil:
// err is already set, do nothing.
default:
var format chezmoi.Format
if format, err = chezmoi.FormatFromAbsPath(c.getConfigFileAbsPath()); err == nil {
if format, err = chezmoi.FormatFromAbsPath(configFileAbsPath); err == nil {
var config map[string]any
if err = format.Unmarshal(configFileContents, &config); err != nil { //nolint:revive
// err is already set, do nothing.
Expand All @@ -1912,7 +1916,7 @@ func (c *Config) persistentPostRunRootE(cmd *cobra.Command, args []string) error
}
}
if err != nil {
c.errorf("warning: %s: %v\n", c.getConfigFileAbsPath(), err)
c.errorf("warning: %s: %v\n", configFileAbsPath, err)
}
}

Expand Down Expand Up @@ -2035,15 +2039,16 @@ func (c *Config) persistentPreRunRootE(cmd *cobra.Command, args []string) error

// Read the config file.
if annotations.hasTag(doesNotRequireValidConfig) {
if c.defaultConfigFileAbsPathErr == nil {
_ = c.readConfig()
if configFileAbsPath, err := c.getConfigFileAbsPath(); err == nil {
_ = c.readConfig(configFileAbsPath)
}
} else {
if c.defaultConfigFileAbsPathErr != nil {
return c.defaultConfigFileAbsPathErr
configFileAbsPath, err := c.getConfigFileAbsPath()
if err != nil {
return err
}
if err := c.readConfig(); err != nil {
return fmt.Errorf("invalid config: %s: %w", c.getConfigFileAbsPath(), err)
if err := c.readConfig(configFileAbsPath); err != nil {
return fmt.Errorf("invalid config: %s: %w", configFileAbsPath, err)
}
}

Expand Down Expand Up @@ -2191,7 +2196,11 @@ func (c *Config) persistentPreRunRootE(cmd *cobra.Command, args []string) error

// Create the config directory if needed.
if annotations.hasTag(requiresConfigDirectory) {
if err := chezmoi.MkdirAll(c.baseSystem, c.getConfigFileAbsPath().Dir(), fs.ModePerm); err != nil {
configFileAbsPath, err := c.getConfigFileAbsPath()
if err != nil {
return err
}
if err := chezmoi.MkdirAll(c.baseSystem, configFileAbsPath.Dir(), fs.ModePerm); err != nil {
return err
}
}
Expand Down Expand Up @@ -2316,24 +2325,11 @@ func (c *Config) persistentStateFile() (chezmoi.AbsPath, error) {
if !c.PersistentStateAbsPath.Empty() {
return c.PersistentStateAbsPath, nil
}
if !c.getConfigFileAbsPath().Empty() {
return c.getConfigFileAbsPath().Dir().Join(persistentStateFileRelPath), nil
}
for _, configDir := range c.bds.ConfigDirs {
configDirAbsPath, err := chezmoi.NewAbsPathFromExtPath(configDir, c.homeDirAbsPath)
if err != nil {
return chezmoi.EmptyAbsPath, err
}
persistentStateFile := configDirAbsPath.Join(chezmoiRelPath, persistentStateFileRelPath)
if _, err := os.Stat(persistentStateFile.String()); err == nil {
return persistentStateFile, nil
}
}
defaultConfigFileAbsPath, err := c.defaultConfigFile(c.fileSystem, c.bds)
configFileAbsPath, err := c.getConfigFileAbsPath()
if err != nil {
return chezmoi.EmptyAbsPath, err
}
return defaultConfigFileAbsPath.Dir().Join(persistentStateFileRelPath), nil
return configFileAbsPath.Dir().Join(persistentStateFileRelPath), nil
}

// progressAutoFunc detects whether progress bars should be displayed.
Expand Down Expand Up @@ -2422,6 +2418,7 @@ func (c *Config) newTemplateData(cmd *cobra.Command) *templateData {
}
}

configFileAbsPath, _ := c.getConfigFileAbsPath()
executable, _ := os.Executable()
windowsVersion, _ := windowsVersion()
sourceDirAbsPath, _ := c.getSourceDirAbsPath(nil)
Expand All @@ -2433,7 +2430,7 @@ func (c *Config) newTemplateData(cmd *cobra.Command) *templateData {
command: cmd.Name(),
commandDir: c.commandDirAbsPath,
config: c.ConfigFile.toMap(),
configFile: c.getConfigFileAbsPath(),
configFile: configFileAbsPath,
destDir: c.DestDirAbsPath,
executable: chezmoi.NewAbsPath(executable),
fqdnHostname: fqdnHostname,
Expand Down Expand Up @@ -2461,8 +2458,8 @@ func (c *Config) newTemplateData(cmd *cobra.Command) *templateData {
}

// readConfig reads the config file, if it exists.
func (c *Config) readConfig() error {
switch err := c.decodeConfigFile(c.getConfigFileAbsPath(), &c.ConfigFile); {
func (c *Config) readConfig(configFileAbsPath chezmoi.AbsPath) error {
switch err := c.decodeConfigFile(configFileAbsPath, &c.ConfigFile); {
case errors.Is(err, fs.ErrNotExist):
return nil
default:
Expand Down
Loading

0 comments on commit 940cc30

Please sign in to comment.