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:
parent
3e0b7fbd46
commit
1c2afe148f
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user