Skip to content

Commit

Permalink
fix: Make splitList output []interface{}
Browse files Browse the repository at this point in the history
  • Loading branch information
bradenhilton committed Feb 27, 2024
1 parent 959e580 commit 6dd1f99
Show file tree
Hide file tree
Showing 3 changed files with 13 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 string, s string) []interface{} {

Check failure on line 485 in internal/cmd/templatefuncs.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
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
2 changes: 2 additions & 0 deletions internal/cmd/testdata/scripts/issue3603.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exec chezmoi execute-template '{{ "a b" | splitList " " | quoteList }}'
stdout '["a" "b"]'

0 comments on commit 6dd1f99

Please sign in to comment.