From ebeb8df42b76eea6da28db5fbd0f50302794a279 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 27 Jul 2022 16:44:01 +0200 Subject: [PATCH] feat: Add replaceAllRegex template function --- .../reference/templates/functions/replaceAllRegex.md | 12 ++++++++++++ assets/chezmoi.io/mkdocs.yml | 1 + pkg/cmd/config.go | 1 + pkg/cmd/templatefuncs.go | 4 ++++ pkg/cmd/testdata/scripts/templatefuncs.txt | 4 ++++ 5 files changed, 22 insertions(+) create mode 100644 assets/chezmoi.io/docs/reference/templates/functions/replaceAllRegex.md diff --git a/assets/chezmoi.io/docs/reference/templates/functions/replaceAllRegex.md b/assets/chezmoi.io/docs/reference/templates/functions/replaceAllRegex.md new file mode 100644 index 00000000000..f7f4be8d30e --- /dev/null +++ b/assets/chezmoi.io/docs/reference/templates/functions/replaceAllRegex.md @@ -0,0 +1,12 @@ +# `replaceAllRegex` *expr* *repl* *text* + +`replaceAllRegex` returns *text* with all substrings matching the regular +expression *expr* replaced with *repl*. It is an alternative to [sprig's +`regexpReplaceAll` function](http://masterminds.github.io/sprig/strings.html) +with a different argument order that supports pipelining. + +!!! example + + ``` + {{ "foo subject string" | replaceAllRegex "foo" "bar" }} + ``` diff --git a/assets/chezmoi.io/mkdocs.yml b/assets/chezmoi.io/mkdocs.yml index be0ac743b34..e48f55adc71 100644 --- a/assets/chezmoi.io/mkdocs.yml +++ b/assets/chezmoi.io/mkdocs.yml @@ -180,6 +180,7 @@ nav: - mozillaInstallHash: reference/templates/functions/mozillaInstallHash.md - output: reference/templates/functions/output.md - quoteList: reference/templates/functions/quoteList.md + - replaceAllRegex: reference/templates/functions/replaceAllRegex.md - stat: reference/templates/functions/stat.md - toToml: reference/templates/functions/toToml.md - toYaml: reference/templates/functions/toYaml.md diff --git a/pkg/cmd/config.go b/pkg/cmd/config.go index f65cb335f8d..aab76490aa9 100644 --- a/pkg/cmd/config.go +++ b/pkg/cmd/config.go @@ -468,6 +468,7 @@ func newConfig(options ...configOption) (*Config, error) { "passFields": c.passFieldsTemplateFunc, "passRaw": c.passRawTemplateFunc, "quoteList": c.quoteListTemplateFunc, + "replaceAllRegex": c.replaceAllRegexTemplateFunc, "secret": c.secretTemplateFunc, "secretJSON": c.secretJSONTemplateFunc, "stat": c.statTemplateFunc, diff --git a/pkg/cmd/templatefuncs.go b/pkg/cmd/templatefuncs.go index 54009d06257..4f0463e9abb 100644 --- a/pkg/cmd/templatefuncs.go +++ b/pkg/cmd/templatefuncs.go @@ -174,6 +174,10 @@ func (c *Config) quoteListTemplateFunc(list []interface{}) []string { return result } +func (c *Config) replaceAllRegexTemplateFunc(expr, repl, s string) string { + return regexp.MustCompile(expr).ReplaceAllString(s, repl) +} + func (c *Config) statTemplateFunc(name string) interface{} { switch fileInfo, err := c.fileSystem.Stat(name); { case err == nil: diff --git a/pkg/cmd/testdata/scripts/templatefuncs.txt b/pkg/cmd/testdata/scripts/templatefuncs.txt index 699ccf604dc..76f9f4a35c6 100644 --- a/pkg/cmd/testdata/scripts/templatefuncs.txt +++ b/pkg/cmd/testdata/scripts/templatefuncs.txt @@ -49,6 +49,10 @@ stdout 2656FF1E876E9973 [!(windows||illumos)] stderr 'error calling output: .*/false: exit status 1' [illumos] stderr 'error calling output: .*/false: exit status 255' +# test replaceAllRegex template function +chezmoi execute-template '{{ "foo bar baz" | replaceAllRegex "ba" "BA" }}' +stdout 'foo BAr BAz' + # test stat template function chezmoi execute-template '{{ (stat ".").isDir }}' stdout true