Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add outputList template function #4255

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 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,18 @@
# `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 *args* to be
created programmatically.

!!! example

```
{{- $args := (list "config" "current-context") }}
current-context: {{ outputList "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
9 changes: 9 additions & 0 deletions internal/cmd/templatefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,15 @@ func (c *Config) outputTemplateFunc(name string, args ...string) string {
return string(output)
}

func (c *Config) outputListTemplateFunc(name string, args []any) string {
slice, err := anyToStringSlice(args)
if err != nil {
panic(fmt.Errorf("args list: %w", err))
}

return c.outputTemplateFunc(name, slice...)
}

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" ) | 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
Loading