Skip to content

Commit

Permalink
feat: add .chezmoi.targetFile template variable
Browse files Browse the repository at this point in the history
The test for `.chezmoi.targetFile` is excluded from tests because of the
difference in path separators.

Resolves #3203
  • Loading branch information
halostatue committed Aug 29, 2023
1 parent da9f3c6 commit acb8937
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
1 change: 1 addition & 0 deletions assets/chezmoi.io/docs/reference/templates/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ chezmoi provides the following automatically-populated variables:
| `.chezmoi.osRelease` | string | The information from `/etc/os-release`, Linux only, run `chezmoi data` to see its output |
| `.chezmoi.sourceDir` | string | The source directory |
| `.chezmoi.sourceFile` | string | The path of the template relative to the source directory |
| `.chezmoi.targetFile` | string | The absolute path of the target file for the template |
| `.chezmoi.uid` | string | The user ID |
| `.chezmoi.username` | string | The username of the user running chezmoi |
| `.chezmoi.version.builtBy` | string | The program that built the `chezmoi` executable, if set |
Expand Down
27 changes: 17 additions & 10 deletions internal/chezmoi/sourcestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ func (s *SourceState) Encryption() Encryption {
// ExecuteTemplateDataOptions are options to SourceState.ExecuteTemplateData.
type ExecuteTemplateDataOptions struct {
Name string
Destination string
Data []byte
TemplateOptions TemplateOptions
}
Expand All @@ -769,6 +770,7 @@ func (s *SourceState) ExecuteTemplateData(options ExecuteTemplateDataOptions) ([
templateData := s.TemplateData()
if chezmoiTemplateData, ok := templateData["chezmoi"].(map[string]any); ok {
chezmoiTemplateData["sourceFile"] = options.Name
chezmoiTemplateData["targetFile"] = options.Destination
}

return tmpl.Execute(templateData)
Expand Down Expand Up @@ -1726,8 +1728,9 @@ func (s *SourceState) newCreateTargetStateEntryFunc(
}
if fileAttr.Template {
contents, err = s.ExecuteTemplateData(ExecuteTemplateDataOptions{
Name: sourceRelPath.String(),
Data: contents,
Name: sourceRelPath.String(),
Data: contents,
Destination: destAbsPath.String(),
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -1783,8 +1786,9 @@ func (s *SourceState) newFileTargetStateEntryFunc(
}
if fileAttr.Template {
contents, err = s.ExecuteTemplateData(ExecuteTemplateDataOptions{
Name: sourceRelPath.String(),
Data: contents,
Name: sourceRelPath.String(),
Data: contents,
Destination: destAbsPath.String(),
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -1829,8 +1833,9 @@ func (s *SourceState) newModifyTargetStateEntryFunc(
}
if fileAttr.Template {
modifierContents, err = s.ExecuteTemplateData(ExecuteTemplateDataOptions{
Name: sourceRelPath.String(),
Data: modifierContents,
Name: sourceRelPath.String(),
Data: modifierContents,
Destination: destAbsPath.String(),
})
if err != nil {
return
Expand Down Expand Up @@ -1934,8 +1939,9 @@ func (s *SourceState) newScriptTargetStateEntryFunc(
}
if fileAttr.Template {
contents, err = s.ExecuteTemplateData(ExecuteTemplateDataOptions{
Name: sourceRelPath.String(),
Data: contents,
Name: sourceRelPath.String(),
Data: contents,
Destination: destAbsPath.String(),
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -1968,8 +1974,9 @@ func (s *SourceState) newSymlinkTargetStateEntryFunc(
}
if fileAttr.Template {
linknameBytes, err = s.ExecuteTemplateData(ExecuteTemplateDataOptions{
Name: sourceRelPath.String(),
Data: linknameBytes,
Name: sourceRelPath.String(),
Data: linknameBytes,
Destination: destAbsPath.String(),
})
if err != nil {
return "", err
Expand Down
12 changes: 12 additions & 0 deletions internal/cmd/testdata/scripts/templatedata.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ stdout ok
exec chezmoi execute-template '{{ template "template" . }}'
stdout ok

[!windows] chhome home4/user

# test that .chezmoi.sourceFile is set
[!windows] exec chezmoi cat $HOME${/}.file
[!windows] cmpenv stdout golden/dot_file

-- golden/dot_file --
dot_file.tmpl
$WORK/home4/user/.file
-- home/user/.local/share/chezmoi/dot_file.tmpl --
{{ .chezmoi.sourceFile }}
-- home2/user/.local/share/chezmoi/.chezmoidata.toml --
Expand All @@ -51,3 +60,6 @@ message: ok
{{ "invalid template"
-- home3/user/.local/share/chezmoi/.chezmoitemplates/template --
{{ .message }}
-- home4/user/.local/share/chezmoi/dot_file.tmpl --
{{ .chezmoi.sourceFile }}
{{ .chezmoi.targetFile }}

0 comments on commit acb8937

Please sign in to comment.