Skip to content

Commit

Permalink
feat: Add replaceAllRegex template function
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Jul 28, 2022
1 parent 199d62e commit ebeb8df
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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" }}
```
1 change: 1 addition & 0 deletions assets/chezmoi.io/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/templatefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/testdata/scripts/templatefuncs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ebeb8df

Please sign in to comment.