Skip to content

Commit

Permalink
bake: add list-variables option
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <[email protected]>
  • Loading branch information
tonistiigi committed Jun 29, 2024
1 parent fb400bd commit 1c27502
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 60 deletions.
29 changes: 16 additions & 13 deletions bake/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func readWithProgress(r io.Reader, setStatus func(st *client.VertexStatus)) (dt
}

func ListTargets(files []File) ([]string, error) {
c, err := ParseFiles(files, nil)
c, _, err := ParseFiles(files, nil)
if err != nil {
return nil, err
}
Expand All @@ -192,7 +192,7 @@ func ListTargets(files []File) ([]string, error) {
}

func ReadTargets(ctx context.Context, files []File, targets, overrides []string, defaults map[string]string) (map[string]*Target, map[string]*Group, error) {
c, err := ParseFiles(files, defaults)
c, _, err := ParseFiles(files, defaults)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -298,7 +298,7 @@ func sliceToMap(env []string) (res map[string]string) {
return
}

func ParseFiles(files []File, defaults map[string]string) (_ *Config, err error) {
func ParseFiles(files []File, defaults map[string]string) (_ *Config, _ *hclparser.ParseMeta, err error) {
defer func() {
err = formatHCLError(err, files)
}()
Expand All @@ -310,45 +310,46 @@ func ParseFiles(files []File, defaults map[string]string) (_ *Config, err error)
isCompose, composeErr := validateComposeFile(f.Data, f.Name)
if isCompose {
if composeErr != nil {
return nil, composeErr
return nil, nil, composeErr
}
composeFiles = append(composeFiles, f)
}
if !isCompose {
hf, isHCL, err := ParseHCLFile(f.Data, f.Name)
if isHCL {
if err != nil {
return nil, err
return nil, nil, err
}
hclFiles = append(hclFiles, hf)
} else if composeErr != nil {
return nil, errors.Wrapf(err, "failed to parse %s: parsing yaml: %v, parsing hcl", f.Name, composeErr)
return nil, nil, errors.Wrapf(err, "failed to parse %s: parsing yaml: %v, parsing hcl", f.Name, composeErr)
} else {
return nil, err
return nil, nil, err
}
}
}

if len(composeFiles) > 0 {
cfg, cmperr := ParseComposeFiles(composeFiles)
if cmperr != nil {
return nil, errors.Wrap(cmperr, "failed to parse compose file")
return nil, nil, errors.Wrap(cmperr, "failed to parse compose file")
}
c = mergeConfig(c, *cfg)
c = dedupeConfig(c)
}

var pm hclparser.ParseMeta
if len(hclFiles) > 0 {
renamed, err := hclparser.Parse(hclparser.MergeFiles(hclFiles), hclparser.Opt{
res, err := hclparser.Parse(hclparser.MergeFiles(hclFiles), hclparser.Opt{
LookupVar: os.LookupEnv,
Vars: defaults,
ValidateLabel: validateTargetName,
}, &c)
if err.HasErrors() {
return nil, err
return nil, nil, err
}

for _, renamed := range renamed {
for _, renamed := range res.Renamed {
for oldName, newNames := range renamed {
newNames = dedupSlice(newNames)
if len(newNames) == 1 && oldName == newNames[0] {
Expand All @@ -361,9 +362,10 @@ func ParseFiles(files []File, defaults map[string]string) (_ *Config, err error)
}
}
c = dedupeConfig(c)
pm = *res
}

return &c, nil
return &c, &pm, nil
}

func dedupeConfig(c Config) Config {
Expand All @@ -388,7 +390,8 @@ func dedupeConfig(c Config) Config {
}

func ParseFile(dt []byte, fn string) (*Config, error) {
return ParseFiles([]File{{Data: dt, Name: fn}}, nil)
c, _, err := ParseFiles([]File{{Data: dt, Name: fn}}, nil)
return c, err
}

type Config struct {
Expand Down
2 changes: 1 addition & 1 deletion bake/bake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1528,7 +1528,7 @@ services:
v2: "bar"
`)

c, err := ParseFiles([]File{
c, _, err := ParseFiles([]File{
{Data: dt, Name: "c1.foo"},
{Data: dt2, Name: "c2.bar"},
}, nil)
Expand Down
26 changes: 13 additions & 13 deletions bake/hcl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func TestHCLMultiFileSharedVariables(t *testing.T) {
}
`)

c, err := ParseFiles([]File{
c, _, err := ParseFiles([]File{
{Data: dt, Name: "c1.hcl"},
{Data: dt2, Name: "c2.hcl"},
}, nil)
Expand All @@ -285,7 +285,7 @@ func TestHCLMultiFileSharedVariables(t *testing.T) {

t.Setenv("FOO", "def")

c, err = ParseFiles([]File{
c, _, err = ParseFiles([]File{
{Data: dt, Name: "c1.hcl"},
{Data: dt2, Name: "c2.hcl"},
}, nil)
Expand Down Expand Up @@ -322,7 +322,7 @@ func TestHCLVarsWithVars(t *testing.T) {
}
`)

c, err := ParseFiles([]File{
c, _, err := ParseFiles([]File{
{Data: dt, Name: "c1.hcl"},
{Data: dt2, Name: "c2.hcl"},
}, nil)
Expand All @@ -334,7 +334,7 @@ func TestHCLVarsWithVars(t *testing.T) {

t.Setenv("BASE", "new")

c, err = ParseFiles([]File{
c, _, err = ParseFiles([]File{
{Data: dt, Name: "c1.hcl"},
{Data: dt2, Name: "c2.hcl"},
}, nil)
Expand Down Expand Up @@ -612,7 +612,7 @@ func TestHCLMultiFileAttrs(t *testing.T) {
FOO="def"
`)

c, err := ParseFiles([]File{
c, _, err := ParseFiles([]File{
{Data: dt, Name: "c1.hcl"},
{Data: dt2, Name: "c2.hcl"},
}, nil)
Expand All @@ -623,7 +623,7 @@ func TestHCLMultiFileAttrs(t *testing.T) {

t.Setenv("FOO", "ghi")

c, err = ParseFiles([]File{
c, _, err = ParseFiles([]File{
{Data: dt, Name: "c1.hcl"},
{Data: dt2, Name: "c2.hcl"},
}, nil)
Expand All @@ -647,7 +647,7 @@ func TestHCLMultiFileGlobalAttrs(t *testing.T) {
FOO = "def"
`)

c, err := ParseFiles([]File{
c, _, err := ParseFiles([]File{
{Data: dt, Name: "c1.hcl"},
{Data: dt2, Name: "c2.hcl"},
}, nil)
Expand Down Expand Up @@ -830,7 +830,7 @@ func TestHCLRenameMultiFile(t *testing.T) {
}
`)

c, err := ParseFiles([]File{
c, _, err := ParseFiles([]File{
{Data: dt, Name: "c1.hcl"},
{Data: dt2, Name: "c2.hcl"},
{Data: dt3, Name: "c3.hcl"},
Expand Down Expand Up @@ -1050,7 +1050,7 @@ func TestHCLMatrixArgsOverride(t *testing.T) {
}
`)

c, err := ParseFiles([]File{
c, _, err := ParseFiles([]File{
{Data: dt, Name: "docker-bake.hcl"},
}, map[string]string{"ABC": "11,22,33"})
require.NoError(t, err)
Expand Down Expand Up @@ -1236,7 +1236,7 @@ services:
v2: "bar"
`)

c, err := ParseFiles([]File{
c, _, err := ParseFiles([]File{
{Data: dt, Name: "c1.hcl"},
{Data: dt2, Name: "c2.yml"},
}, nil)
Expand All @@ -1258,7 +1258,7 @@ func TestHCLBuiltinVars(t *testing.T) {
}
`)

c, err := ParseFiles([]File{
c, _, err := ParseFiles([]File{
{Data: dt, Name: "c1.hcl"},
}, map[string]string{
"BAKE_CMD_CONTEXT": "foo",
Expand All @@ -1272,7 +1272,7 @@ func TestHCLBuiltinVars(t *testing.T) {
}

func TestCombineHCLAndJSONTargets(t *testing.T) {
c, err := ParseFiles([]File{
c, _, err := ParseFiles([]File{
{
Name: "docker-bake.hcl",
Data: []byte(`
Expand Down Expand Up @@ -1348,7 +1348,7 @@ target "b" {
}

func TestCombineHCLAndJSONVars(t *testing.T) {
c, err := ParseFiles([]File{
c, _, err := ParseFiles([]File{
{
Name: "docker-bake.hcl",
Data: []byte(`
Expand Down
42 changes: 37 additions & 5 deletions bake/hclparser/hclparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ type Opt struct {
}

type variable struct {
Name string `json:"-" hcl:"name,label"`
Default *hcl.Attribute `json:"default,omitempty" hcl:"default,optional"`
Body hcl.Body `json:"-" hcl:",body"`
Name string `json:"-" hcl:"name,label"`
Default *hcl.Attribute `json:"default,omitempty" hcl:"default,optional"`
Description string `json:"description,omitempty" hcl:"description,optional"`
Body hcl.Body `json:"-" hcl:",body"`
Remain hcl.Body `json:"-" hcl:",remain"`
}

type functionDef struct {
Expand Down Expand Up @@ -534,7 +536,18 @@ func (p *parser) resolveBlockNames(block *hcl.Block) ([]string, error) {
return names, nil
}

func Parse(b hcl.Body, opt Opt, val interface{}) (map[string]map[string][]string, hcl.Diagnostics) {
type Variable struct {
Name string
Description string
Value *string
}

type ParseMeta struct {
Renamed map[string]map[string][]string
AllVariables []*Variable
}

func Parse(b hcl.Body, opt Opt, val interface{}) (*ParseMeta, hcl.Diagnostics) {
reserved := map[string]struct{}{}
schema, _ := gohcl.ImpliedBodySchema(val)

Expand Down Expand Up @@ -643,6 +656,7 @@ func Parse(b hcl.Body, opt Opt, val interface{}) (map[string]map[string][]string
}
}

vars := make([]*Variable, 0, len(p.vars))
for k := range p.vars {
if err := p.resolveValue(p.ectx, k); err != nil {
if diags, ok := err.(hcl.Diagnostics); ok {
Expand All @@ -651,6 +665,21 @@ func Parse(b hcl.Body, opt Opt, val interface{}) (map[string]map[string][]string
r := p.vars[k].Body.MissingItemRange()
return nil, wrapErrorDiagnostic("Invalid value", err, &r, &r)
}
v := &Variable{
Name: p.vars[k].Name,
Description: p.vars[k].Description,
}
if vv := p.ectx.Variables[k]; !vv.IsNull() {
var s string
switch vv.Type() {
case cty.String:
s = vv.AsString()
case cty.Bool:
s = strconv.FormatBool(vv.True())
}
v.Value = &s
}
vars = append(vars, v)
}

for k := range p.funcs {
Expand Down Expand Up @@ -795,7 +824,10 @@ func Parse(b hcl.Body, opt Opt, val interface{}) (map[string]map[string][]string
}
}

return renamed, nil
return &ParseMeta{
Renamed: renamed,
AllVariables: vars,
}, nil
}

// wrapErrorDiagnostic wraps an error into a hcl.Diagnostics object.
Expand Down
26 changes: 12 additions & 14 deletions bake/hclparser/merged.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,19 @@ func (mb mergedBodies) JustAttributes() (hcl.Attributes, hcl.Diagnostics) {
diags = append(diags, thisDiags...)
}

if thisAttrs != nil {
for name, attr := range thisAttrs {
if existing := attrs[name]; existing != nil {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Duplicate argument",
Detail: fmt.Sprintf(
"Argument %q was already set at %s",
name, existing.NameRange.String(),
),
Subject: thisAttrs[name].NameRange.Ptr(),
})
}
attrs[name] = attr
for name, attr := range thisAttrs {
if existing := attrs[name]; existing != nil {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Duplicate argument",
Detail: fmt.Sprintf(
"Argument %q was already set at %s",
name, existing.NameRange.String(),
),
Subject: thisAttrs[name].NameRange.Ptr(),
})
}
attrs[name] = attr
}
}

Expand Down
Loading

0 comments on commit 1c27502

Please sign in to comment.