diff --git a/internal/cmd/autobool.go b/internal/cmd/autobool.go index 63c05d81a24..66ea9dfa9f0 100644 --- a/internal/cmd/autobool.go +++ b/internal/cmd/autobool.go @@ -1,12 +1,14 @@ package cmd import ( + "errors" "fmt" "reflect" "strconv" "strings" "github.com/mitchellh/mapstructure" + "gopkg.in/yaml.v3" "github.com/twpayne/chezmoi/v2/internal/chezmoi" ) @@ -71,6 +73,39 @@ func (b *autoBool) Type() string { return "bool|auto" } +// UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON. +func (b *autoBool) UnmarshalJSON(data []byte) error { + if string(data) == `"auto"` { + b.auto = true + return nil + } + value, err := chezmoi.ParseBool(string(data)) + if err != nil { + return err + } + b.auto = false + b.value = value + return nil +} + +// UnmarshalYAML implements gopkg.in/yaml.Unmarshaler.UnmarshalYAML. +func (b *autoBool) UnmarshalYAML(value *yaml.Node) error { + if value.Kind != yaml.ScalarNode { + return errors.New("expected scalar node") + } + if value.Value == "auto" { + b.auto = true + return nil + } + boolValue, err := chezmoi.ParseBool(value.Value) + if err != nil { + return err + } + b.auto = false + b.value = boolValue + return nil +} + // Value returns b's value, calling b's autoFunc if needed. func (b *autoBool) Value(autoFunc func() bool) bool { if b.auto {