Add support for docker push --quiet

Signed-off-by: Justyn Temme <justyntemme@gmail.com>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Justyn Temme 2018-07-18 14:29:56 -05:00 committed by Sebastiaan van Stijn
parent 37f9a88c69
commit 756ab2fb92
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
4 changed files with 34 additions and 14 deletions

View File

@ -2,6 +2,7 @@ package image
import ( import (
"context" "context"
"fmt"
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
@ -14,6 +15,7 @@ import (
type pushOptions struct { type pushOptions struct {
remote string remote string
untrusted bool untrusted bool
quiet bool
} }
// NewPushCommand creates a new `docker push` command // NewPushCommand creates a new `docker push` command
@ -31,7 +33,7 @@ func NewPushCommand(dockerCli command.Cli) *cobra.Command {
} }
flags := cmd.Flags() flags := cmd.Flags()
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Suppress verbose output")
command.AddTrustSigningFlags(flags, &opts.untrusted, dockerCli.ContentTrustEnabled()) command.AddTrustSigningFlags(flags, &opts.untrusted, dockerCli.ContentTrustEnabled())
return cmd return cmd
@ -66,5 +68,9 @@ func RunPush(dockerCli command.Cli, opts pushOptions) error {
} }
defer responseBody.Close() defer responseBody.Close()
if !opts.quiet {
return jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil) return jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil)
} }
fmt.Fprintln(dockerCli.Out(), ref.String())
return nil
}

View File

@ -51,21 +51,34 @@ func TestNewPushCommandSuccess(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
args []string args []string
output string
}{ }{
{ {
name: "simple", name: "push",
args: []string{"image:tag"}, args: []string{"image:tag"},
}, },
{
name: "push quiet",
args: []string{"--quiet", "image:tag"},
output: `docker.io/library/image:tag
`,
},
} }
for _, tc := range testCases { for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
imagePushFunc: func(ref string, options types.ImagePushOptions) (io.ReadCloser, error) { imagePushFunc: func(ref string, options types.ImagePushOptions) (io.ReadCloser, error) {
return ioutil.NopCloser(strings.NewReader("")), nil return ioutil.NopCloser(strings.NewReader("")), nil
}, },
}) })
cmd := NewPushCommand(cli) cmd := NewPushCommand(cli)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(cli.OutBuffer())
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
assert.NilError(t, cmd.Execute()) assert.NilError(t, cmd.Execute())
if tc.output != "" {
assert.Equal(t, tc.output, cli.OutBuffer().String())
}
})
} }
} }

View File

@ -3131,7 +3131,7 @@ _docker_image_pull() {
_docker_image_push() { _docker_image_push() {
case "$cur" in 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) local counter=$(__docker_pos_first_nonflag)

View File

@ -23,6 +23,7 @@ Push an image or a repository to a registry
Options: Options:
--disable-content-trust Skip image signing (default true) --disable-content-trust Skip image signing (default true)
--help Print usage --help Print usage
-q, --quiet Suppress verbose output
``` ```
## Description ## Description