Skip to content

Commit

Permalink
feat: Extend toPrettyJson template function to take indent
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Feb 8, 2023
1 parent 7ff0aca commit 3ef62d6
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# `toPrettyJson` [*indent*] *value*

`toPrettyJson` returns the JSON representation of *value*. The optional *indent*
specifies how much nested elements are indented relative to their parent.
*indent* defaults to two spaces.

!!! examples

```
{{ dict "a" (dict "b" "c") | toPrettyJson "\t" }}
```
1 change: 1 addition & 0 deletions assets/chezmoi.io/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ nav:
- setValueAtPath: reference/templates/functions/setValueAtPath.md
- stat: reference/templates/functions/stat.md
- toIni: reference/templates/functions/toIni.md
- toPrettyJson: reference/templates/functions/toPrettyJson.md
- toToml: reference/templates/functions/toToml.md
- toYaml: reference/templates/functions/toYaml.md
- GitHub functions:
Expand Down
21 changes: 13 additions & 8 deletions pkg/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,15 +346,19 @@ func newConfig(options ...configOption) (*Config, error) {
stderr: os.Stderr,
}

// Override sprig's toPrettyJson template function. Delete it from the
// template function map first to avoid a duplication function panic.
delete(c.templateFuncs, "toPrettyJson")

// The completion template function is added in persistentPreRunRootE as
// it needs a *cobra.Command, which we don't yet have.
for key, value := range map[string]any{
"awsSecretsManager": c.awsSecretsManagerTemplateFunc,
"awsSecretsManagerRaw": c.awsSecretsManagerRawTemplateFunc,
"bitwarden": c.bitwardenTemplateFunc,
"bitwardenAttachment": c.bitwardenAttachmentTemplateFunc,
"bitwardenFields": c.bitwardenFieldsTemplateFunc,
"comment": c.commentTemplateFunc,
// The completion template function is added in persistentPreRunRootE as
// it needs a *cobra.Command, which we don't yet have.
"awsSecretsManager": c.awsSecretsManagerTemplateFunc,
"awsSecretsManagerRaw": c.awsSecretsManagerRawTemplateFunc,
"bitwarden": c.bitwardenTemplateFunc,
"bitwardenAttachment": c.bitwardenAttachmentTemplateFunc,
"bitwardenFields": c.bitwardenFieldsTemplateFunc,
"comment": c.commentTemplateFunc,
"decrypt": c.decryptTemplateFunc,
"deleteValueAtPath": c.deleteValueAtPathTemplateFunc,
"encrypt": c.encryptTemplateFunc,
Expand Down Expand Up @@ -404,6 +408,7 @@ func newConfig(options ...configOption) (*Config, error) {
"setValueAtPath": c.setValueAtPathTemplateFunc,
"stat": c.statTemplateFunc,
"toIni": c.toIniTemplateFunc,
"toPrettyJson": c.toPrettyJsonTemplateFunc,
"toToml": c.toTomlTemplateFunc,
"toYaml": c.toYamlTemplateFunc,
"vault": c.vaultTemplateFunc,
Expand Down
29 changes: 29 additions & 0 deletions pkg/cmd/templatefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -413,6 +414,34 @@ func (c *Config) toIniTemplateFunc(data map[string]any) string {
return builder.String()
}

//nolint:revive,stylecheck
func (c *Config) toPrettyJsonTemplateFunc(args ...any) string {
var (
indent = " "
value any
)
switch len(args) {
case 1:
value = args[0]
case 2:
var ok bool
indent, ok = args[0].(string)
if !ok {
panic(fmt.Errorf("arg 1: expected a string, got a %T", args[0]))
}
value = args[1]
default:
panic(fmt.Errorf("expected 1 or 2 arguments, got %d", len(args)))
}
var builder strings.Builder
encoder := json.NewEncoder(&builder)
encoder.SetIndent("", indent)
if err := encoder.Encode(value); err != nil {
panic(err)
}
return builder.String()
}

func (c *Config) toTomlTemplateFunc(data any) string {
toml, err := chezmoi.FormatTOML.Marshal(data)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions pkg/cmd/testdata/scripts/templatefuncs.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ stdout '^value$'
exec chezmoi execute-template '{{ dict "key" "value" | toToml }}'
stdout '^key = .value.$'

# test toPrettyJson template function
exec chezmoi execute-template '{{ dict "a" (dict "b" "c") | toPrettyJson " " }}'
cmp stdout golden/toPrettyJson

# test fromYaml template function
exec chezmoi execute-template '{{ (fromYaml "key1: value1\nkey2: value2").key2 }}'
stdout '^value2$'
Expand Down Expand Up @@ -205,6 +209,12 @@ key = value

[section]
subkey = subvalue
-- golden/toPrettyJson --
{
"a": {
"b": "c"
}
}
-- home/user/.include --
# contents of .include
-- home/user/.local/share/chezmoi/.chezmoitemplates/template --
Expand Down

0 comments on commit 3ef62d6

Please sign in to comment.