1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-11-06 09:09:29 +02:00

feat: add basic string function to template (#1232)

This commit is contained in:
Gustavo Chaín
2019-11-07 18:49:36 +01:00
committed by Carlos Alexandro Becker
parent 333d834b49
commit 3d95590238
3 changed files with 41 additions and 8 deletions

View File

@@ -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 {

View File

@@ -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)
}
}
}