Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Carry #553 : Add option to pull images quietly #882

Merged
merged 1 commit into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions cli/command/image/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type PullOptions struct {
remote string
all bool
platform string
quiet bool
untrusted bool
}

Expand All @@ -38,6 +39,7 @@ func NewPullCommand(dockerCli command.Cli) *cobra.Command {
flags := cmd.Flags()

flags.BoolVarP(&opts.all, "all-tags", "a", false, "Download all tagged images in the repository")
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Suppress verbose output")

command.AddPlatformFlag(flags, &opts.platform)
command.AddTrustVerificationFlags(flags, &opts.untrusted, dockerCli.ContentTrustEnabled())
Expand All @@ -55,7 +57,7 @@ func RunPull(cli command.Cli, opts PullOptions) error {
return errors.New("tag can't be used with --all-tags/-a")
case !opts.all && reference.IsNameOnly(distributionRef):
distributionRef = reference.TagNameOnly(distributionRef)
if tagged, ok := distributionRef.(reference.Tagged); ok {
if tagged, ok := distributionRef.(reference.Tagged); ok && !opts.quiet {
fmt.Fprintf(cli.Out(), "Using default tag: %s\n", tagged.Tag())
}
}
Expand All @@ -69,15 +71,16 @@ func RunPull(cli command.Cli, opts PullOptions) error {
// Check if reference has a digest
_, isCanonical := distributionRef.(reference.Canonical)
if !opts.untrusted && !isCanonical {
err = trustedPull(ctx, cli, imgRefAndAuth, opts.platform)
err = trustedPull(ctx, cli, imgRefAndAuth, opts)
} else {
err = imagePullPrivileged(ctx, cli, imgRefAndAuth, opts.all, opts.platform)
err = imagePullPrivileged(ctx, cli, imgRefAndAuth, opts)
}
if err != nil {
if strings.Contains(err.Error(), "when fetching 'plugin'") {
return errors.New(err.Error() + " - Use `docker plugin install`")
}
return err
}
fmt.Fprintln(cli.Out(), imgRefAndAuth.Reference().String())
return nil
}
9 changes: 9 additions & 0 deletions cli/command/image/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func TestNewPullCommandSuccess(t *testing.T) {
testCases := []struct {
name string
args []string
flags map[string]string
expectedTag string
}{
{
Expand All @@ -62,6 +63,14 @@ func TestNewPullCommandSuccess(t *testing.T) {
args: []string{"image"},
expectedTag: "image:latest",
},
{
name: "simple-quiet",
args: []string{"image"},
flags: map[string]string{
"quiet": "true",
},
expectedTag: "image:latest",
},
}
for _, tc := range testCases {
cli := test.NewFakeCli(&fakeClient{
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Using default tag: latest
docker.io/library/image:latest
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Using default tag: latest
thaJeztah marked this conversation as resolved.
Show resolved Hide resolved
docker.io/library/image:latest
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker.io/library/image:tag
22 changes: 16 additions & 6 deletions cli/command/image/trust.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"sort"

"github.com/docker/cli/cli/command"
Expand Down Expand Up @@ -180,7 +181,7 @@ func imagePushPrivileged(ctx context.Context, cli command.Cli, authConfig types.
}

// trustedPull handles content trust pulling of an image
func trustedPull(ctx context.Context, cli command.Cli, imgRefAndAuth trust.ImageRefAndAuth, platform string) error {
func trustedPull(ctx context.Context, cli command.Cli, imgRefAndAuth trust.ImageRefAndAuth, opts PullOptions) error {
refs, err := getTrustedPullTargets(cli, imgRefAndAuth)
if err != nil {
return err
Expand All @@ -202,7 +203,12 @@ func trustedPull(ctx context.Context, cli command.Cli, imgRefAndAuth trust.Image
if err != nil {
return err
}
if err := imagePullPrivileged(ctx, cli, updatedImgRefAndAuth, false, platform); err != nil {
if err := imagePullPrivileged(ctx, cli, updatedImgRefAndAuth, PullOptions{
all: false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for this PR, but wondering if we need a validation step for --all somewhere (as it's being ignored here); i.e. is there a situation where I could've provided the --all flag, and no error is printed (but it's being ignored)

platform: opts.platform,
quiet: opts.quiet,
remote: opts.remote,
}); err != nil {
return err
}

Expand Down Expand Up @@ -268,7 +274,7 @@ func getTrustedPullTargets(cli command.Cli, imgRefAndAuth trust.ImageRefAndAuth)
}

// imagePullPrivileged pulls the image and displays it to the output
func imagePullPrivileged(ctx context.Context, cli command.Cli, imgRefAndAuth trust.ImageRefAndAuth, all bool, platform string) error {
func imagePullPrivileged(ctx context.Context, cli command.Cli, imgRefAndAuth trust.ImageRefAndAuth, opts PullOptions) error {
ref := reference.FamiliarString(imgRefAndAuth.Reference())

encodedAuth, err := command.EncodeAuthToBase64(*imgRefAndAuth.AuthConfig())
Expand All @@ -279,16 +285,20 @@ func imagePullPrivileged(ctx context.Context, cli command.Cli, imgRefAndAuth tru
options := types.ImagePullOptions{
RegistryAuth: encodedAuth,
PrivilegeFunc: requestPrivilege,
All: all,
Platform: platform,
All: opts.all,
Platform: opts.platform,
}
responseBody, err := cli.Client().ImagePull(ctx, ref, options)
if err != nil {
return err
}
defer responseBody.Close()

return jsonmessage.DisplayJSONMessagesToStream(responseBody, cli.Out(), nil)
out := cli.Out()
if opts.quiet {
out = command.NewOutStream(ioutil.Discard)
}
return jsonmessage.DisplayJSONMessagesToStream(responseBody, out, nil)
}

// TrustedReference returns the canonical trusted reference for an image reference
Expand Down
1 change: 1 addition & 0 deletions docs/reference/commandline/pull.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
--help Print usage
-q, --quiet Suppress verbose output
```

## Description
Expand Down
9 changes: 9 additions & 0 deletions e2e/image/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

"github.com/docker/cli/e2e/internal/fixtures"
"github.com/docker/cli/internal/test/environment"
"gotest.tools/assert"
is "gotest.tools/assert/cmp"
"gotest.tools/golden"
"gotest.tools/icmd"
"gotest.tools/skip"
Expand Down Expand Up @@ -32,6 +34,13 @@ func TestPullWithContentTrust(t *testing.T) {
golden.Assert(t, result.Stdout(), "pull-with-content-trust.golden")
}

func TestPullQuiet(t *testing.T) {
result := icmd.RunCommand("docker", "pull", "--quiet", fixtures.AlpineImage)
result.Assert(t, icmd.Success)
assert.Check(t, is.Equal(result.Stdout(), "registry:5000/alpine:3.6\n"))
assert.Check(t, is.Equal(result.Stderr(), ""))
}

func TestPullWithContentTrustUsesCacheWhenNotaryUnavailable(t *testing.T) {
skip.If(t, environment.RemoteDaemon())

Expand Down
1 change: 1 addition & 0 deletions e2e/image/testdata/pull-with-content-trust.golden
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ Pull (1 of 1): registry:5000/trust-pull:latest@sha256:641b95ddb2ea9dc2af1a0113b6
sha256:641b95ddb2ea9dc2af1a0113b6b348ebc20872ba615204fbe12148e98fd6f23d: Pulling from trust-pull
Digest: sha256:641b95ddb2ea9dc2af1a0113b6b348ebc20872ba615204fbe12148e98fd6f23d
Status: Downloaded newer image for registry:5000/trust-pull@sha256:641b95ddb2ea9dc2af1a0113b6b348ebc20872ba615204fbe12148e98fd6f23d
registry:5000/trust-pull:latest