Skip to content

Commit c51004e

Browse files
authored
Merge pull request #2556 from tonistiigi/bake-call
bake: add call methods support and printing
2 parents 8535c6b + 153e5ed commit c51004e

File tree

10 files changed

+552
-90
lines changed

10 files changed

+552
-90
lines changed

bake/bake.go

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func readWithProgress(r io.Reader, setStatus func(st *client.VertexStatus)) (dt
177177
}
178178

179179
func ListTargets(files []File) ([]string, error) {
180-
c, err := ParseFiles(files, nil)
180+
c, _, err := ParseFiles(files, nil)
181181
if err != nil {
182182
return nil, err
183183
}
@@ -192,7 +192,7 @@ func ListTargets(files []File) ([]string, error) {
192192
}
193193

194194
func ReadTargets(ctx context.Context, files []File, targets, overrides []string, defaults map[string]string) (map[string]*Target, map[string]*Group, error) {
195-
c, err := ParseFiles(files, defaults)
195+
c, _, err := ParseFiles(files, defaults)
196196
if err != nil {
197197
return nil, nil, err
198198
}
@@ -298,7 +298,7 @@ func sliceToMap(env []string) (res map[string]string) {
298298
return
299299
}
300300

301-
func ParseFiles(files []File, defaults map[string]string) (_ *Config, err error) {
301+
func ParseFiles(files []File, defaults map[string]string) (_ *Config, _ *hclparser.ParseMeta, err error) {
302302
defer func() {
303303
err = formatHCLError(err, files)
304304
}()
@@ -310,45 +310,46 @@ func ParseFiles(files []File, defaults map[string]string) (_ *Config, err error)
310310
isCompose, composeErr := validateComposeFile(f.Data, f.Name)
311311
if isCompose {
312312
if composeErr != nil {
313-
return nil, composeErr
313+
return nil, nil, composeErr
314314
}
315315
composeFiles = append(composeFiles, f)
316316
}
317317
if !isCompose {
318318
hf, isHCL, err := ParseHCLFile(f.Data, f.Name)
319319
if isHCL {
320320
if err != nil {
321-
return nil, err
321+
return nil, nil, err
322322
}
323323
hclFiles = append(hclFiles, hf)
324324
} else if composeErr != nil {
325-
return nil, errors.Wrapf(err, "failed to parse %s: parsing yaml: %v, parsing hcl", f.Name, composeErr)
325+
return nil, nil, errors.Wrapf(err, "failed to parse %s: parsing yaml: %v, parsing hcl", f.Name, composeErr)
326326
} else {
327-
return nil, err
327+
return nil, nil, err
328328
}
329329
}
330330
}
331331

332332
if len(composeFiles) > 0 {
333333
cfg, cmperr := ParseComposeFiles(composeFiles)
334334
if cmperr != nil {
335-
return nil, errors.Wrap(cmperr, "failed to parse compose file")
335+
return nil, nil, errors.Wrap(cmperr, "failed to parse compose file")
336336
}
337337
c = mergeConfig(c, *cfg)
338338
c = dedupeConfig(c)
339339
}
340340

341+
var pm hclparser.ParseMeta
341342
if len(hclFiles) > 0 {
342-
renamed, err := hclparser.Parse(hclparser.MergeFiles(hclFiles), hclparser.Opt{
343+
res, err := hclparser.Parse(hclparser.MergeFiles(hclFiles), hclparser.Opt{
343344
LookupVar: os.LookupEnv,
344345
Vars: defaults,
345346
ValidateLabel: validateTargetName,
346347
}, &c)
347348
if err.HasErrors() {
348-
return nil, err
349+
return nil, nil, err
349350
}
350351

351-
for _, renamed := range renamed {
352+
for _, renamed := range res.Renamed {
352353
for oldName, newNames := range renamed {
353354
newNames = dedupSlice(newNames)
354355
if len(newNames) == 1 && oldName == newNames[0] {
@@ -361,9 +362,10 @@ func ParseFiles(files []File, defaults map[string]string) (_ *Config, err error)
361362
}
362363
}
363364
c = dedupeConfig(c)
365+
pm = *res
364366
}
365367

366-
return &c, nil
368+
return &c, &pm, nil
367369
}
368370

369371
func dedupeConfig(c Config) Config {
@@ -388,7 +390,8 @@ func dedupeConfig(c Config) Config {
388390
}
389391

390392
func ParseFile(dt []byte, fn string) (*Config, error) {
391-
return ParseFiles([]File{{Data: dt, Name: fn}}, nil)
393+
c, _, err := ParseFiles([]File{{Data: dt, Name: fn}}, nil)
394+
return c, err
392395
}
393396

394397
type Config struct {
@@ -669,13 +672,15 @@ func (c Config) target(name string, visited map[string]*Target, overrides map[st
669672
}
670673

671674
type Group struct {
672-
Name string `json:"-" hcl:"name,label" cty:"name"`
673-
Targets []string `json:"targets" hcl:"targets" cty:"targets"`
675+
Name string `json:"-" hcl:"name,label" cty:"name"`
676+
Description string `json:"description,omitempty" hcl:"description,optional" cty:"description"`
677+
Targets []string `json:"targets" hcl:"targets" cty:"targets"`
674678
// Target // TODO?
675679
}
676680

677681
type Target struct {
678-
Name string `json:"-" hcl:"name,label" cty:"name"`
682+
Name string `json:"-" hcl:"name,label" cty:"name"`
683+
Description string `json:"description,omitempty" hcl:"description,optional" cty:"description"`
679684

680685
// Inherits is the only field that cannot be overridden with --set
681686
Inherits []string `json:"inherits,omitempty" hcl:"inherits,optional" cty:"inherits"`
@@ -702,7 +707,8 @@ type Target struct {
702707
NoCacheFilter []string `json:"no-cache-filter,omitempty" hcl:"no-cache-filter,optional" cty:"no-cache-filter"`
703708
ShmSize *string `json:"shm-size,omitempty" hcl:"shm-size,optional"`
704709
Ulimits []string `json:"ulimits,omitempty" hcl:"ulimits,optional"`
705-
// IMPORTANT: if you add more fields here, do not forget to update newOverrides and docs/bake-reference.md.
710+
Call *string `json:"call,omitempty" hcl:"call,optional" cty:"call"`
711+
// IMPORTANT: if you add more fields here, do not forget to update newOverrides/AddOverrides and docs/bake-reference.md.
706712

707713
// linked is a private field to mark a target used as a linked one
708714
linked bool
@@ -776,6 +782,9 @@ func (t *Target) Merge(t2 *Target) {
776782
if t2.Target != nil {
777783
t.Target = t2.Target
778784
}
785+
if t2.Call != nil {
786+
t.Call = t2.Call
787+
}
779788
if t2.Annotations != nil { // merge
780789
t.Annotations = append(t.Annotations, t2.Annotations...)
781790
}
@@ -819,6 +828,9 @@ func (t *Target) Merge(t2 *Target) {
819828
if t2.Ulimits != nil { // merge
820829
t.Ulimits = append(t.Ulimits, t2.Ulimits...)
821830
}
831+
if t2.Description != "" {
832+
t.Description = t2.Description
833+
}
822834
t.Inherits = append(t.Inherits, t2.Inherits...)
823835
}
824836

@@ -863,6 +875,8 @@ func (t *Target) AddOverrides(overrides map[string]Override) error {
863875
t.CacheTo = o.ArrValue
864876
case "target":
865877
t.Target = &value
878+
case "call":
879+
t.Call = &value
866880
case "secrets":
867881
t.Secrets = o.ArrValue
868882
case "ssh":
@@ -1298,6 +1312,12 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
12981312
bo.Target = *t.Target
12991313
}
13001314

1315+
if t.Call != nil {
1316+
bo.PrintFunc = &build.PrintFunc{
1317+
Name: *t.Call,
1318+
}
1319+
}
1320+
13011321
cacheImports, err := buildflags.ParseCacheEntry(t.CacheFrom)
13021322
if err != nil {
13031323
return nil, err

bake/bake_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1528,7 +1528,7 @@ services:
15281528
v2: "bar"
15291529
`)
15301530

1531-
c, err := ParseFiles([]File{
1531+
c, _, err := ParseFiles([]File{
15321532
{Data: dt, Name: "c1.foo"},
15331533
{Data: dt2, Name: "c2.bar"},
15341534
}, nil)

bake/hcl_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func TestHCLMultiFileSharedVariables(t *testing.T) {
273273
}
274274
`)
275275

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

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

288-
c, err = ParseFiles([]File{
288+
c, _, err = ParseFiles([]File{
289289
{Data: dt, Name: "c1.hcl"},
290290
{Data: dt2, Name: "c2.hcl"},
291291
}, nil)
@@ -322,7 +322,7 @@ func TestHCLVarsWithVars(t *testing.T) {
322322
}
323323
`)
324324

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

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

337-
c, err = ParseFiles([]File{
337+
c, _, err = ParseFiles([]File{
338338
{Data: dt, Name: "c1.hcl"},
339339
{Data: dt2, Name: "c2.hcl"},
340340
}, nil)
@@ -612,7 +612,7 @@ func TestHCLMultiFileAttrs(t *testing.T) {
612612
FOO="def"
613613
`)
614614

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

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

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

650-
c, err := ParseFiles([]File{
650+
c, _, err := ParseFiles([]File{
651651
{Data: dt, Name: "c1.hcl"},
652652
{Data: dt2, Name: "c2.hcl"},
653653
}, nil)
@@ -830,7 +830,7 @@ func TestHCLRenameMultiFile(t *testing.T) {
830830
}
831831
`)
832832

833-
c, err := ParseFiles([]File{
833+
c, _, err := ParseFiles([]File{
834834
{Data: dt, Name: "c1.hcl"},
835835
{Data: dt2, Name: "c2.hcl"},
836836
{Data: dt3, Name: "c3.hcl"},
@@ -1050,7 +1050,7 @@ func TestHCLMatrixArgsOverride(t *testing.T) {
10501050
}
10511051
`)
10521052

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

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

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

12741274
func TestCombineHCLAndJSONTargets(t *testing.T) {
1275-
c, err := ParseFiles([]File{
1275+
c, _, err := ParseFiles([]File{
12761276
{
12771277
Name: "docker-bake.hcl",
12781278
Data: []byte(`
@@ -1348,7 +1348,7 @@ target "b" {
13481348
}
13491349

13501350
func TestCombineHCLAndJSONVars(t *testing.T) {
1351-
c, err := ParseFiles([]File{
1351+
c, _, err := ParseFiles([]File{
13521352
{
13531353
Name: "docker-bake.hcl",
13541354
Data: []byte(`

bake/hclparser/hclparser.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ type Opt struct {
2525
}
2626

2727
type variable struct {
28-
Name string `json:"-" hcl:"name,label"`
29-
Default *hcl.Attribute `json:"default,omitempty" hcl:"default,optional"`
30-
Body hcl.Body `json:"-" hcl:",body"`
28+
Name string `json:"-" hcl:"name,label"`
29+
Default *hcl.Attribute `json:"default,omitempty" hcl:"default,optional"`
30+
Description string `json:"description,omitempty" hcl:"description,optional"`
31+
Body hcl.Body `json:"-" hcl:",body"`
32+
Remain hcl.Body `json:"-" hcl:",remain"`
3133
}
3234

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

537-
func Parse(b hcl.Body, opt Opt, val interface{}) (map[string]map[string][]string, hcl.Diagnostics) {
539+
type Variable struct {
540+
Name string
541+
Description string
542+
Value *string
543+
}
544+
545+
type ParseMeta struct {
546+
Renamed map[string]map[string][]string
547+
AllVariables []*Variable
548+
}
549+
550+
func Parse(b hcl.Body, opt Opt, val interface{}) (*ParseMeta, hcl.Diagnostics) {
538551
reserved := map[string]struct{}{}
539552
schema, _ := gohcl.ImpliedBodySchema(val)
540553

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

659+
vars := make([]*Variable, 0, len(p.vars))
646660
for k := range p.vars {
647661
if err := p.resolveValue(p.ectx, k); err != nil {
648662
if diags, ok := err.(hcl.Diagnostics); ok {
@@ -651,6 +665,21 @@ func Parse(b hcl.Body, opt Opt, val interface{}) (map[string]map[string][]string
651665
r := p.vars[k].Body.MissingItemRange()
652666
return nil, wrapErrorDiagnostic("Invalid value", err, &r, &r)
653667
}
668+
v := &Variable{
669+
Name: p.vars[k].Name,
670+
Description: p.vars[k].Description,
671+
}
672+
if vv := p.ectx.Variables[k]; !vv.IsNull() {
673+
var s string
674+
switch vv.Type() {
675+
case cty.String:
676+
s = vv.AsString()
677+
case cty.Bool:
678+
s = strconv.FormatBool(vv.True())
679+
}
680+
v.Value = &s
681+
}
682+
vars = append(vars, v)
654683
}
655684

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

798-
return renamed, nil
827+
return &ParseMeta{
828+
Renamed: renamed,
829+
AllVariables: vars,
830+
}, nil
799831
}
800832

801833
// wrapErrorDiagnostic wraps an error into a hcl.Diagnostics object.

0 commit comments

Comments
 (0)