From 77d602ea07ab786446fd1014fca20e84cbbdebda Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Thu, 30 Sep 2021 17:21:10 +0900 Subject: [PATCH] build: support --quiet Signed-off-by: Akihiro Suda --- cmd/nerdctl/build.go | 14 ++++++++++++-- cmd/nerdctl/load.go | 14 ++++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/cmd/nerdctl/build.go b/cmd/nerdctl/build.go index 11b9651b7a0..a0afd0a0482 100644 --- a/cmd/nerdctl/build.go +++ b/cmd/nerdctl/build.go @@ -84,6 +84,11 @@ var buildCommand = &cli.Command{ Name: "ssh", Usage: "SSH agent socket or keys to expose to the build (format: default|[=|[,]])", }, + &cli.BoolFlag{ + Name: "quiet", + Aliases: []string{"q"}, + Usage: "Suppress the build output and print image ID on success", + }, }, } @@ -98,6 +103,8 @@ func buildAction(clicontext *cli.Context) error { return err } + quiet := clicontext.Bool("quiet") + logrus.Debugf("running %s %v", buildctlBinary, buildctlArgs) buildctlCmd := exec.Command(buildctlBinary, buildctlArgs...) buildctlCmd.Env = os.Environ() @@ -111,14 +118,17 @@ func buildAction(clicontext *cli.Context) error { } else { buildctlCmd.Stdout = clicontext.App.Writer } - buildctlCmd.Stderr = clicontext.App.ErrWriter + + if !quiet { + buildctlCmd.Stderr = clicontext.App.ErrWriter + } if err := buildctlCmd.Start(); err != nil { return err } if needsLoading { - if err = loadImage(buildctlStdout, clicontext); err != nil { + if err = loadImage(buildctlStdout, clicontext, quiet); err != nil { return err } } diff --git a/cmd/nerdctl/load.go b/cmd/nerdctl/load.go index 3ca4c1998dc..dbe0d37af1d 100644 --- a/cmd/nerdctl/load.go +++ b/cmd/nerdctl/load.go @@ -55,10 +55,10 @@ func loadAction(clicontext *cli.Context) error { defer f.Close() in = f } - return loadImage(in, clicontext) + return loadImage(in, clicontext, false) } -func loadImage(in io.Reader, clicontext *cli.Context) error { +func loadImage(in io.Reader, clicontext *cli.Context, quiet bool) error { client, ctx, cancel, err := newClient(clicontext, containerd.WithDefaultPlatform(platforms.DefaultStrict())) if err != nil { return err @@ -74,12 +74,18 @@ func loadImage(in io.Reader, clicontext *cli.Context) error { image := containerd.NewImage(client, img) // TODO: Show unpack status - fmt.Fprintf(clicontext.App.Writer, "unpacking %s (%s)...", img.Name, img.Target.Digest) + if !quiet { + fmt.Fprintf(clicontext.App.Writer, "unpacking %s (%s)...", img.Name, img.Target.Digest) + } err = image.Unpack(ctx, sn) if err != nil { return err } - fmt.Fprintf(clicontext.App.Writer, "done\n") + if quiet { + fmt.Fprintln(clicontext.App.Writer, img.Target.Digest) + } else { + fmt.Fprintf(clicontext.App.Writer, "done\n") + } } return nil