Skip to content

Commit

Permalink
feat: Add toYamlWithIndent template function
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Jul 21, 2024
1 parent 2a7845f commit cced052
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
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

0 comments on commit cced052

Please sign in to comment.