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

fix: Ensure that all .chezmoi.config template variables have simple types #3609

Merged
merged 1 commit into from
Feb 29, 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
10 changes: 9 additions & 1 deletion internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2783,8 +2783,16 @@ func (f *ConfigFile) toMap() map[string]any {
configFile.Diff.Pager = ""
}

// This is a horrible hack. We want the returned map to contain only simple
// types because they are used with masterminds/sprig template functions
// which don't accept fmt.Stringers in place of strings. As a work-around,
// round-trip via JSON.
data, err := json.Marshal(configFile)
if err != nil {
return nil
}
var result map[string]any
if err := mapstructure.Decode(configFile, &result); err != nil {
if err := json.Unmarshal(data, &result); err != nil {
panic(err)
}
return result
Expand Down
12 changes: 6 additions & 6 deletions internal/cmd/testdata/scripts/addautotemplate.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ exec chezmoi add --autotemplate $HOME${/}.symlink
cmp $CHEZMOISOURCEDIR/symlink_dot_symlink.tmpl golden/symlink_dot_symlink.tmpl

# test that chezmoi add --autotemplate does not create a template if no replacements occurred
exec chezmoi add --autotemplate $HOME${/}.file
cmp $CHEZMOISOURCEDIR/dot_file golden/dot_file
exec chezmoi add --autotemplate $HOME${/}.notatemplate
cmp $CHEZMOISOURCEDIR/dot_notatemplate golden/dot_notatemplate

# test that chezmoi add --autotemplate escapes brackets
exec chezmoi add --autotemplate $HOME${/}.vimrc
cmp $CHEZMOISOURCEDIR/dot_vimrc.tmpl golden/dot_vimrc.tmpl

-- golden/dot_file --
# contents of .file
-- golden/dot_notatemplate --
# contents of .notatemplate
-- golden/dot_template.tmpl --
key = {{ .variable }}
-- golden/dot_vimrc.tmpl --
Expand All @@ -26,8 +26,8 @@ set foldmarker={{ "{{" }},{{ "}}" }}
-- home/user/.config/chezmoi/chezmoi.toml --
[data]
variable = "value"
-- home/user/.file --
# contents of .file
-- home/user/.notatemplate --
# contents of .notatemplate
-- home/user/.template --
key = value
-- home/user/.vimrc --
Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/testdata/scripts/issue3602.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# test that the .chezmoi.config.destDir template variable is compatible with the replace template function
exec chezmoi execute-template '{{ .chezmoi.config.destDir | replace "abc" "def "}}'
Loading