mirror of https://github.com/docker/buildx.git
bake: fix printer handling
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
parent
f28dff7598
commit
64171cb13e
128
commands/bake.go
128
commands/bake.go
|
@ -29,7 +29,6 @@ import (
|
||||||
"github.com/docker/buildx/util/progress"
|
"github.com/docker/buildx/util/progress"
|
||||||
"github.com/docker/buildx/util/tracing"
|
"github.com/docker/buildx/util/tracing"
|
||||||
"github.com/docker/cli/cli/command"
|
"github.com/docker/cli/cli/command"
|
||||||
"github.com/moby/buildkit/client"
|
|
||||||
"github.com/moby/buildkit/identity"
|
"github.com/moby/buildkit/identity"
|
||||||
"github.com/moby/buildkit/util/progress/progressui"
|
"github.com/moby/buildkit/util/progress/progressui"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
@ -146,42 +145,13 @@ func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in ba
|
||||||
printer, err = progress.NewPrinter(ctx2, os.Stderr, progressMode,
|
printer, err = progress.NewPrinter(ctx2, os.Stderr, progressMode,
|
||||||
progress.WithDesc(progressTextDesc, progressConsoleDesc),
|
progress.WithDesc(progressTextDesc, progressConsoleDesc),
|
||||||
progress.WithOnClose(func() {
|
progress.WithOnClose(func() {
|
||||||
if p := printer; p != nil {
|
printWarnings(os.Stderr, printer.Warnings(), progressMode)
|
||||||
printWarnings(os.Stderr, p.Warnings(), progressMode)
|
|
||||||
}
|
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var resp map[string]*client.SolveResponse
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
if printer != nil {
|
|
||||||
err1 := printer.Wait()
|
|
||||||
if err == nil {
|
|
||||||
err = err1
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if progressMode != progressui.QuietMode && progressMode != progressui.RawJSONMode {
|
|
||||||
desktop.PrintBuildDetails(os.Stderr, printer.BuildRefs(), term)
|
|
||||||
}
|
|
||||||
if resp != nil && len(in.metadataFile) > 0 {
|
|
||||||
dt := make(map[string]interface{})
|
|
||||||
for t, r := range resp {
|
|
||||||
dt[t] = decodeExporterResponse(r.ExporterResponse)
|
|
||||||
}
|
|
||||||
if warnings := printer.Warnings(); len(warnings) > 0 && confutil.MetadataWarningsEnabled() {
|
|
||||||
dt["buildx.build.warnings"] = warnings
|
|
||||||
}
|
|
||||||
err = writeMetadataFile(in.metadataFile, dt)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
files, inp, err := readBakeFiles(ctx, nodes, url, in.files, dockerCli.In(), printer)
|
files, inp, err := readBakeFiles(ctx, nodes, url, in.files, dockerCli.In(), printer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -203,10 +173,7 @@ func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in ba
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if err = printer.Wait(); err != nil {
|
||||||
err = printer.Wait()
|
|
||||||
printer = nil
|
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if in.listTargets {
|
if in.listTargets {
|
||||||
|
@ -249,17 +216,15 @@ func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in ba
|
||||||
}
|
}
|
||||||
|
|
||||||
if in.printOnly {
|
if in.printOnly {
|
||||||
dt, err := json.MarshalIndent(def, "", " ")
|
if err = printer.Wait(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
dtdef, err := json.MarshalIndent(def, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = printer.Wait()
|
_, err = fmt.Fprintln(dockerCli.Out(), string(dtdef))
|
||||||
printer = nil
|
return err
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
fmt.Fprintln(dockerCli.Out(), string(dt))
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, opt := range bo {
|
for _, opt := range bo {
|
||||||
|
@ -272,41 +237,32 @@ func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in ba
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
prm := confutil.MetadataProvenance()
|
if err := saveLocalStateGroup(dockerCli, in, targets, bo, overrides, def); err != nil {
|
||||||
if len(in.metadataFile) == 0 {
|
|
||||||
prm = confutil.MetadataProvenanceModeDisabled
|
|
||||||
}
|
|
||||||
|
|
||||||
groupRef := identity.NewID()
|
|
||||||
var refs []string
|
|
||||||
for k, b := range bo {
|
|
||||||
b.Ref = identity.NewID()
|
|
||||||
b.GroupRef = groupRef
|
|
||||||
b.ProvenanceResponseMode = prm
|
|
||||||
refs = append(refs, b.Ref)
|
|
||||||
bo[k] = b
|
|
||||||
}
|
|
||||||
dt, err := json.Marshal(def)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := saveLocalStateGroup(dockerCli, groupRef, localstate.StateGroup{
|
|
||||||
Definition: dt,
|
|
||||||
Targets: targets,
|
|
||||||
Inputs: overrides,
|
|
||||||
Refs: refs,
|
|
||||||
}); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err = build.Build(ctx, nodes, bo, dockerutil.NewClient(dockerCli), confutil.ConfigDir(dockerCli), printer)
|
resp, retErr := build.Build(ctx, nodes, bo, dockerutil.NewClient(dockerCli), confutil.ConfigDir(dockerCli), printer)
|
||||||
if err != nil {
|
if err := printer.Wait(); retErr == nil {
|
||||||
return wrapBuildError(err, true)
|
retErr = err
|
||||||
|
}
|
||||||
|
if retErr != nil {
|
||||||
|
return wrapBuildError(retErr, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = printer.Wait()
|
if progressMode != progressui.QuietMode && progressMode != progressui.RawJSONMode {
|
||||||
if err != nil {
|
desktop.PrintBuildDetails(os.Stderr, printer.BuildRefs(), term)
|
||||||
return err
|
}
|
||||||
|
if callFunc == nil && len(in.metadataFile) > 0 {
|
||||||
|
dt := make(map[string]interface{})
|
||||||
|
for t, r := range resp {
|
||||||
|
dt[t] = decodeExporterResponse(r.ExporterResponse)
|
||||||
|
}
|
||||||
|
if warnings := printer.Warnings(); len(warnings) > 0 && confutil.MetadataWarningsEnabled() {
|
||||||
|
dt["buildx.build.warnings"] = warnings
|
||||||
|
}
|
||||||
|
if err := writeMetadataFile(in.metadataFile, dt); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var callFormatJSON bool
|
var callFormatJSON bool
|
||||||
|
@ -468,12 +424,34 @@ func bakeCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveLocalStateGroup(dockerCli command.Cli, ref string, lsg localstate.StateGroup) error {
|
func saveLocalStateGroup(dockerCli command.Cli, in bakeOptions, targets []string, bo map[string]build.Options, overrides []string, def any) error {
|
||||||
|
prm := confutil.MetadataProvenance()
|
||||||
|
if len(in.metadataFile) == 0 {
|
||||||
|
prm = confutil.MetadataProvenanceModeDisabled
|
||||||
|
}
|
||||||
|
groupRef := identity.NewID()
|
||||||
|
refs := make([]string, 0, len(bo))
|
||||||
|
for k, b := range bo {
|
||||||
|
b.Ref = identity.NewID()
|
||||||
|
b.GroupRef = groupRef
|
||||||
|
b.ProvenanceResponseMode = prm
|
||||||
|
refs = append(refs, b.Ref)
|
||||||
|
bo[k] = b
|
||||||
|
}
|
||||||
l, err := localstate.New(confutil.ConfigDir(dockerCli))
|
l, err := localstate.New(confutil.ConfigDir(dockerCli))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return l.SaveGroup(ref, lsg)
|
dtdef, err := json.MarshalIndent(def, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return l.SaveGroup(groupRef, localstate.StateGroup{
|
||||||
|
Definition: dtdef,
|
||||||
|
Targets: targets,
|
||||||
|
Inputs: overrides,
|
||||||
|
Refs: refs,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func readBakeFiles(ctx context.Context, nodes []builder.Node, url string, names []string, stdin io.Reader, pw progress.Writer) (files []bake.File, inp *bake.Input, err error) {
|
func readBakeFiles(ctx context.Context, nodes []builder.Node, url string, names []string, stdin io.Reader, pw progress.Writer) (files []bake.File, inp *bake.Input, err error) {
|
||||||
|
|
Loading…
Reference in New Issue