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

feat: title template function #3590

Merged
merged 2 commits into from Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -41,6 +41,7 @@ require (
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa
golang.org/x/oauth2 v0.0.0-20220722155238-128564f6959c
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
golang.org/x/text v0.3.7
gopkg.in/mail.v2 v2.3.1
gopkg.in/yaml.v3 v3.0.1
)
Expand Down Expand Up @@ -154,7 +155,6 @@ require (
go.opencensus.io v0.23.0 // indirect
golang.org/x/net v0.0.0-20220805013720-a33c5aa5df48 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 // indirect
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f // indirect
google.golang.org/api v0.91.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions internal/tmpl/tmpl.go
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/pkg/build"
"github.com/goreleaser/goreleaser/pkg/context"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)

// Template holds data that can be applied to a template string.
Expand Down Expand Up @@ -185,6 +187,7 @@ func (t *Template) Apply(s string) (string, error) {
"trim": strings.TrimSpace,
"trimprefix": strings.TrimPrefix,
"trimsuffix": strings.TrimSuffix,
"title": cases.Title(language.English).String,
"dir": filepath.Dir,
"abs": filepath.Abs,
"incmajor": incMajor,
Expand Down
5 changes: 5 additions & 0 deletions internal/tmpl/tmpl_test.go
Expand Up @@ -227,6 +227,11 @@ func TestFuncMap(t *testing.T) {
Name: "trimsuffix",
Expected: "https://github.com/foo/bar",
},
{
Template: `{{ title "file" }}`,
Name: "title",
Expected: "File",
},
{
Template: `{{ .ReleaseURL }}`,
Name: "trimsuffix",
Expand Down
19 changes: 10 additions & 9 deletions www/docs/customization/templates.md
Expand Up @@ -104,18 +104,19 @@ On all fields, you have these available functions:

Usage |Description
------------------------------|------------------------------------------------------------------------------------------------------------------------------
`replace "v1.2" "v" ""` |replaces all matches. See [ReplaceAll](https://golang.org/pkg/strings/#ReplaceAll)
`replace "v1.2" "v" ""` |replaces all matches. See [ReplaceAll](https://golang.org/pkg/strings/#ReplaceAll).
`split "1.2" "."` |split string at separator. See [Split](https://golang.org/pkg/strings/#Split). Since v1.11.
`time "01/02/2006"` |current UTC time in the specified format (this is not deterministic, a new time for every call)
`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)
`trimprefix "v1.2" "v"` |removes provided leading prefix string, if present. See [TrimPrefix](https://golang.org/pkg/strings/#TrimPrefix)
`trimsuffix "1.2v" "v"` |removes provided trailing suffix string, if present. See [TrimSuffix](https://pkg.go.dev/strings#TrimSuffix)
`dir .Path` |returns all but the last element of path, typically the path's directory. See [Dir](https://golang.org/pkg/path/filepath/#Dir)
`abs .ArtifactPath` |returns an absolute representation of path. See [Abs](https://golang.org/pkg/path/filepath/#Abs)
`time "01/02/2006"` |current UTC time in the specified format (this is not deterministic, a new time for every call).
`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).
`trimprefix "v1.2" "v"` |removes provided leading prefix string, if present. See [TrimPrefix](https://golang.org/pkg/strings/#TrimPrefix).
`trimsuffix "1.2v" "v"` |removes provided trailing suffix string, if present. See [TrimSuffix](https://pkg.go.dev/strings#TrimSuffix).
`dir .Path` |returns all but the last element of path, typically the path's directory. See [Dir](https://golang.org/pkg/path/filepath/#Dir).
`abs .ArtifactPath` |returns an absolute representation of path. See [Abs](https://golang.org/pkg/path/filepath/#Abs).
`filter "text" "regex"` |keeps only the lines matching the given regex, analogous to `grep -E`. Since v1.6.
`reverseFilter "text" "regex"`|keeps only the lines **not** matching the given regex, analogous to `grep -vE`. Since v1.6.
`title "foo"` |"titlenize" the string using english as language. See [Title](https://pkg.go.dev/golang.org/x/text/cases#Title). Since v1.14.

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