mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-02-03 13:11:48 +02:00
fix: validate env templates (#3591)
ignore invalid environment variables (e.g. no key, no value, or no equal sign) Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
parent
9abc613ad1
commit
a1305d3912
@ -119,7 +119,10 @@ func New(ctx *context.Context) *Template {
|
||||
func (t *Template) WithEnvS(envs []string) *Template {
|
||||
result := map[string]string{}
|
||||
for _, env := range envs {
|
||||
k, v, _ := strings.Cut(env, "=")
|
||||
k, v, ok := strings.Cut(env, "=")
|
||||
if !ok || k == "" {
|
||||
continue
|
||||
}
|
||||
result[k] = v
|
||||
}
|
||||
return t.WithEnv(result)
|
||||
|
@ -159,12 +159,21 @@ func TestWithEnv(t *testing.T) {
|
||||
"FOO": "BAR",
|
||||
}
|
||||
ctx.Git.CurrentTag = "v1.2.3"
|
||||
out, err := New(ctx).WithEnvS([]string{
|
||||
tpl := New(ctx).WithEnvS([]string{
|
||||
"FOO=foo",
|
||||
"BAR=bar",
|
||||
}).Apply("{{ .Env.FOO }}-{{ .Env.BAR }}")
|
||||
"NOVAL=",
|
||||
"=NOKEY",
|
||||
"=",
|
||||
"NOTHING",
|
||||
})
|
||||
out, err := tpl.Apply("{{ .Env.FOO }}-{{ .Env.BAR }}")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "foo-bar", out)
|
||||
|
||||
out, err = tpl.Apply(`{{ range $idx, $key := .Env }}{{ $idx }},{{ end }}`)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "BAR,FOO,NOVAL,", out)
|
||||
}
|
||||
|
||||
func TestFuncMap(t *testing.T) {
|
||||
|
@ -71,6 +71,8 @@ builds:
|
||||
|
||||
# Custom environment variables to be set during the builds.
|
||||
#
|
||||
# Invalid environment variables will be ignored.
|
||||
#
|
||||
# Default: `os.Environ()` merged with what you set the root `env` section.
|
||||
env:
|
||||
- CGO_ENABLED=0
|
||||
|
Loading…
x
Reference in New Issue
Block a user