Skip to content

Commit

Permalink
fix: Allow create_ entries to be templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Peterdes authored and twpayne committed Oct 5, 2021
1 parent 8d9a89b commit 02abd9a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 21 deletions.
25 changes: 18 additions & 7 deletions internal/chezmoi/sourcestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -1184,21 +1184,32 @@ func (s *SourceState) newSourceStateDir(sourceRelPath SourceRelPath, dirAttr Dir
// newCreateTargetStateEntryFunc returns a targetStateEntryFunc that returns a
// file with sourceLazyContents if the file does not already exist, or returns
// the actual file's contents unchanged if the file already exists.
func (s *SourceState) newCreateTargetStateEntryFunc(fileAttr FileAttr, sourceLazyContents *lazyContents) targetStateEntryFunc {
func (s *SourceState) newCreateTargetStateEntryFunc(sourceRelPath SourceRelPath, fileAttr FileAttr, sourceLazyContents *lazyContents) targetStateEntryFunc {
return func(destSystem System, destAbsPath AbsPath) (TargetStateEntry, error) {
var lazyContents *lazyContents
contents, err := destSystem.ReadFile(destAbsPath)
switch {
case err == nil:
lazyContents = newLazyContents(contents)
case errors.Is(err, fs.ErrNotExist):
contents, err = sourceLazyContents.Contents()
if err != nil {
return nil, err
}
lazyContents = newLazyContentsFunc(func() ([]byte, error) {
contents, err = sourceLazyContents.Contents()
if err != nil {
return nil, err
}
if fileAttr.Template {
contents, err = s.ExecuteTemplateData(sourceRelPath.String(), contents)
if err != nil {
return nil, err
}
}
return contents, nil
})
default:
return nil, err
}
return &TargetStateFile{
lazyContents: newLazyContents(contents),
lazyContents: lazyContents,
empty: true,
perm: fileAttr.perm() &^ s.umask,
}, nil
Expand Down Expand Up @@ -1386,7 +1397,7 @@ func (s *SourceState) newSourceStateFile(sourceRelPath SourceRelPath, fileAttr F
var targetStateEntryFunc targetStateEntryFunc
switch fileAttr.Type {
case SourceFileTypeCreate:
targetStateEntryFunc = s.newCreateTargetStateEntryFunc(fileAttr, sourceLazyContents)
targetStateEntryFunc = s.newCreateTargetStateEntryFunc(sourceRelPath, fileAttr, sourceLazyContents)
case SourceFileTypeFile:
targetStateEntryFunc = s.newFileTargetStateEntryFunc(sourceRelPath, fileAttr, sourceLazyContents)
case SourceFileTypeModify:
Expand Down
43 changes: 43 additions & 0 deletions internal/cmd/testdata/scripts/create.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# test that chezmoi apply creates a file from a template
mkdir $CHEZMOISOURCEDIR
cp golden/create_dot_create.tmpl $CHEZMOISOURCEDIR
chezmoi apply --force
cmp $HOME/.create golden/.create

[windows] skip 'UNIX only'
[!exec:age] skip 'age not found in $PATH'

chhome home2/user

# test that chezmoi apply creates encrypted create_ targets
mkageconfig
mkdir $CHEZMOISOURCEDIR
chezmoi encrypt --output=$CHEZMOISOURCEDIR/create_encrypted_dot_create.age golden/.create
grep '-----BEGIN AGE ENCRYPTED FILE-----' $CHEZMOISOURCEDIR/create_encrypted_dot_create.age
chezmoi apply --force
cmp $HOME/.create golden/.create

chhome home3/user

# test that chezmoi apply creates encrypted template create_ targets
mkageconfig
mkdir $CHEZMOISOURCEDIR
chezmoi encrypt --output=$CHEZMOISOURCEDIR/create_encrypted_dot_create.tmpl.age golden/create_dot_create.tmpl
grep '-----BEGIN AGE ENCRYPTED FILE-----' $CHEZMOISOURCEDIR/create_encrypted_dot_create.tmpl.age
chezmoi apply --force
cmp $HOME/.create golden/.create

chhome home4/user

# test that chezmoi manage does not execute template create_ targets
chezmoi managed
cmp stdout golden/managed

-- golden/.create --
# contents of .create
-- golden/create_dot_create.tmpl --
{{ "# contents of .create" }}
-- golden/managed --
.create
-- home4/user/.local/share/chezmoi/create_dot_create.tmpl --
{{ fail "Template should not be executed }}
14 changes: 0 additions & 14 deletions internal/cmd/testdata/scripts/createencrypted.txt

This file was deleted.

0 comments on commit 02abd9a

Please sign in to comment.