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 toYamlWithIndent template function #3875

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# `toYamlWithIndent` *spaces* *value*

`toYamlWithIndent` returns the YAML representation of *value* with an indent of
*spaces* spaces.

!!! example

```
{{ dict "key" (dict "subKey" "value") | toYamlWithIndent 2 }}
```
1 change: 1 addition & 0 deletions assets/chezmoi.io/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ nav:
- toPrettyJson: reference/templates/functions/toPrettyJson.md
- toToml: reference/templates/functions/toToml.md
- toYaml: reference/templates/functions/toYaml.md
- toYamlWithIndent: reference/templates/functions/toYamlWithIndent.md
- GitHub functions:
- reference/templates/github-functions/index.md
- gitHubKeys: reference/templates/github-functions/gitHubKeys.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 @@ -491,6 +491,7 @@ func newConfig(options ...configOption) (*Config, error) {
"toPrettyJson": c.toPrettyJsonTemplateFunc,
"toToml": c.toTomlTemplateFunc,
"toYaml": c.toYamlTemplateFunc,
"toYamlWithIndent": c.toYamlWithIndentTemplateFunc,
"vault": c.vaultTemplateFunc,
} {
c.addTemplateFunc(key, value)
Expand Down
11 changes: 11 additions & 0 deletions internal/cmd/templatefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/bradenhilton/mozillainstallhash"
"github.com/itchyny/gojq"
"gopkg.in/ini.v1"
"gopkg.in/yaml.v3"
"howett.net/plist"

"github.com/twpayne/chezmoi/v2/internal/chezmoi"
Expand Down Expand Up @@ -553,6 +554,16 @@ func (c *Config) toYamlTemplateFunc(data any) string {
return string(yaml)
}

func (c *Config) toYamlWithIndentTemplateFunc(spaces int, data any) string {
var builder strings.Builder
encoder := yaml.NewEncoder(&builder)
encoder.SetIndent(spaces)
if err := encoder.Encode(data); err != nil {
panic(err)
}
return builder.String()
}

func fileInfoToMap(fileInfo fs.FileInfo) map[string]any {
return map[string]any{
"name": fileInfo.Name(),
Expand Down
12 changes: 12 additions & 0 deletions internal/cmd/testdata/scripts/templatefuncs.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ stdout '^value2$'
exec chezmoi execute-template '{{ dict "key" "value" | toYaml }}'
stdout '^key: value$'

# test toYamlWithIndent template function
exec chezmoi execute-template '{{ dict "key" (dict "nestedKey" "value") | toYamlWithIndent 2 }}'
cmp stdout golden/toYamlWithIndent2.yaml
exec chezmoi execute-template '{{ dict "key" (dict "nestedKey" "value") | toYamlWithIndent 4 }}'
cmp stdout golden/toYamlWithIndent4.yaml

# test that the overridden splitList function's output is compatible with quoteList
exec chezmoi execute-template '{{ "a b" | splitList " " | quoteList }}'
stdout '["a" "b"]'
Expand Down Expand Up @@ -285,6 +291,12 @@ subkey = subvalue
"b": "&"
}
}
-- golden/toYamlWithIndent2.yaml --
key:
nestedKey: value
-- golden/toYamlWithIndent4.yaml --
key:
nestedKey: value
-- home/user/.include --
# contents of .include
-- home/user/.local/share/chezmoi/.chezmoitemplates/template --
Expand Down
Loading