From 355261e49ee4af63a198917d538dd8b99b091e43 Mon Sep 17 00:00:00 2001 From: Patrick Van Stee Date: Thu, 7 May 2020 23:53:21 -0400 Subject: [PATCH] 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 {