From 98d0c8a4a54844acc4066c9c93ae45ab1d6b9c82 Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Thu, 24 Nov 2022 14:46:42 -0300 Subject: [PATCH 1/2] feat: title template function Signed-off-by: Carlos A Becker --- internal/tmpl/tmpl.go | 3 +++ internal/tmpl/tmpl_test.go | 5 +++++ www/docs/customization/templates.md | 19 ++++++++++--------- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/internal/tmpl/tmpl.go b/internal/tmpl/tmpl.go index 84c771d9ce9..e6371a95971 100644 --- a/internal/tmpl/tmpl.go +++ b/internal/tmpl/tmpl.go @@ -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. @@ -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, diff --git a/internal/tmpl/tmpl_test.go b/internal/tmpl/tmpl_test.go index 13ba4d796b0..14f8bb169a6 100644 --- a/internal/tmpl/tmpl_test.go +++ b/internal/tmpl/tmpl_test.go @@ -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", diff --git a/www/docs/customization/templates.md b/www/docs/customization/templates.md index ced3cd8de8d..a653c3cf282 100644 --- a/www/docs/customization/templates.md +++ b/www/docs/customization/templates.md @@ -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: From b07bd7ca3de0bca3d3d6cf7a951909a4c952b156 Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Thu, 24 Nov 2022 14:47:41 -0300 Subject: [PATCH 2/2] chore: go mod tidy Signed-off-by: Carlos A Becker --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 37e09a58f3f..3e8767fe7f7 100644 --- a/go.mod +++ b/go.mod @@ -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 ) @@ -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