Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added trim functions #1558

Merged
merged 10 commits into from
Apr 12, 2022
27 changes: 27 additions & 0 deletions docs/templating-language.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ provides the following functions:
- [mergeMap](#mergemap)
- [mergeMapWithOverride](#mergemapwithoverride)
- [trimSpace](#trimspace)
- [trim](#trim)
- [trimPrefix](#trimprefix)
- [trimSuffix](#trimsuffix)
- [parseBool](#parsebool)
- [parseFloat](#parsefloat)
- [parseInt](#parseint)
Expand Down Expand Up @@ -1256,6 +1259,30 @@ Takes the provided input and trims all whitespace, tabs and newlines:
{{ file "/etc/ec2_version" | trimSpace }}
```

### `trim`

Takes the provided input and trims all leading and trailing unicode:

```golang
{{ "hello world!!" | trim "!!" }}
```

### `trimPrefix`

Takes the provided input and trims leading prefix string:

```golang
{{ "hello world!!" | trimPrefix "hello" }}
```

### `trimSuffix`

Takes the provided input and trims trailing suffix string:

```golang
{{ "hello world!!" | trimSuffix "world!!" }}
```

### `parseBool`

Takes the given string and parses it as a boolean:
Expand Down
15 changes: 15 additions & 0 deletions template/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,21 @@ func join(sep string, a []string) (string, error) {
return strings.Join(a, sep), nil
}

// trim is a version of strings.Trim that can be piped
func trim(cutset string, s string) (string, error) {
return strings.Trim(s, cutset), nil
}

// trimPrefix is a version of strings.TrimPrefix that can be piped
func trimPrefix(prefix string, s string) (string, error) {
return strings.TrimPrefix(s, prefix), nil
}

// trimSuffix is a version of strings.TrimSuffix that can be piped
func trimSuffix(suffix string, s string) (string, error) {
return strings.TrimSuffix(s, suffix), nil
}

// TrimSpace is a version of strings.TrimSpace that can be piped
func trimSpace(s string) (string, error) {
return strings.TrimSpace(s), nil
Expand Down
3 changes: 3 additions & 0 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ func funcMap(i *funcMapInput) template.FuncMap {
"indent": indent,
"loop": loop,
"join": join,
"trim": trim,
"trimPrefix": trimPrefix,
"trimSuffix": trimSuffix,
"trimSpace": trimSpace,
"parseBool": parseBool,
"parseFloat": parseFloat,
Expand Down
35 changes: 34 additions & 1 deletion template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,39 @@ func TestTemplate_Execute(t *testing.T) {
"a;b;c",
false,
},
{
"helper_trim",
&NewTemplateInput{
Contents: `{{ "!!hello world!!" | trim "!!" }}`,
},
&ExecuteInput{
Brain: NewBrain(),
},
"hello world",
false,
},
{
"helper_trimPrefix",
&NewTemplateInput{
Contents: `{{ "hello world!!" | trimPrefix "hello " }}`,
},
&ExecuteInput{
Brain: NewBrain(),
},
"world!!",
false,
},
{
"helper_trimSuffix",
&NewTemplateInput{
Contents: `{{ "hello world!!" | trimSuffix " world!!" }}`,
},
&ExecuteInput{
Brain: NewBrain(),
},
"hello",
false,
},
{
"helper_parseBool",
&NewTemplateInput{
Expand Down Expand Up @@ -1993,7 +2026,7 @@ func Test_writeToFile(t *testing.T) {

cases := []struct {
name string
filePath string
filePath string
content string
username string
groupName string
Expand Down