1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-17 20:47:50 +02:00

feat: Provide time template func

This commit adds a customizable time function to the release name
template.
This commit is contained in:
Dominik Schulz 2018-03-28 15:55:05 +02:00 committed by Carlos Alexandro Becker
parent aa8f870523
commit c0379ed858
3 changed files with 59 additions and 0 deletions

View File

@ -31,6 +31,8 @@ release:
# - ProjectName
# - Tag
# - Version (Git tag without `v` prefix)
# There is also a template function "time" that takes a Go time format
# string to insert a formated timestamp into the release name.
# Default is ``
name_template: "{{.ProjectName}}-v{{.Version}}"
```

View File

@ -3,14 +3,20 @@ package client
import (
"bytes"
"text/template"
"time"
"github.com/goreleaser/goreleaser/context"
)
var (
timeNow = time.Now
)
func releaseTitle(ctx *context.Context) (string, error) {
var out bytes.Buffer
t, err := template.New("github").
Option("missingkey=error").
Funcs(mkFuncMap()).
Parse(ctx.Config.Release.NameTemplate)
if err != nil {
return "", err
@ -24,3 +30,14 @@ func releaseTitle(ctx *context.Context) (string, error) {
})
return out.String(), err
}
func mkFuncMap() template.FuncMap {
return template.FuncMap{
"time": func(s ...string) (string, error) {
if len(s) < 1 {
return "", nil
}
return timeNow().Format(s[0]), nil
},
}
}

View File

@ -0,0 +1,40 @@
package client
import (
"testing"
"time"
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
"github.com/stretchr/testify/assert"
)
func TestFuncMap(t *testing.T) {
timeNow = func() time.Time {
return time.Date(2018, 12, 11, 10, 9, 8, 7, time.UTC)
}
var ctx = context.New(config.Project{
ProjectName: "proj",
})
for _, tc := range []struct {
Template string
Name string
Output string
}{
{
Template: `{{ time "2006-01-02" }}`,
Name: "YYYY-MM-DD",
Output: "2018-12-11",
},
{
Template: `{{ time "01/02/2006" }}`,
Name: "MM/DD/YYYY",
Output: "12/11/2018",
},
} {
ctx.Config.Release.NameTemplate = tc.Template
out, err := releaseTitle(ctx)
assert.NoError(t, err)
assert.Equal(t, tc.Output, out)
}
}