From 756ab2fb92998047d072a81ef432851411819030 Mon Sep 17 00:00:00 2001 From: Justyn Temme Date: Wed, 18 Jul 2018 14:29:56 -0500 Subject: [PATCH] Add support for docker push --quiet Signed-off-by: Justyn Temme Signed-off-by: Sebastiaan van Stijn --- cli/command/image/push.go | 10 +++++++-- cli/command/image/push_test.go | 35 ++++++++++++++++++++---------- contrib/completion/bash/docker | 2 +- docs/reference/commandline/push.md | 1 + 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/cli/command/image/push.go b/cli/command/image/push.go index de6c2ec34e..1bcce816dc 100644 --- a/cli/command/image/push.go +++ b/cli/command/image/push.go @@ -2,6 +2,7 @@ package image import ( "context" + "fmt" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" @@ -14,6 +15,7 @@ import ( type pushOptions struct { remote string untrusted bool + quiet bool } // NewPushCommand creates a new `docker push` command @@ -31,7 +33,7 @@ func NewPushCommand(dockerCli command.Cli) *cobra.Command { } flags := cmd.Flags() - + flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Suppress verbose output") command.AddTrustSigningFlags(flags, &opts.untrusted, dockerCli.ContentTrustEnabled()) return cmd @@ -66,5 +68,9 @@ func RunPush(dockerCli command.Cli, opts pushOptions) error { } defer responseBody.Close() - return jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil) + if !opts.quiet { + return jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil) + } + fmt.Fprintln(dockerCli.Out(), ref.String()) + return nil } diff --git a/cli/command/image/push_test.go b/cli/command/image/push_test.go index 75798aaaf1..f552de2f95 100644 --- a/cli/command/image/push_test.go +++ b/cli/command/image/push_test.go @@ -49,23 +49,36 @@ func TestNewPushCommandErrors(t *testing.T) { func TestNewPushCommandSuccess(t *testing.T) { testCases := []struct { - name string - args []string + name string + args []string + output string }{ { - name: "simple", + name: "push", args: []string{"image:tag"}, }, + { + name: "push quiet", + args: []string{"--quiet", "image:tag"}, + output: `docker.io/library/image:tag +`, + }, } for _, tc := range testCases { - cli := test.NewFakeCli(&fakeClient{ - imagePushFunc: func(ref string, options types.ImagePushOptions) (io.ReadCloser, error) { - return ioutil.NopCloser(strings.NewReader("")), nil - }, + tc := tc + t.Run(tc.name, func(t *testing.T) { + cli := test.NewFakeCli(&fakeClient{ + imagePushFunc: func(ref string, options types.ImagePushOptions) (io.ReadCloser, error) { + return ioutil.NopCloser(strings.NewReader("")), nil + }, + }) + cmd := NewPushCommand(cli) + cmd.SetOutput(cli.OutBuffer()) + cmd.SetArgs(tc.args) + assert.NilError(t, cmd.Execute()) + if tc.output != "" { + assert.Equal(t, tc.output, cli.OutBuffer().String()) + } }) - cmd := NewPushCommand(cli) - cmd.SetOutput(ioutil.Discard) - cmd.SetArgs(tc.args) - assert.NilError(t, cmd.Execute()) } } diff --git a/contrib/completion/bash/docker b/contrib/completion/bash/docker index bc3f5d088f..a1f8246a2c 100644 --- a/contrib/completion/bash/docker +++ b/contrib/completion/bash/docker @@ -3131,7 +3131,7 @@ _docker_image_pull() { _docker_image_push() { case "$cur" in -*) - COMPREPLY=( $( compgen -W "--disable-content-trust=false --help" -- "$cur" ) ) + COMPREPLY=( $( compgen -W "--disable-content-trust=false --help --quiet -q" -- "$cur" ) ) ;; *) local counter=$(__docker_pos_first_nonflag) diff --git a/docs/reference/commandline/push.md b/docs/reference/commandline/push.md index 36ff319651..336ff5f6d5 100644 --- a/docs/reference/commandline/push.md +++ b/docs/reference/commandline/push.md @@ -23,6 +23,7 @@ Push an image or a repository to a registry Options: --disable-content-trust Skip image signing (default true) --help Print usage + -q, --quiet Suppress verbose output ``` ## Description