Skip to content

Commit

Permalink
feat: Add outputList template function
Browse files Browse the repository at this point in the history
Resolves #4253
  • Loading branch information
halostatue committed Feb 4, 2025
1 parent 9075fec commit ce88aac
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `output` *name* [*arg*...]

`output` returns the output of executing the command *name* with *arg*s. If
`output` returns the output of executing the command _name_ with *arg*s. If
executing the command returns an error then template execution exits with an
error. The execution occurs every time that the template is executed. It is the
user's responsibility to ensure that executing the command is both idempotent
Expand Down
19 changes: 19 additions & 0 deletions assets/chezmoi.io/docs/reference/templates/functions/outputList.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# `outputList` *name* [*argList*]

`outputList` returns the output of executing the command _name_ with the *argList*.
*arg*s. If executing the command returns an error then template execution exits
with an error. The execution occurs every time that the template is executed. It
is the user's responsibility to ensure that executing the command is both
idempotent and fast.

This differs from [`output`](output.md) in that it allows for the `outputList`
to be created programmatically. The list *must* be converted to a string list
with `toStrings`.

!!! example

```
{{- $args := (list "config" "current-context") | toStrings }}
current-context: {{ output "kubectl" $args | trim }}
```

1 change: 1 addition & 0 deletions assets/chezmoi.io/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ nav:
- lstat: reference/templates/functions/lstat.md
- mozillaInstallHash: reference/templates/functions/mozillaInstallHash.md
- output: reference/templates/functions/output.md
- outputList: reference/templates/functions/outputList.md
- pruneEmptyDicts: reference/templates/functions/pruneEmptyDicts.md
- quoteList: reference/templates/functions/quoteList.md
- replaceAllRegex: reference/templates/functions/replaceAllRegex.md
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ func newConfig(options ...configOption) (*Config, error) {
"onepasswordSDKItemsGet": c.onepasswordSDKItemsGet,
"onepasswordSDKSecretsResolve": c.onepasswordSDKSecretsResolve,
"output": c.outputTemplateFunc,
"outputList": c.outputListTemplateFunc,
"pass": c.passTemplateFunc,
"passFields": c.passFieldsTemplateFunc,
"passRaw": c.passRawTemplateFunc,
Expand Down
4 changes: 4 additions & 0 deletions internal/cmd/templatefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@ func (c *Config) outputTemplateFunc(name string, args ...string) string {
return string(output)
}

func (c *Config) outputListTemplateFunc(name string, args []string) string {
return c.outputTemplateFunc(name, args...)
}

func (c *Config) pruneEmptyDictsTemplateFunc(dict map[string]any) map[string]any {
pruneEmptyMaps(dict)
return dict
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 @@ -138,6 +138,10 @@ stdout 2656FF1E876E9973
[unix] exec chezmoi execute-template '{{ $red := output "generate-color-formats" "#ff0000" | fromJson }}{{ $red.rgb.r }}'
[unix] stdout '^255$'

# test the outputList and fromJson template functions
[unix] exec chezmoi execute-template '{{ $red := outputList "generate-color-formats" (list "#ff0000" | toStrings) | fromJson }}{{ $red.rgb.r }}'
[unix] stdout '^255$'

# test that the output function returns an error if the command fails
[unix] ! exec chezmoi execute-template '{{ output "false" }}'
[unix] stderr 'error calling output: .*/false: exit status 1'
Expand Down

0 comments on commit ce88aac

Please sign in to comment.