Skip to content

Commit

Permalink
feat: Add includeTemplate template function
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Aug 31, 2022
1 parent 097a88c commit 3e6e26d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# `includeTemplate` *filename* [*data*]

`includeTemplate` returns the result of executing the contents of *filename*
with the optional *data*. Relative paths are interpreted relative to the source
directory.
1 change: 1 addition & 0 deletions assets/chezmoi.io/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ nav:
- fromYaml: reference/templates/functions/fromYaml.md
- glob: reference/templates/functions/glob.md
- include: reference/templates/functions/include.md
- includeTemplate: reference/templates/functions/includeTemplate.md
- ioreg: reference/templates/functions/ioreg.md
- joinPath: reference/templates/functions/joinPath.md
- lookPath: reference/templates/functions/lookPath.md
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ func newConfig(options ...configOption) (*Config, error) {
"gopass": c.gopassTemplateFunc,
"gopassRaw": c.gopassRawTemplateFunc,
"include": c.includeTemplateFunc,
"includeTemplate": c.includeTemplateTemplateFunc,
"ioreg": c.ioregTemplateFunc,
"joinPath": c.joinPathTemplateFunc,
"keepassxc": c.keepassxcTemplateFunc,
Expand Down
55 changes: 44 additions & 11 deletions pkg/cmd/templatefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"runtime"
"strconv"
"strings"
"text/template"

"github.com/bmatcuk/doublestar/v4"
"github.com/bradenhilton/mozillainstallhash"
Expand Down Expand Up @@ -87,23 +88,41 @@ func (c *Config) globTemplateFunc(pattern string) []string {
}

func (c *Config) includeTemplateFunc(filename string) string {
var absPath chezmoi.AbsPath
if filepath.IsAbs(filename) {
var err error
absPath, err = chezmoi.NewAbsPathFromExtPath(filename, c.homeDirAbsPath)
if err != nil {
panic(err)
}
} else {
absPath = c.SourceDirAbsPath.JoinString(filename)
}
contents, err := c.fileSystem.ReadFile(absPath.String())
contents, err := c.readFile(filename)
if err != nil {
panic(err)
}
return string(contents)
}

func (c *Config) includeTemplateTemplateFunc(filename string, args ...any) string {
var data any
switch len(args) {
case 0:
// Do nothing.
case 1:
data = args[0]
default:
panic(fmt.Errorf("expected 0 or 1 arguments, got %d", len(args)))
}

contents, err := c.readFile(filename)
if err != nil {
panic(err)
}

tmpl, err := template.New(filename).Funcs(c.templateFuncs).Parse(string(contents))
if err != nil {
panic(err)
}

var builder strings.Builder
if err := tmpl.Execute(&builder, data); err != nil {
panic(err)
}
return builder.String()
}

func (c *Config) ioregTemplateFunc() map[string]any {
if runtime.GOOS != "darwin" {
return nil
Expand Down Expand Up @@ -188,6 +207,20 @@ func (c *Config) quoteListTemplateFunc(list []any) []string {
return result
}

func (c *Config) readFile(filename string) ([]byte, error) {
var absPath chezmoi.AbsPath
if filepath.IsAbs(filename) {
var err error
absPath, err = chezmoi.NewAbsPathFromExtPath(filename, c.homeDirAbsPath)
if err != nil {
return nil, err
}
} else {
absPath = c.SourceDirAbsPath.JoinString(filename)
}
return c.fileSystem.ReadFile(absPath.String())
}

func (c *Config) replaceAllRegexTemplateFunc(expr, repl, s string) string {
return regexp.MustCompile(expr).ReplaceAllString(s, repl)
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/cmd/testdata/scripts/templatefuncs.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ cmp stdout golden/include-abspath
exec chezmoi execute-template '{{ include ".include" }}'
cmp stdout golden/include-relpath

# test includeTemplate template function
exec chezmoi execute-template '{{ includeTemplate ".template" "data" }}'
stdout ^data$

# test joinPath template function
exec chezmoi execute-template '{{ joinPath "a" "b" }}'
stdout a${/}b
Expand Down Expand Up @@ -158,5 +162,7 @@ subkey = "subvalue"
# contents of .include
-- home/user/.local/share/chezmoi/.include --
# contents of .local/share/chezmoi/.include
-- home/user/.local/share/chezmoi/.template --
{{ . }}
-- home/user/file1.txt --
-- home/user/file2.txt --

0 comments on commit 3e6e26d

Please sign in to comment.