mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-05-29 08:57:44 +08:00
Provide current env as variables in eval context
Signed-off-by: Patrick Van Stee <patrick@vanstee.me>
This commit is contained in:
parent
4121ae50b5
commit
a520de447e
21
bake/hcl.go
21
bake/hcl.go
@ -1,10 +1,27 @@
|
|||||||
package bake
|
package bake
|
||||||
|
|
||||||
import "github.com/hashicorp/hcl/v2/hclsimple"
|
import (
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
hcl "github.com/hashicorp/hcl/v2"
|
||||||
|
"github.com/hashicorp/hcl/v2/hclsimple"
|
||||||
|
"github.com/zclconf/go-cty/cty"
|
||||||
|
)
|
||||||
|
|
||||||
func ParseHCL(dt []byte, fn string) (*Config, error) {
|
func ParseHCL(dt []byte, fn string) (*Config, error) {
|
||||||
|
variables := make(map[string]cty.Value)
|
||||||
|
for _, env := range os.Environ() {
|
||||||
|
parts := strings.SplitN(env, "=", 2)
|
||||||
|
variables[parts[0]] = cty.StringVal(parts[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := &hcl.EvalContext{
|
||||||
|
Variables: variables,
|
||||||
|
}
|
||||||
|
|
||||||
var c Config
|
var c Config
|
||||||
if err := hclsimple.Decode(fn, dt, nil, &c); err != nil {
|
if err := hclsimple.Decode(fn, dt, ctx, &c); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &c, nil
|
return &c, nil
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
package bake
|
package bake
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParseHCL(t *testing.T) {
|
func TestParseHCL(t *testing.T) {
|
||||||
|
os.Setenv("IAMCROSS", "true")
|
||||||
|
|
||||||
var dt = []byte(`
|
var dt = []byte(`
|
||||||
group "default" {
|
group "default" {
|
||||||
targets = ["db", "webapp"]
|
targets = ["db", "webapp"]
|
||||||
@ -35,7 +38,7 @@ func TestParseHCL(t *testing.T) {
|
|||||||
target "webapp-plus" {
|
target "webapp-plus" {
|
||||||
inherits = ["webapp", "cross"]
|
inherits = ["webapp", "cross"]
|
||||||
args = {
|
args = {
|
||||||
IAMCROSS = "true"
|
IAMCROSS = "${IAMCROSS}"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`)
|
`)
|
||||||
@ -58,4 +61,8 @@ func TestParseHCL(t *testing.T) {
|
|||||||
require.Equal(t, c.Targets[2].Name, "cross")
|
require.Equal(t, c.Targets[2].Name, "cross")
|
||||||
require.Equal(t, 2, len(c.Targets[2].Platforms))
|
require.Equal(t, 2, len(c.Targets[2].Platforms))
|
||||||
require.Equal(t, []string{"linux/amd64", "linux/arm64"}, c.Targets[2].Platforms)
|
require.Equal(t, []string{"linux/amd64", "linux/arm64"}, c.Targets[2].Platforms)
|
||||||
|
|
||||||
|
require.Equal(t, c.Targets[3].Name, "webapp-plus")
|
||||||
|
require.Equal(t, 1, len(c.Targets[3].Args))
|
||||||
|
require.Equal(t, map[string]string{"IAMCROSS": "true"}, c.Targets[3].Args)
|
||||||
}
|
}
|
||||||
|
1
go.mod
1
go.mod
@ -55,6 +55,7 @@ require (
|
|||||||
github.com/theupdateframework/notary v0.6.1 // indirect
|
github.com/theupdateframework/notary v0.6.1 // indirect
|
||||||
github.com/tonistiigi/units v0.0.0-20180711220420-6950e57a87ea
|
github.com/tonistiigi/units v0.0.0-20180711220420-6950e57a87ea
|
||||||
github.com/xlab/handysort v0.0.0-20150421192137-fb3537ed64a1 // indirect
|
github.com/xlab/handysort v0.0.0-20150421192137-fb3537ed64a1 // indirect
|
||||||
|
github.com/zclconf/go-cty v1.2.0
|
||||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
|
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
|
||||||
gopkg.in/dancannon/gorethink.v3 v3.0.5 // indirect
|
gopkg.in/dancannon/gorethink.v3 v3.0.5 // indirect
|
||||||
gopkg.in/fatih/pool.v2 v2.0.0 // indirect
|
gopkg.in/fatih/pool.v2 v2.0.0 // indirect
|
||||||
|
Loading…
x
Reference in New Issue
Block a user