Skip to content

Commit

Permalink
Merge pull request #280 from vanstee/hcl-json-support
Browse files Browse the repository at this point in the history
Support parsing json config with hcl v2
  • Loading branch information
tonistiigi authored May 8, 2020
2 parents fc78756 + 355261e commit 42448c5
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
4 changes: 2 additions & 2 deletions bake/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
19 changes: 15 additions & 4 deletions bake/hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -103,10 +104,20 @@ type staticConfig struct {
}

func ParseHCL(dt []byte, fn string) (*Config, error) {
// Decode user defined functions.
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 {
Expand Down
60 changes: 60 additions & 0 deletions bake/hcl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down

0 comments on commit 42448c5

Please sign in to comment.