Skip to content

Commit

Permalink
feat: .Now template variable (#3853)
Browse files Browse the repository at this point in the history
This will allow more customized templates using dates!

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Mar 20, 2023
1 parent 6e4c0e6 commit 590eff3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/tmpl/tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const (
isSnapshot = "IsSnapshot"
env = "Env"
date = "Date"
now = "Now"
timestamp = "Timestamp"
modulePath = "ModulePath"
releaseNotes = "ReleaseNotes"
Expand Down Expand Up @@ -102,6 +103,7 @@ func New(ctx *context.Context) *Template {
releaseURL: ctx.ReleaseURL,
env: ctx.Env,
date: ctx.Date.UTC().Format(time.RFC3339),
now: ctx.Date.UTC(),
timestamp: ctx.Date.UTC().Unix(),
major: ctx.Semver.Major,
minor: ctx.Semver.Minor,
Expand Down
5 changes: 5 additions & 0 deletions internal/tmpl/tmpl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"runtime"
"testing"
"text/template"
"time"

"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/testctx"
Expand Down Expand Up @@ -38,6 +39,7 @@ func TestWithArtifact(t *testing.T) {
func(ctx *context.Context) {
ctx.ModulePath = "github.com/goreleaser/goreleaser"
ctx.ReleaseNotes = "test release notes"
ctx.Date = time.Unix(1678327562, 0)
},
)
for expect, tmpl := range map[string]string{
Expand Down Expand Up @@ -75,6 +77,9 @@ func TestWithArtifact(t *testing.T) {
"artifact path: /tmp/foo.exe": "artifact path: {{ .ArtifactPath }}",
"artifact basename: foo.exe": "artifact basename: {{ base .ArtifactPath }}",
"artifact dir: /tmp": "artifact dir: {{ dir .ArtifactPath }}",
"2023": `{{ .Now.Format "2006" }}`,
"2023-03-09T02:06:02Z": `{{ .Date }}`,
"1678327562": `{{ .Timestamp }}`,

"remove this": "{{ filter .Env.MULTILINE \".*remove.*\" }}",
"something with\nmultiple lines\nto test things": "{{ reverseFilter .Env.MULTILINE \".*remove.*\" }}",
Expand Down
1 change: 1 addition & 0 deletions www/docs/customization/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Key |Description
`.IsNightly` |`true` if `--nightly` is set, `false` otherwise
`.Env` |a map with system's environment variables
`.Date` |current UTC date in RFC 3339 format
`.Now` |current UTC date as `time.Time` struct, allows all `time.Time` functions (e.g. `{{ .Now.Format "2006" }}`) . Since v1.17.
`.Timestamp` |current UTC time in Unix format
`.ModulePath` |the go module path, as reported by `go list -m`
`incpatch "v1.2.4"` |increments the patch of the given version[^panic-if-not-semver]
Expand Down

0 comments on commit 590eff3

Please sign in to comment.