Skip to content

Commit

Permalink
fix: Make splitList return []any
Browse files Browse the repository at this point in the history
  • Loading branch information
bradenhilton committed Feb 27, 2024
1 parent 959e580 commit f83aa2a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ func newConfig(options ...configOption) (*Config, error) {
// Override sprig template functions. Delete them from the template function
// map first to avoid a duplicate function panic.
delete(c.templateFuncs, "fromJson")
delete(c.templateFuncs, "splitList")
delete(c.templateFuncs, "toPrettyJson")

// The completion template function is added in persistentPreRunRootE as
Expand Down Expand Up @@ -477,6 +478,7 @@ func newConfig(options ...configOption) (*Config, error) {
"secret": c.secretTemplateFunc,
"secretJSON": c.secretJSONTemplateFunc,
"setValueAtPath": c.setValueAtPathTemplateFunc,
"splitList": c.splitListTemplateFunc,
"stat": c.statTemplateFunc,
"toIni": c.toIniTemplateFunc,
"toPrettyJson": c.toPrettyJsonTemplateFunc,
Expand Down
9 changes: 9 additions & 0 deletions internal/cmd/templatefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,15 @@ func (c *Config) setValueAtPathTemplateFunc(path, value, dict any) any {
return result
}

func (c *Config) splitListTemplateFunc(sep, s string) []any {
strSlice := strings.Split(s, sep)
result := make([]interface{}, len(strSlice))
for i, v := range strSlice {
result[i] = v
}
return result
}

func (c *Config) statTemplateFunc(name string) any {
switch fileInfo, err := c.fileSystem.Stat(name); {
case err == nil:
Expand Down
4 changes: 4 additions & 0 deletions internal/cmd/testdata/scripts/templatefuncs.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ stdout '^value2$'
exec chezmoi execute-template '{{ dict "key" "value" | toYaml }}'
stdout '^key: value$'

# test that the overridden splitList function's output is compatible with quoteList
exec chezmoi execute-template '{{ "a b" | splitList " " | quoteList }}'
stdout '["a" "b"]'

-- bin/chezmoi-output-test --
#!/bin/sh

Expand Down

0 comments on commit f83aa2a

Please sign in to comment.