From 523dd96fe58ab33cebe28e5307e732236ebc25e6 Mon Sep 17 00:00:00 2001 From: Rodrigo Arguello Date: Fri, 15 Mar 2024 14:28:58 +0100 Subject: [PATCH] add an option to use a custom progress printer for the build progress output Signed-off-by: Rodrigo Arguello --- pkg/api/api.go | 3 +++ pkg/compose/build.go | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/pkg/api/api.go b/pkg/api/api.go index 53fce924e8..641840a3e5 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -19,6 +19,7 @@ package api import ( "context" "fmt" + "io" "strings" "time" @@ -147,6 +148,8 @@ type BuildOptions struct { Memory int64 // Builder name passed in the command line Builder string + // OutPrinter used for printing the progress output + OutPrinter io.Writer } // Apply mutates project according to build options diff --git a/pkg/compose/build.go b/pkg/compose/build.go index 8dacba1ba4..a6ce19a12d 100644 --- a/pkg/compose/build.go +++ b/pkg/compose/build.go @@ -20,12 +20,14 @@ import ( "context" "errors" "fmt" + "io" "os" "path/filepath" "github.com/moby/buildkit/util/progress/progressui" "github.com/compose-spec/compose-go/v2/types" + "github.com/containerd/console" "github.com/containerd/containerd/platforms" "github.com/docker/buildx/build" "github.com/docker/buildx/builder" @@ -127,10 +129,15 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti progressCtx, cancel := context.WithCancel(context.Background()) defer cancel() + var outPrinter io.Writer = os.Stdout + if options.OutPrinter != nil { + outPrinter = options.OutPrinter + } + if options.Quiet { options.Progress = progress.ModeQuiet } - w, err = xprogress.NewPrinter(progressCtx, os.Stdout, progressui.DisplayMode(options.Progress), + w, err = xprogress.NewPrinter(progressCtx, progressPrinter(outPrinter), progressui.DisplayMode(options.Progress), xprogress.WithDesc( fmt.Sprintf("building with %q instance using %s driver", b.Name, b.Driver), fmt.Sprintf("%s:%s", b.Driver, b.Name), @@ -567,3 +574,29 @@ func parsePlatforms(service types.ServiceConfig) ([]specs.Platform, error) { return ret, nil } + +type pPrinter struct { + io.Writer +} + +func (p *pPrinter) Read(_ []byte) (n int, err error) { + return 0, errors.New("not implemented") +} + +func (p *pPrinter) Close() error { + return nil +} + +func (p *pPrinter) Fd() uintptr { + return 0 +} + +func (p *pPrinter) Name() string { + return "pPrinter" +} + +func progressPrinter(w io.Writer) console.File { + return &pPrinter{ + Writer: w, + } +}