From cced0526ecb8d623bb742704dcb8104033150554 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Sun, 21 Jul 2024 21:35:01 +0200 Subject: [PATCH] feat: Add toYamlWithIndent template function --- .../templates/functions/toYamlWithIndent.md | 10 ++++++++++ assets/chezmoi.io/mkdocs.yml | 1 + internal/cmd/config.go | 1 + internal/cmd/templatefuncs.go | 11 +++++++++++ internal/cmd/testdata/scripts/templatefuncs.txtar | 12 ++++++++++++ 5 files changed, 35 insertions(+) create mode 100644 assets/chezmoi.io/docs/reference/templates/functions/toYamlWithIndent.md diff --git a/assets/chezmoi.io/docs/reference/templates/functions/toYamlWithIndent.md b/assets/chezmoi.io/docs/reference/templates/functions/toYamlWithIndent.md new file mode 100644 index 00000000000..45556e5f807 --- /dev/null +++ b/assets/chezmoi.io/docs/reference/templates/functions/toYamlWithIndent.md @@ -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 }} + ``` diff --git a/assets/chezmoi.io/mkdocs.yml b/assets/chezmoi.io/mkdocs.yml index 31a9df256d9..1755c73e8b7 100644 --- a/assets/chezmoi.io/mkdocs.yml +++ b/assets/chezmoi.io/mkdocs.yml @@ -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 diff --git a/internal/cmd/config.go b/internal/cmd/config.go index b1a512a9493..9dcd4072102 100644 --- a/internal/cmd/config.go +++ b/internal/cmd/config.go @@ -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) diff --git a/internal/cmd/templatefuncs.go b/internal/cmd/templatefuncs.go index 998cbf0d40b..903778cbcfe 100644 --- a/internal/cmd/templatefuncs.go +++ b/internal/cmd/templatefuncs.go @@ -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" @@ -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(), diff --git a/internal/cmd/testdata/scripts/templatefuncs.txtar b/internal/cmd/testdata/scripts/templatefuncs.txtar index bbfd45d00b5..81a315e4a0a 100644 --- a/internal/cmd/testdata/scripts/templatefuncs.txtar +++ b/internal/cmd/testdata/scripts/templatefuncs.txtar @@ -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"]' @@ -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 --