mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-07-05 02:57:41 +08:00
Merge pull request #280 from vanstee/hcl-json-support
Support parsing json config with hcl v2
This commit is contained in:
commit
42448c5f37
@ -72,8 +72,8 @@ func ParseFile(fn string) (*Config, error) {
|
|||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Variables []*Variable `json:"-" hcl:"variable,block"`
|
Variables []*Variable `json:"-" hcl:"variable,block"`
|
||||||
Groups []*Group `json:"groups" hcl:"group,block"`
|
Groups []*Group `json:"group" hcl:"group,block"`
|
||||||
Targets []*Target `json:"targets" hcl:"target,block"`
|
Targets []*Target `json:"target" hcl:"target,block"`
|
||||||
Remain hcl.Body `json:"-" hcl:",remain"`
|
Remain hcl.Body `json:"-" hcl:",remain"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
19
bake/hcl.go
19
bake/hcl.go
@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/hashicorp/hcl/v2/ext/userfunc"
|
"github.com/hashicorp/hcl/v2/ext/userfunc"
|
||||||
"github.com/hashicorp/hcl/v2/hclsimple"
|
"github.com/hashicorp/hcl/v2/hclsimple"
|
||||||
"github.com/hashicorp/hcl/v2/hclsyntax"
|
"github.com/hashicorp/hcl/v2/hclsyntax"
|
||||||
|
"github.com/hashicorp/hcl/v2/json"
|
||||||
"github.com/zclconf/go-cty/cty"
|
"github.com/zclconf/go-cty/cty"
|
||||||
"github.com/zclconf/go-cty/cty/function"
|
"github.com/zclconf/go-cty/cty/function"
|
||||||
"github.com/zclconf/go-cty/cty/function/stdlib"
|
"github.com/zclconf/go-cty/cty/function/stdlib"
|
||||||
@ -103,10 +104,20 @@ type staticConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ParseHCL(dt []byte, fn string) (*Config, error) {
|
func ParseHCL(dt []byte, fn string) (*Config, error) {
|
||||||
// Decode user defined functions.
|
// Decode user defined functions, first parsing as hcl and falling back to
|
||||||
file, diags := hclsyntax.ParseConfig(dt, fn, hcl.Pos{Line: 1, Column: 1})
|
// json, returning errors based on the file suffix.
|
||||||
if diags.HasErrors() {
|
file, hcldiags := hclsyntax.ParseConfig(dt, fn, hcl.Pos{Line: 1, Column: 1})
|
||||||
return nil, diags
|
if hcldiags.HasErrors() {
|
||||||
|
var jsondiags hcl.Diagnostics
|
||||||
|
file, jsondiags = json.Parse(dt, fn)
|
||||||
|
if jsondiags.HasErrors() {
|
||||||
|
fnl := strings.ToLower(fn)
|
||||||
|
if strings.HasSuffix(fnl, ".json") {
|
||||||
|
return nil, jsondiags
|
||||||
|
} else {
|
||||||
|
return nil, hcldiags
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
userFunctions, _, diags := userfunc.DecodeUserFunctions(file.Body, "function", func() *hcl.EvalContext {
|
userFunctions, _, diags := userfunc.DecodeUserFunctions(file.Body, "function", func() *hcl.EvalContext {
|
||||||
|
@ -68,6 +68,66 @@ func TestParseHCL(t *testing.T) {
|
|||||||
require.Equal(t, map[string]string{"IAMCROSS": "true"}, c.Targets[3].Args)
|
require.Equal(t, map[string]string{"IAMCROSS": "true"}, c.Targets[3].Args)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("BasicInJSON", func(t *testing.T) {
|
||||||
|
dt := []byte(`
|
||||||
|
{
|
||||||
|
"group": {
|
||||||
|
"default": {
|
||||||
|
"targets": ["db", "webapp"]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"target": {
|
||||||
|
"db": {
|
||||||
|
"context": "./db",
|
||||||
|
"tags": ["docker.io/tonistiigi/db"]
|
||||||
|
},
|
||||||
|
"webapp": {
|
||||||
|
"context": "./dir",
|
||||||
|
"dockerfile": "Dockerfile-alternate",
|
||||||
|
"args": {
|
||||||
|
"buildno": "123"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"cross": {
|
||||||
|
"platforms": [
|
||||||
|
"linux/amd64",
|
||||||
|
"linux/arm64"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"webapp-plus": {
|
||||||
|
"inherits": ["webapp", "cross"],
|
||||||
|
"args": {
|
||||||
|
"IAMCROSS": "true"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`)
|
||||||
|
|
||||||
|
c, err := ParseHCL(dt, "docker-bake.json")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
require.Equal(t, 1, len(c.Groups))
|
||||||
|
require.Equal(t, "default", c.Groups[0].Name)
|
||||||
|
require.Equal(t, []string{"db", "webapp"}, c.Groups[0].Targets)
|
||||||
|
|
||||||
|
require.Equal(t, 4, len(c.Targets))
|
||||||
|
require.Equal(t, c.Targets[0].Name, "db")
|
||||||
|
require.Equal(t, "./db", *c.Targets[0].Context)
|
||||||
|
|
||||||
|
require.Equal(t, c.Targets[1].Name, "webapp")
|
||||||
|
require.Equal(t, 1, len(c.Targets[1].Args))
|
||||||
|
require.Equal(t, "123", c.Targets[1].Args["buildno"])
|
||||||
|
|
||||||
|
require.Equal(t, c.Targets[2].Name, "cross")
|
||||||
|
require.Equal(t, 2, len(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)
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("WithFunctions", func(t *testing.T) {
|
t.Run("WithFunctions", func(t *testing.T) {
|
||||||
dt := []byte(`
|
dt := []byte(`
|
||||||
group "default" {
|
group "default" {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user