From c19c018a4ca0c321f376d789394de1b1193c0589 Mon Sep 17 00:00:00 2001 From: Andy Caldwell Date: Thu, 19 Aug 2021 04:05:15 +0100 Subject: [PATCH 1/3] Implement `--quiet` support Signed-off-by: Andy Caldwell --- Makefile | 4 ++-- build/build.go | 10 +++++++++- commands/build.go | 19 +++++++++++-------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 9c67a1a9..4d03a8de 100644 --- a/Makefile +++ b/Makefile @@ -22,10 +22,10 @@ test: validate-vendor: ./hack/validate-vendor - + validate-docs: ./hack/validate-docs - + validate-all: lint test validate-vendor validate-docs vendor: diff --git a/build/build.go b/build/build.go index fa85ccf4..bce81610 100644 --- a/build/build.go +++ b/build/build.go @@ -54,6 +54,7 @@ type Options struct { BuildArgs map[string]string Pull bool ImageIDFile string + Quiet bool ExtraHosts []string NetworkMode string @@ -683,8 +684,12 @@ func Build(ctx context.Context, drivers []DriverInfo, opt map[string]Options, do resp[k] = res[0] respMu.Unlock() if len(res) == 1 { + digest := res[0].ExporterResponse["containerimage.digest"] + if opt.Quiet { + fmt.Println(digest) + } if opt.ImageIDFile != "" { - return ioutil.WriteFile(opt.ImageIDFile, []byte(res[0].ExporterResponse["containerimage.digest"]), 0644) + return ioutil.WriteFile(opt.ImageIDFile, []byte(digest), 0644) } return nil } @@ -713,6 +718,9 @@ func Build(ctx context.Context, drivers []DriverInfo, opt map[string]Options, do if err != nil { return err } + if opt.Quiet { + fmt.Println(desc.Digest) + } if opt.ImageIDFile != "" { return ioutil.WriteFile(opt.ImageIDFile, []byte(desc.Digest), 0644) } diff --git a/commands/build.go b/commands/build.go index ada866e9..ab5ecba7 100644 --- a/commands/build.go +++ b/commands/build.go @@ -19,7 +19,6 @@ import ( "github.com/moby/buildkit/session/auth/authprovider" "github.com/moby/buildkit/util/appcontext" "github.com/pkg/errors" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/pflag" ) @@ -44,10 +43,10 @@ type buildOptions struct { imageIDFile string extraHosts []string networkMode string + quiet bool // unimplemented squash bool - quiet bool allow []string @@ -85,10 +84,6 @@ func runBuild(dockerCli command.Cli, in buildOptions) (err error) { if in.squash { return errors.Errorf("squash currently not implemented") } - if in.quiet { - logrus.Warnf("quiet currently not implemented") - } - ctx := appcontext.Context() ctx, end, err := tracing.TraceCurrentCommand(ctx, "build") @@ -108,6 +103,12 @@ func runBuild(dockerCli command.Cli, in buildOptions) (err error) { pull = *in.pull } + if in.quiet && in.progress != "auto" && in.progress != "quiet" { + return errors.Errorf("progress=%s and quiet cannot be used together", in.progress) + } else if in.quiet { + in.progress = "quiet" + } + opts := build.Options{ Inputs: build.Inputs{ ContextPath: in.contextPath, @@ -121,6 +122,7 @@ func runBuild(dockerCli command.Cli, in buildOptions) (err error) { NoCache: noCache, Target: in.target, ImageIDFile: in.imageIDFile, + Quiet: in.quiet, ExtraHosts: in.extraHosts, NetworkMode: in.networkMode, } @@ -225,6 +227,7 @@ func buildTargets(ctx context.Context, dockerCli command.Cli, opts map[string]bu ctx2, cancel := context.WithCancel(context.TODO()) defer cancel() + printer := progress.NewPrinter(ctx2, os.Stderr, progressMode) resp, err := build.Build(ctx, dis, opts, dockerAPI(dockerCli), dockerCli.ConfigFile(), printer) @@ -287,14 +290,14 @@ func buildCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command { flags.StringSliceVar(&options.allow, "allow", []string{}, "Allow extra privileged entitlement, e.g. network.host, security.insecure") - // not implemented flags.BoolVarP(&options.quiet, "quiet", "q", false, "Suppress the build output and print image ID on success") flags.StringVar(&options.networkMode, "network", "default", "Set the networking mode for the RUN instructions during build") flags.StringSliceVar(&options.extraHosts, "add-host", []string{}, "Add a custom host-to-IP mapping (host:ip)") flags.SetAnnotation("add-host", "docs.external.url", []string{"https://docs.docker.com/engine/reference/commandline/build/#add-entries-to-container-hosts-file---add-host"}) flags.StringVar(&options.imageIDFile, "iidfile", "", "Write the image ID to the file") + + // not implemented flags.BoolVar(&options.squash, "squash", false, "Squash newly built layers into a single new layer") - flags.MarkHidden("quiet") flags.MarkHidden("squash") // hidden flags From 3ab0b6953af991fc2d2ed55831600d3b92f9267c Mon Sep 17 00:00:00 2001 From: Andy Caldwell Date: Fri, 20 Aug 2021 00:03:46 +0100 Subject: [PATCH 2/3] Regenerate docs now that --quiet is unmasked Signed-off-by: Andy Caldwell --- docs/reference/buildx_build.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/reference/buildx_build.md b/docs/reference/buildx_build.md index b457f950..35457722 100644 --- a/docs/reference/buildx_build.md +++ b/docs/reference/buildx_build.md @@ -33,6 +33,7 @@ Start a build | [`--progress string`](#progress) | Set type of progress output (auto, plain, tty). Use plain to show container output | | `--pull` | Always attempt to pull a newer version of the image | | [`--push`](#push) | Shorthand for --output=type=registry | +| `-q`, `--quiet` | Suppress the build output and print image ID on success | | `--secret stringArray` | Secret file to expose to the build: id=mysecret,src=/local/secret | | `--ssh stringArray` | SSH agent socket or keys to expose to the build (format: default|[=|[,]]) | | [`-t`](https://docs.docker.com/engine/reference/commandline/build/#tag-an-image--t), [`--tag stringArray`](https://docs.docker.com/engine/reference/commandline/build/#tag-an-image--t) | Name and optionally a tag in the 'name:tag' format | From ef3e46fd62135592933c2a4bcc044274c03713ac Mon Sep 17 00:00:00 2001 From: Andy Caldwell Date: Fri, 20 Aug 2021 15:09:56 +0100 Subject: [PATCH 3/3] Move printing to stdout up to the command itself Signed-off-by: Andy Caldwell --- build/build.go | 7 ------- commands/build.go | 24 ++++++++++++++++-------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/build/build.go b/build/build.go index bce81610..e7eacd07 100644 --- a/build/build.go +++ b/build/build.go @@ -54,7 +54,6 @@ type Options struct { BuildArgs map[string]string Pull bool ImageIDFile string - Quiet bool ExtraHosts []string NetworkMode string @@ -685,9 +684,6 @@ func Build(ctx context.Context, drivers []DriverInfo, opt map[string]Options, do respMu.Unlock() if len(res) == 1 { digest := res[0].ExporterResponse["containerimage.digest"] - if opt.Quiet { - fmt.Println(digest) - } if opt.ImageIDFile != "" { return ioutil.WriteFile(opt.ImageIDFile, []byte(digest), 0644) } @@ -718,9 +714,6 @@ func Build(ctx context.Context, drivers []DriverInfo, opt map[string]Options, do if err != nil { return err } - if opt.Quiet { - fmt.Println(desc.Digest) - } if opt.ImageIDFile != "" { return ioutil.WriteFile(opt.ImageIDFile, []byte(desc.Digest), 0644) } diff --git a/commands/build.go b/commands/build.go index ab5ecba7..19befaac 100644 --- a/commands/build.go +++ b/commands/build.go @@ -3,6 +3,7 @@ package commands import ( "context" "encoding/json" + "fmt" "os" "path/filepath" "strings" @@ -122,7 +123,6 @@ func runBuild(dockerCli command.Cli, in buildOptions) (err error) { NoCache: noCache, Target: in.target, ImageIDFile: in.imageIDFile, - Quiet: in.quiet, ExtraHosts: in.extraHosts, NetworkMode: in.networkMode, } @@ -216,13 +216,21 @@ func runBuild(dockerCli command.Cli, in buildOptions) (err error) { contextPathHash = in.contextPath } - return buildTargets(ctx, dockerCli, map[string]build.Options{defaultTargetName: opts}, in.progress, contextPathHash, in.builder, in.metadataFile) + imageID, err := buildTargets(ctx, dockerCli, map[string]build.Options{defaultTargetName: opts}, in.progress, contextPathHash, in.builder, in.metadataFile) + if err != nil { + return err + } + + if in.quiet { + fmt.Println(imageID) + } + return nil } -func buildTargets(ctx context.Context, dockerCli command.Cli, opts map[string]build.Options, progressMode, contextPathHash, instance string, metadataFile string) error { +func buildTargets(ctx context.Context, dockerCli command.Cli, opts map[string]build.Options, progressMode, contextPathHash, instance string, metadataFile string) (imageID string, err error) { dis, err := getInstanceOrDefault(ctx, dockerCli, instance, contextPathHash) if err != nil { - return err + return "", err } ctx2, cancel := context.WithCancel(context.TODO()) @@ -236,20 +244,20 @@ func buildTargets(ctx context.Context, dockerCli command.Cli, opts map[string]bu err = err1 } if err != nil { - return err + return "", err } if len(metadataFile) > 0 && resp != nil { mdatab, err := json.MarshalIndent(resp[defaultTargetName].ExporterResponse, "", " ") if err != nil { - return err + return "", err } if err := ioutils.AtomicWriteFile(metadataFile, mdatab, 0644); err != nil { - return err + return "", err } } - return err + return resp[defaultTargetName].ExporterResponse["containerimage.digest"], err } func buildCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {