1
0
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:
Carlos Alexandro Becker 2022-11-24 17:12:54 -03:00 committed by GitHub
parent 9abc613ad1
commit a1305d3912
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 3 deletions

View File

@ -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)

View File

@ -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) {

View File

@ -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