Skip to content

Commit

Permalink
feat: add basic string function to template (#1232)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Chaín authored and caarlos0 committed Nov 7, 2019
1 parent 333d834 commit 3d95590
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
4 changes: 4 additions & 0 deletions internal/tmpl/tmpl.go
Expand Up @@ -108,9 +108,13 @@ func (t *Template) Apply(s string) (string, error) {
tmpl, err := template.New("tmpl").
Option("missingkey=error").
Funcs(template.FuncMap{
"replace": strings.ReplaceAll,
"time": func(s string) string {
return time.Now().UTC().Format(s)
},
"tolower": strings.ToLower,
"toupper": strings.ToUpper,
"trim": strings.TrimSpace,
}).
Parse(s)
if err != nil {
Expand Down
33 changes: 29 additions & 4 deletions internal/tmpl/tmpl_test.go
Expand Up @@ -154,23 +154,48 @@ func TestFuncMap(t *testing.T) {
for _, tc := range []struct {
Template string
Name string
Expected string
}{
{
Template: `{{ replace "v1.24" "v" "" }}`,
Name: "replace",
Expected: "1.24",
},
{
Template: `{{ time "2006-01-02" }}`,
Name: "YYYY-MM-DD",
Name: "time YYYY-MM-DD",
},
{
Template: `{{ time "01/02/2006" }}`,
Name: "MM/DD/YYYY",
Name: "time MM/DD/YYYY",
},
{
Template: `{{ time "01/02/2006" }}`,
Name: "MM/DD/YYYY",
Name: "time MM/DD/YYYY",
},
{
Template: `{{ tolower "TEST" }}`,
Name: "tolower",
Expected: "test",
},
{
Template: `{{ toupper "test" }}`,
Name: "toupper",
Expected: "TEST",
},
{
Template: `{{ trim " test " }}`,
Name: "trim",
Expected: "test",
},
} {
out, err := New(ctx).Apply(tc.Template)
assert.NoError(t, err)
assert.NotEmpty(t, out)
if tc.Expected != "" {
assert.Equal(t, tc.Expected, out)
} else {
assert.NotEmpty(t, out)
}
}
}

Expand Down
12 changes: 8 additions & 4 deletions www/content/templates.md
Expand Up @@ -42,15 +42,19 @@ may have some extra fields:

On all fields, you have these available functions:

| Usage | Description |
| :-----------------: | :--------------------------------------: |
| `time "01/02/2006"` | current UTC time in the specified format |
| Usage | Description |
| :--------------------: | :----------------------------------------------------------------------------------: |
| `replace "v1.2" "v" ""` | replaces all macthes. See [ReplaceAll](https://golang.org/pkg/strings/#ReplaceAll) |
| `time "01/02/2006"` | current UTC time in the specified format |
| `tolower "V1.2"` | makes input string lowercase. See [ToLower](https://golang.org/pkg/strings/#ToLower) |
| `toupper "v1.2"` | makes input string uppercase. See [ToUpper](https://golang.org/pkg/strings/#ToUpper) |
| `trim " v1.2 "` | removes all leading and trailing white space. See [TrimSpace](https://golang.org/pkg/strings/#TrimSpace) |

With all those fields, you may be able to compose the name of your artifacts
pretty much the way you want:

```yaml
example_template: '{{ .ProjectName }}_{{ .Env.USER }}_{{ time "2006" }}'
example_template: '{{ tolower .ProjectName }}_{{ .Env.USER }}_{{ time "2006" }}'
```

For example, if you want to add the go version to some artifact:
Expand Down

0 comments on commit 3d95590

Please sign in to comment.