mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-01-10 03:47:03 +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 (
|
import (
|
||||||
ctx "context"
|
ctx "context"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -31,6 +32,7 @@ type Binary struct {
|
|||||||
type Context struct {
|
type Context struct {
|
||||||
ctx.Context
|
ctx.Context
|
||||||
Config config.Project
|
Config config.Project
|
||||||
|
Env map[string]string
|
||||||
Token string
|
Token string
|
||||||
Git GitInfo
|
Git GitInfo
|
||||||
Binaries map[string]map[string][]Binary
|
Binaries map[string]map[string][]Binary
|
||||||
@ -96,6 +98,16 @@ func New(config config.Project) *Context {
|
|||||||
return &Context{
|
return &Context{
|
||||||
Context: ctx.Background(),
|
Context: ctx.Background(),
|
||||||
Config: config,
|
Config: config,
|
||||||
|
Env: splitEnv(os.Environ()),
|
||||||
Parallelism: 4,
|
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 (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -25,7 +23,7 @@ func ldflags(ctx *context.Context, build config.Build) (string, error) {
|
|||||||
Tag: ctx.Git.CurrentTag,
|
Tag: ctx.Git.CurrentTag,
|
||||||
Version: ctx.Version,
|
Version: ctx.Version,
|
||||||
Date: time.Now().UTC().Format(time.RFC3339),
|
Date: time.Now().UTC().Format(time.RFC3339),
|
||||||
Env: loadEnvs(),
|
Env: ctx.Env,
|
||||||
}
|
}
|
||||||
var out bytes.Buffer
|
var out bytes.Buffer
|
||||||
t, err := template.New("ldflags").Parse(build.Ldflags)
|
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)
|
err = t.Execute(&out, data)
|
||||||
return out.String(), err
|
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
|
package build
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/goreleaser/goreleaser/config"
|
"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{
|
var ctx = &context.Context{
|
||||||
Git: context.GitInfo{
|
Git: context.GitInfo{
|
||||||
CurrentTag: "v1.2.3",
|
CurrentTag: "v1.2.3",
|
||||||
@ -26,6 +23,7 @@ func TestLdFlagsFullTemplate(t *testing.T) {
|
|||||||
},
|
},
|
||||||
Version: "1.2.3",
|
Version: "1.2.3",
|
||||||
Config: config,
|
Config: config,
|
||||||
|
Env: map[string]string{"FOO": "123"},
|
||||||
}
|
}
|
||||||
flags, err := ldflags(ctx, ctx.Config.Builds[0])
|
flags, err := ldflags(ctx, ctx.Config.Builds[0])
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
Loading…
Reference in New Issue
Block a user