Skip to content

Commit

Permalink
fix: validate env templates
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Nov 24, 2022
1 parent 9abc613 commit ad52cb2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
5 changes: 4 additions & 1 deletion internal/tmpl/tmpl.go
Expand Up @@ -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 == "" || v == "" {
continue
}
result[k] = v
}
return t.WithEnv(result)
Expand Down
13 changes: 11 additions & 2 deletions internal/tmpl/tmpl_test.go
Expand Up @@ -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,", out)
}

func TestFuncMap(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions www/docs/customization/build.md
Expand Up @@ -71,6 +71,8 @@ builds:

# Custom environment variables to be set during the builds.
#
# Invalid and empty environment variables are ignored.
#
# Default: `os.Environ()` merged with what you set the root `env` section.
env:
- CGO_ENABLED=0
Expand Down

0 comments on commit ad52cb2

Please sign in to comment.