Skip to content

Commit

Permalink
Fix crash when color was set to auto in config file
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Aug 3, 2021
1 parent 95def3f commit 42e1f2f
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 27 deletions.
13 changes: 2 additions & 11 deletions internal/cmd/autobool.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,10 @@ import (

type autoBool struct {
auto bool
autoFunc func() (bool, error)
value bool
valueErr error
}

// newAutoBool returns a new autoBool.
func newAutoBool(autoFunc func() (bool, error)) *autoBool {
return &autoBool{
auto: true,
autoFunc: autoFunc,
}
}

// Set implements github.com/spf13/pflag.Value.Set.
func (b *autoBool) Set(s string) error {
if strings.ToLower(s) == "auto" {
Expand Down Expand Up @@ -52,9 +43,9 @@ func (b *autoBool) Type() string {
}

// Value returns b's value, calling b's autoFunc if needed.
func (b *autoBool) Value() (bool, error) {
func (b *autoBool) Value(autoFunc func() (bool, error)) (bool, error) {
if b.auto {
b.value, b.valueErr = b.autoFunc()
b.value, b.valueErr = autoFunc()
b.auto = false
}
return b.value, b.valueErr
Expand Down
28 changes: 16 additions & 12 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ type Config struct {
DestDirAbsPath chezmoi.AbsPath `mapstructure:"destDir"`
Umask fs.FileMode `mapstructure:"umask"`
Remove bool `mapstructure:"remove"`
Color *autoBool `mapstructure:"color"`
Color autoBool `mapstructure:"color"`
Data map[string]interface{} `mapstructure:"data"`
Template templateConfig `mapstructure:"template"`
UseBuiltinGit *autoBool `mapstructure:"useBuiltinGit"`
UseBuiltinGit autoBool `mapstructure:"useBuiltinGit"`
Pager string `mapstructure:"pager"`
Interpreters map[string]*chezmoi.Interpreter `mapstructure:"interpreters"`

Expand Down Expand Up @@ -202,10 +202,16 @@ func newConfig(options ...configOption) (*Config, error) {
}

c := &Config{
bds: bds,
fileSystem: vfs.OSFS,
homeDir: userHomeDir,
Umask: chezmoi.Umask,
bds: bds,
fileSystem: vfs.OSFS,
homeDir: userHomeDir,
Umask: chezmoi.Umask,
Color: autoBool{
auto: true,
},
UseBuiltinGit: autoBool{
auto: true,
},
Pager: os.Getenv("PAGER"),
Interpreters: defaultInterpreters,
Add: addCmdConfig{
Expand Down Expand Up @@ -385,8 +391,6 @@ func newConfig(options ...configOption) (*Config, error) {
return nil, err
}
c.DestDirAbsPath = c.homeDirAbsPath
c.Color = newAutoBool(c.colorAutoFunc)
c.UseBuiltinGit = newAutoBool(c.useBuiltinGitAutoFunc)
c._import.destination = c.homeDirAbsPath

return c, nil
Expand Down Expand Up @@ -761,7 +765,7 @@ func (c *Config) destAbsPathInfos(sourceState *chezmoi.SourceState, args []strin
func (c *Config) diffFile(path chezmoi.RelPath, fromData []byte, fromMode fs.FileMode, toData []byte, toMode fs.FileMode) error {
var sb strings.Builder
unifiedEncoder := diff.NewUnifiedEncoder(&sb, diff.DefaultContextLines)
color, err := c.Color.Value()
color, err := c.Color.Value(c.colorAutoFunc)
if err != nil {
return err
}
Expand Down Expand Up @@ -975,11 +979,11 @@ func (c *Config) newRootCmd() (*cobra.Command, error) {

persistentFlags := rootCmd.PersistentFlags()

persistentFlags.Var(c.Color, "color", "Colorize output")
persistentFlags.Var(&c.Color, "color", "Colorize output")
persistentFlags.VarP(&c.DestDirAbsPath, "destination", "D", "Set destination directory")
persistentFlags.BoolVar(&c.Remove, "remove", c.Remove, "Remove entries from destination directory")
persistentFlags.VarP(&c.SourceDirAbsPath, "source", "S", "Set source directory")
persistentFlags.Var(c.UseBuiltinGit, "use-builtin-git", "Use builtin git")
persistentFlags.Var(&c.UseBuiltinGit, "use-builtin-git", "Use builtin git")
for _, key := range []string{
"color",
"destination",
Expand Down Expand Up @@ -1153,7 +1157,7 @@ func (c *Config) persistentPreRunRootE(cmd *cobra.Command, args []string) error
c.errorf("warning: %s: %v\n", c.configFileAbsPath, err)
}

color, err := c.Color.Value()
color, err := c.Color.Value(c.colorAutoFunc)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestParseConfig(t *testing.T) {
}, func(fileSystem vfs.FS) {
c := newTestConfig(t, fileSystem)
require.NoError(t, c.execute([]string{"init"}))
actualColor, err := c.Color.Value()
actualColor, err := c.Color.Value(c.colorAutoFunc)
assert.NoError(t, err)
assert.Equal(t, tc.expectedColor, actualColor)
})
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/diffcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c *Config) newDiffCmd() *cobra.Command {
func (c *Config) runDiffCmd(cmd *cobra.Command, args []string) error {
sb := strings.Builder{}
dryRunSystem := chezmoi.NewDryRunSystem(c.destSystem)
color, err := c.Color.Value()
color, err := c.Color.Value(c.colorAutoFunc)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/initcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (c *Config) runInitCmd(cmd *cobra.Command, args []string) error {
return err
}

useBuiltinGit, err := c.UseBuiltinGit.Value()
useBuiltinGit, err := c.UseBuiltinGit.Value(c.useBuiltinGitAutoFunc)
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/testdata/scripts/config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ chezmoi data --config=$CHEZMOICONFIGDIR/chezmoi.yaml --format=yaml
stdout 'sourceDir: .*/config2/source'

-- home2/user/.config/chezmoi/chezmoi.toml --
color = "auto"
sourceDir = "/config/source"
-- home2/user/.config/chezmoi/chezmoi.yaml --
color: auto
sourceDir: /config2/source
2 changes: 1 addition & 1 deletion internal/cmd/updatecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *Config) newUpdateCmd() *cobra.Command {
}

func (c *Config) runUpdateCmd(cmd *cobra.Command, args []string) error {
switch useBuiltinGit, err := c.UseBuiltinGit.Value(); {
switch useBuiltinGit, err := c.UseBuiltinGit.Value(c.useBuiltinGitAutoFunc); {
case err != nil:
return err
case useBuiltinGit:
Expand Down

0 comments on commit 42e1f2f

Please sign in to comment.