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()
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
} }

View File

@ -49,23 +49,36 @@ func TestNewPushCommandErrors(t *testing.T) {
func TestNewPushCommandSuccess(t *testing.T) { 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 {
cli := test.NewFakeCli(&fakeClient{ tc := tc
imagePushFunc: func(ref string, options types.ImagePushOptions) (io.ReadCloser, error) { t.Run(tc.name, func(t *testing.T) {
return ioutil.NopCloser(strings.NewReader("")), nil 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())
} }
} }

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