Skip to content

Commit

Permalink
Parse bake config as hcl falling back to json
Browse files Browse the repository at this point in the history
  • Loading branch information
vanstee committed May 8, 2020
1 parent 5f23874 commit a3a0b95
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions bake/hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a3a0b95

Please sign in to comment.