mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-03-17 20:47:50 +02:00
feat: add basic string function to template (#1232)
This commit is contained in:
parent
333d834b49
commit
3d95590238
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -42,15 +42,19 @@ may have some extra fields:
|
||||
|
||||
On all fields, you have these available functions:
|
||||
|
||||
| Usage | Description |
|
||||
| :-----------------: | :--------------------------------------: |
|
||||
| `time "01/02/2006"` | current UTC time in the specified format |
|
||||
| Usage | Description |
|
||||
| :--------------------: | :----------------------------------------------------------------------------------: |
|
||||
| `replace "v1.2" "v" ""` | replaces all macthes. See [ReplaceAll](https://golang.org/pkg/strings/#ReplaceAll) |
|
||||
| `time "01/02/2006"` | current UTC time in the specified format |
|
||||
| `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) |
|
||||
|
||||
With all those fields, you may be able to compose the name of your artifacts
|
||||
pretty much the way you want:
|
||||
|
||||
```yaml
|
||||
example_template: '{{ .ProjectName }}_{{ .Env.USER }}_{{ time "2006" }}'
|
||||
example_template: '{{ tolower .ProjectName }}_{{ .Env.USER }}_{{ time "2006" }}'
|
||||
```
|
||||
|
||||
For example, if you want to add the go version to some artifact:
|
||||
|
Loading…
x
Reference in New Issue
Block a user