mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-05-19 01:47:43 +08:00
bake: add list-targets options to list available targets/groups
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
parent
8f4c8b094a
commit
7460f049f2
@ -670,6 +670,7 @@ func (c Config) target(name string, visited map[string]*Target, overrides map[st
|
|||||||
|
|
||||||
type Group struct {
|
type Group struct {
|
||||||
Name string `json:"-" hcl:"name,label" cty:"name"`
|
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"`
|
Targets []string `json:"targets" hcl:"targets" cty:"targets"`
|
||||||
// Target // TODO?
|
// Target // TODO?
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package commands
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"cmp"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -9,6 +10,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
"text/tabwriter"
|
||||||
|
|
||||||
"github.com/containerd/console"
|
"github.com/containerd/console"
|
||||||
"github.com/containerd/platforms"
|
"github.com/containerd/platforms"
|
||||||
@ -36,6 +38,7 @@ type bakeOptions struct {
|
|||||||
files []string
|
files []string
|
||||||
overrides []string
|
overrides []string
|
||||||
printOnly bool
|
printOnly bool
|
||||||
|
listTargets bool
|
||||||
sbom string
|
sbom string
|
||||||
provenance string
|
provenance string
|
||||||
|
|
||||||
@ -183,12 +186,28 @@ func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in ba
|
|||||||
return errors.New("couldn't find a bake definition")
|
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
|
// don't forget to update documentation if you add a new
|
||||||
// built-in variable: docs/bake-reference.md#built-in-variables
|
// built-in variable: docs/bake-reference.md#built-in-variables
|
||||||
"BAKE_CMD_CONTEXT": cmdContext,
|
"BAKE_CMD_CONTEXT": cmdContext,
|
||||||
"BAKE_LOCAL_PLATFORM": platforms.Format(platforms.DefaultSpec()),
|
"BAKE_LOCAL_PLATFORM": platforms.Format(platforms.DefaultSpec()),
|
||||||
})
|
}
|
||||||
|
|
||||||
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -419,6 +438,7 @@ func bakeCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
|
|||||||
flags.StringArrayVarP(&options.files, "file", "f", []string{}, "Build definition file")
|
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.exportLoad, "load", false, `Shorthand for "--set=*.output=type=docker"`)
|
||||||
flags.BoolVar(&options.printOnly, "print", false, "Print the options without building")
|
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.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.sbom, "sbom", "", `Shorthand for "--set=*.attest=type=sbom"`)
|
||||||
flags.StringVar(&options.provenance, "provenance", "", `Shorthand for "--set=*.attest=type=provenance"`)
|
flags.StringVar(&options.provenance, "provenance", "", `Shorthand for "--set=*.attest=type=provenance"`)
|
||||||
@ -482,3 +502,54 @@ func readBakeFiles(ctx context.Context, nodes []builder.Node, url string, names
|
|||||||
|
|
||||||
return
|
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
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user