1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-08 03:31:59 +02:00

fix: move env vars to context

In preparation to support env vars for Docker tag_template and also to
simplify the tests by not chaning the global os.Environ I've moved the
parsed env var map into the context.Context.
This commit is contained in:
Frank Schroeder 2017-12-06 00:01:47 +01:00 committed by Carlos Alexandro Becker
parent 3e0b7fbd46
commit 1c2afe148f
3 changed files with 14 additions and 15 deletions

View File

@ -8,6 +8,7 @@ package context
import (
ctx "context"
"os"
"path/filepath"
"strings"
"sync"
@ -31,6 +32,7 @@ type Binary struct {
type Context struct {
ctx.Context
Config config.Project
Env map[string]string
Token string
Git GitInfo
Binaries map[string]map[string][]Binary
@ -96,6 +98,16 @@ func New(config config.Project) *Context {
return &Context{
Context: ctx.Background(),
Config: config,
Env: splitEnv(os.Environ()),
Parallelism: 4,
}
}
func splitEnv(env []string) map[string]string {
r := map[string]string{}
for _, e := range env {
p := strings.SplitN(e, "=", 2)
r[p[0]] = p[1]
}
return r
}

View File

@ -2,8 +2,6 @@ package build
import (
"bytes"
"os"
"strings"
"text/template"
"time"
@ -25,7 +23,7 @@ func ldflags(ctx *context.Context, build config.Build) (string, error) {
Tag: ctx.Git.CurrentTag,
Version: ctx.Version,
Date: time.Now().UTC().Format(time.RFC3339),
Env: loadEnvs(),
Env: ctx.Env,
}
var out bytes.Buffer
t, err := template.New("ldflags").Parse(build.Ldflags)
@ -35,12 +33,3 @@ func ldflags(ctx *context.Context, build config.Build) (string, error) {
err = t.Execute(&out, data)
return out.String(), err
}
func loadEnvs() map[string]string {
r := map[string]string{}
for _, e := range os.Environ() {
env := strings.SplitN(e, "=", 2)
r[env[0]] = env[1]
}
return r
}

View File

@ -1,7 +1,6 @@
package build
import (
"os"
"testing"
"github.com/goreleaser/goreleaser/config"
@ -17,8 +16,6 @@ func TestLdFlagsFullTemplate(t *testing.T) {
},
},
}
os.Setenv("FOO", "123")
defer os.Unsetenv("FOO")
var ctx = &context.Context{
Git: context.GitInfo{
CurrentTag: "v1.2.3",
@ -26,6 +23,7 @@ func TestLdFlagsFullTemplate(t *testing.T) {
},
Version: "1.2.3",
Config: config,
Env: map[string]string{"FOO": "123"},
}
flags, err := ldflags(ctx, ctx.Config.Builds[0])
assert.NoError(t, err)