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

feat: isEnvSet tmpl function (#4775)

useful for conditionally enable/disable things based on whether an env
var is set or not
This commit is contained in:
Carlos Alexandro Becker 2024-04-12 11:09:18 -03:00 committed by GitHub
parent c1615ea521
commit 60a1cccf57
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 8 additions and 0 deletions

View File

@ -230,6 +230,7 @@ func (t *Template) Apply(s string) (string, error) {
"reverseFilter": filter(true),
"mdv2escape": mdv2Escape,
"envOrDefault": t.envOrDefault,
"isEnvSet": t.isEnvSet,
"map": makemap,
"indexOrDefault": indexOrDefault,
}).
@ -256,6 +257,11 @@ func (t *Template) ApplyAll(sps ...*string) error {
return nil
}
func (t *Template) isEnvSet(name string) bool {
s, ok := t.fields[env].(context.Env)[name]
return ok && s != ""
}
func (t *Template) envOrDefault(name, value string) string {
s, ok := t.fields[env].(context.Env)[name]
if !ok {

View File

@ -96,6 +96,7 @@ func TestWithArtifact(t *testing.T) {
"state dirty": `state {{.GitTreeState}}`,
"env bar: barrrrr": `env bar: {{ envOrDefault "BAR" "barrrrr" }}`,
"env foo: bar": `env foo: {{ envOrDefault "FOO" "barrrrr" }}`,
"env foo is set: true": `env foo is set: {{ isEnvSet "FOO" }}`,
"remove this": "{{ filter .Env.MULTILINE \".*remove.*\" }}",
"something with\nmultiple lines\nto test things": "{{ reverseFilter .Env.MULTILINE \".*remove.*\" }}",

View File

@ -160,6 +160,7 @@ On all fields, you have these available functions:
| `title "foo"` | "titlenize" the string using english as language. See [Title](https://pkg.go.dev/golang.org/x/text/cases#Title). Since v1.14 |
| `mdv2escape "foo"` | escape characters according to MarkdownV2, especially useful in the Telegram integration. Since v1.19 |
| `envOrDefault "NAME" "value"` | either gets the value of the given environment variable, or the given default. Since v1.19 |
| `isEnvSet "NAME"` | returns true if the env is set and not empty, false otherwise. Since v1.26 |
| `$m := map "KEY" "VALUE"` | creates a map from a list of key and value pairs. Both keys and values must be of type `string`. Since v1.21 |
| `indexOrDefault $m "KEY" "value"` | either gets the value of the given key or the given default value from the given map. Since v1.21 |