Skip to content

Commit

Permalink
bake: add list-targets options to list available targets/groups
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <[email protected]>
  • Loading branch information
tonistiigi committed Jun 28, 2024
1 parent 0f0f11f commit 892b131
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 9 deletions.
5 changes: 3 additions & 2 deletions bake/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,8 +669,9 @@ func (c Config) target(name string, visited map[string]*Target, overrides map[st
}

type Group struct {
Name string `json:"-" hcl:"name,label" cty:"name"`
Targets []string `json:"targets" hcl:"targets" cty:"targets"`
Name string `json:"-" hcl:"name,label" cty:"name"`
Description string `json:"description,omitempty" hcl:"description,optional" cty:"description"`
Targets []string `json:"targets" hcl:"targets" cty:"targets"`
// Target // TODO?
}

Expand Down
86 changes: 79 additions & 7 deletions commands/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package commands

import (
"bytes"
"cmp"
"context"
"encoding/json"
"fmt"
"io"
"os"
"slices"
"strings"
"text/tabwriter"

"github.com/containerd/console"
"github.com/containerd/containerd/platforms"
Expand All @@ -32,11 +35,12 @@ import (
)

type bakeOptions struct {
files []string
overrides []string
printOnly bool
sbom string
provenance string
files []string
overrides []string
printOnly bool
listTargets bool
sbom string
provenance string

builder string
metadataFile string
Expand Down Expand Up @@ -178,12 +182,28 @@ func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in ba
return errors.New("couldn't find a bake definition")
}

tgts, grps, err := bake.ReadTargets(ctx, files, targets, overrides, map[string]string{
defaults := map[string]string{
// don't forget to update documentation if you add a new
// built-in variable: docs/bake-reference.md#built-in-variables
"BAKE_CMD_CONTEXT": cmdContext,
"BAKE_LOCAL_PLATFORM": platforms.DefaultString(),
})
}

if in.listTargets {
cfg, err := bake.ParseFiles(files, defaults)
if err != nil {
return err
}

err = printer.Wait()
printer = nil
if err != nil {
return err
}
return printTargetList(dockerCli.Out(), cfg)
}

tgts, grps, err := bake.ReadTargets(ctx, files, targets, overrides, defaults)
if err != nil {
return err
}
Expand Down Expand Up @@ -414,6 +434,7 @@ func bakeCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
flags.StringArrayVarP(&options.files, "file", "f", []string{}, "Build definition file")
flags.BoolVar(&options.exportLoad, "load", false, `Shorthand for "--set=*.output=type=docker"`)
flags.BoolVar(&options.printOnly, "print", false, "Print the options without building")
flags.BoolVar(&options.listTargets, "list-targets", false, "List available targets")
flags.BoolVar(&options.exportPush, "push", false, `Shorthand for "--set=*.output=type=registry"`)
flags.StringVar(&options.sbom, "sbom", "", `Shorthand for "--set=*.attest=type=sbom"`)
flags.StringVar(&options.provenance, "provenance", "", `Shorthand for "--set=*.attest=type=provenance"`)
Expand Down Expand Up @@ -477,3 +498,54 @@ func readBakeFiles(ctx context.Context, nodes []builder.Node, url string, names

return
}

func printTargetList(w io.Writer, cfg *bake.Config) error {
tw := tabwriter.NewWriter(w, 1, 8, 1, '\t', 0)
defer tw.Flush()

tw.Write([]byte("TARGET\tDESCRIPTION\n"))

type targetOrGroup struct {
name string
target *bake.Target
group *bake.Group
}

list := make([]targetOrGroup, 0, len(cfg.Targets)+len(cfg.Groups))
for _, tgt := range cfg.Targets {
list = append(list, targetOrGroup{name: tgt.Name, target: tgt})
}
for _, grp := range cfg.Groups {
list = append(list, targetOrGroup{name: grp.Name, group: grp})
}

slices.SortFunc(list, func(a, b targetOrGroup) int {
return cmp.Compare(a.name, b.name)
})

for _, tgt := range list {
if strings.HasPrefix(tgt.name, "_") {
// convention for a private target
continue
}
var descr string
if tgt.target != nil {
descr = tgt.target.Description
} else if tgt.group != nil {
descr = tgt.group.Description

if len(tgt.group.Targets) > 0 {
slices.Sort(tgt.group.Targets)
names := strings.Join(tgt.group.Targets, ", ")
if descr != "" {
descr += " (" + names + ")"
} else {
descr = names
}
}
}
fmt.Fprintf(tw, "%s\t%s\n", tgt.name, descr)
}

return nil
}

0 comments on commit 892b131

Please sign in to comment.