From 340686a383cffd5bb04274b7c81f27c60d4cba81 Mon Sep 17 00:00:00 2001 From: Patrick Van Stee Date: Thu, 7 May 2020 21:50:32 -0400 Subject: [PATCH 1/4] Support parsing json config with hcl v2 Signed-off-by: Patrick Van Stee --- bake/hcl.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/bake/hcl.go b/bake/hcl.go index cce83821..7dd372a9 100644 --- a/bake/hcl.go +++ b/bake/hcl.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/hcl/v2/ext/userfunc" "github.com/hashicorp/hcl/v2/hclsimple" "github.com/hashicorp/hcl/v2/hclsyntax" + "github.com/hashicorp/hcl/v2/json" "github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty/function" "github.com/zclconf/go-cty/cty/function/stdlib" @@ -73,8 +74,16 @@ type staticConfig struct { } func ParseHCL(dt []byte, fn string) (*Config, error) { + var file *hcl.File + var diags hcl.Diagnostics + // Decode user defined functions. - file, diags := hclsyntax.ParseConfig(dt, fn, hcl.Pos{Line: 1, Column: 1}) + fnl := strings.ToLower(fn) + if strings.HasSuffix(fnl, ".json") { + file, diags = json.Parse(dt, fn) + } else { + file, diags = hclsyntax.ParseConfig(dt, fn, hcl.Pos{Line: 1, Column: 1}) + } if diags.HasErrors() { return nil, diags } From 1bc068a5838ec67d82013c1bb22b32d3a6d84280 Mon Sep 17 00:00:00 2001 From: Patrick Van Stee Date: Thu, 7 May 2020 21:59:49 -0400 Subject: [PATCH 2/4] Fix json keys for groups and targets Signed-off-by: Patrick Van Stee --- bake/bake.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bake/bake.go b/bake/bake.go index 26c0e0f2..e5ee0ca9 100644 --- a/bake/bake.go +++ b/bake/bake.go @@ -72,8 +72,8 @@ func ParseFile(fn string) (*Config, error) { type Config struct { Variables []*Variable `json:"-" hcl:"variable,block"` - Groups []*Group `json:"groups" hcl:"group,block"` - Targets []*Target `json:"targets" hcl:"target,block"` + Groups []*Group `json:"group" hcl:"group,block"` + Targets []*Target `json:"target" hcl:"target,block"` Remain hcl.Body `json:"-" hcl:",remain"` } From 44c840b31d1712d0f1031e9a532dfc47fae4148b Mon Sep 17 00:00:00 2001 From: Patrick Van Stee Date: Thu, 7 May 2020 23:51:06 -0400 Subject: [PATCH 3/4] Add test of parsing a json bake config Signed-off-by: Patrick Van Stee --- bake/hcl_test.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/bake/hcl_test.go b/bake/hcl_test.go index 7ea047a0..6cb53ea3 100644 --- a/bake/hcl_test.go +++ b/bake/hcl_test.go @@ -68,6 +68,66 @@ func TestParseHCL(t *testing.T) { 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) { dt := []byte(` group "default" { From 355261e49ee4af63a198917d538dd8b99b091e43 Mon Sep 17 00:00:00 2001 From: Patrick Van Stee Date: Thu, 7 May 2020 23:53:21 -0400 Subject: [PATCH 4/4] Parse bake config as hcl falling back to json Signed-off-by: Patrick Van Stee --- bake/hcl.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/bake/hcl.go b/bake/hcl.go index 7dd372a9..555d91e1 100644 --- a/bake/hcl.go +++ b/bake/hcl.go @@ -74,18 +74,20 @@ type staticConfig struct { } func ParseHCL(dt []byte, fn string) (*Config, error) { - var file *hcl.File - var diags hcl.Diagnostics - - // Decode user defined functions. - fnl := strings.ToLower(fn) - if strings.HasSuffix(fnl, ".json") { - file, diags = json.Parse(dt, fn) - } else { - file, diags = hclsyntax.ParseConfig(dt, fn, hcl.Pos{Line: 1, Column: 1}) - } - if diags.HasErrors() { - return nil, diags + // Decode user defined functions, first parsing as hcl and falling back to + // json, returning errors based on the file suffix. + file, hcldiags := hclsyntax.ParseConfig(dt, fn, hcl.Pos{Line: 1, Column: 1}) + 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 {