Skip to content

Commit

Permalink
bake: merge attributes from multiple JSON files
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Apr 5, 2022
1 parent 90f198d commit 5e9d5ff
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
47 changes: 47 additions & 0 deletions bake/hcl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,3 +745,50 @@ target "two" {
require.Equal(t, c.Targets[1].Name, "two")
require.Equal(t, map[string]string{"b": "pre-jkl"}, c.Targets[1].Args)
}

func TestCombineHCLAndJSONAttrs(t *testing.T) {
c, err := ParseFiles([]File{
{
Name: "docker-bake.hcl",
Data: []byte(`
ABC = "foo"
variable "DEF" {
default = ""
}
group "default" {
targets = ["one"]
}
target "one" {
args = {
a = "pre-${ABC}"
}
}
target "two" {
args = {
b = "pre-${DEF}"
}
}`),
},
{
Name: "foo.json",
Data: []byte(`{"ABC": "oof", "variable": {"DEF": {"default": "bar"}}, "target": { "one": { "args": {"a": "pre-${ABC}-${DEF}"}} } }`),
},
{
Name: "bar.json",
Data: []byte(`{"ABC": "ghi", "DEF": "jkl"}`),
},
}, nil)
require.NoError(t, err)

require.Equal(t, 1, len(c.Groups))
require.Equal(t, "default", c.Groups[0].Name)
require.Equal(t, []string{"one"}, c.Groups[0].Targets)

require.Equal(t, 2, len(c.Targets))

require.Equal(t, c.Targets[0].Name, "one")
require.Equal(t, map[string]string{"a": "pre-ghi-jkl"}, c.Targets[0].Args)

require.Equal(t, c.Targets[1].Name, "two")
require.Equal(t, map[string]string{"b": "pre-jkl"}, c.Targets[1].Args)
}
20 changes: 13 additions & 7 deletions bake/hclparser/hclparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,6 @@ func Parse(b hcl.Body, opt Opt, val interface{}) hcl.Diagnostics {
}

attrs, diags := b.JustAttributes()
if diags.HasErrors() {
if d := removeAttributesDiags(diags, reserved, p.vars); len(d) > 0 {
return d
}
}

for _, v := range attrs {
if _, ok := reserved[v.Name]; ok {
continue
Expand All @@ -315,6 +309,12 @@ func Parse(b hcl.Body, opt Opt, val interface{}) hcl.Diagnostics {
}
delete(p.attrs, "function")

if diags.HasErrors() {
if d := removeAttributesDiags(diags, reserved, p.vars, p.attrs); len(d) > 0 {
return d
}
}

for k := range p.opt.Vars {
_ = p.resolveValue(k)
}
Expand Down Expand Up @@ -513,7 +513,7 @@ func setLabel(v reflect.Value, lbl string) int {
return -1
}

func removeAttributesDiags(diags hcl.Diagnostics, reserved map[string]struct{}, vars map[string]*variable) hcl.Diagnostics {
func removeAttributesDiags(diags hcl.Diagnostics, reserved map[string]struct{}, vars map[string]*variable, attrs map[string]*hcl.Attribute) hcl.Diagnostics {
var fdiags hcl.Diagnostics
for _, d := range diags {
if fout := func(d *hcl.Diagnostic) bool {
Expand All @@ -535,6 +535,12 @@ func removeAttributesDiags(diags hcl.Diagnostics, reserved map[string]struct{},
return true
}
}
for a := range attrs {
// Do the same for global attributes
if strings.HasPrefix(d.Detail, fmt.Sprintf(`Argument "%s" was already set at `, a)) {
return true
}
}
return false
}(d); !fout {
fdiags = append(fdiags, d)
Expand Down

0 comments on commit 5e9d5ff

Please sign in to comment.