1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-11-06 09:09:29 +02:00

fix: all envs allowed on build template (#992)

This commit is contained in:
Carlos Alexandro Becker
2019-04-09 09:15:05 -03:00
committed by GitHub
parent ce69ee5316
commit 310bf450a8
7 changed files with 68 additions and 25 deletions

View File

@@ -3,6 +3,7 @@ package tmpl
import (
"bytes"
"strings"
"text/template"
"time"
@@ -63,6 +64,23 @@ func New(ctx *context.Context) *Template {
}
}
// WithEnvS overrides template's env field with the given KEY=VALUE list of
// environment variables
func (t *Template) WithEnvS(envs []string) *Template {
var result = map[string]string{}
for _, env := range envs {
var parts = strings.SplitN(env, "=", 2)
result[parts[0]] = parts[1]
}
return t.WithEnv(result)
}
// WithEnv overrides template's env field with the given environment map
func (t *Template) WithEnv(e map[string]string) *Template {
t.fields[env] = e
return t
}
// WithArtifact populates fields from the artifact and replacements
func (t *Template) WithArtifact(a artifact.Artifact, replacements map[string]string) *Template {
var bin = a.Extra[binary]

View File

@@ -113,6 +113,20 @@ func TestEnv(t *testing.T) {
}
}
func TestWithEnv(t *testing.T) {
var ctx = context.New(config.Project{})
ctx.Env = map[string]string{
"FOO": "BAR",
}
ctx.Git.CurrentTag = "v1.2.3"
out, err := New(ctx).WithEnvS([]string{
"FOO=foo",
"BAR=bar",
}).Apply("{{ .Env.FOO }}-{{ .Env.BAR }}")
assert.NoError(t, err)
assert.Equal(t, "foo-bar", out)
}
func TestFuncMap(t *testing.T) {
var ctx = context.New(config.Project{
ProjectName: "proj",