github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/api/client/pull.go (about)

     1  package client
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	Cli "github.com/docker/docker/cli"
     8  	"github.com/docker/docker/pkg/jsonmessage"
     9  	flag "github.com/docker/docker/pkg/mflag"
    10  	"github.com/docker/docker/reference"
    11  	"github.com/docker/docker/registry"
    12  	"github.com/docker/engine-api/client"
    13  	"github.com/docker/engine-api/types"
    14  )
    15  
    16  // CmdPull pulls an image or a repository from the registry.
    17  //
    18  // Usage: docker pull [OPTIONS] IMAGENAME[:TAG|@DIGEST]
    19  func (cli *DockerCli) CmdPull(args ...string) error {
    20  	cmd := Cli.Subcmd("pull", []string{"NAME[:TAG|@DIGEST]"}, Cli.DockerCommands["pull"].Description, true)
    21  	allTags := cmd.Bool([]string{"a", "-all-tags"}, false, "Download all tagged images in the repository")
    22  	addTrustedFlags(cmd, true)
    23  	cmd.Require(flag.Exact, 1)
    24  
    25  	cmd.ParseFlags(args, true)
    26  	remote := cmd.Arg(0)
    27  
    28  	distributionRef, err := reference.ParseNamed(remote)
    29  	if err != nil {
    30  		return err
    31  	}
    32  	if *allTags && !reference.IsNameOnly(distributionRef) {
    33  		return errors.New("tag can't be used with --all-tags/-a")
    34  	}
    35  
    36  	if !*allTags && reference.IsNameOnly(distributionRef) {
    37  		distributionRef = reference.WithDefaultTag(distributionRef)
    38  		fmt.Fprintf(cli.out, "Using default tag: %s\n", reference.DefaultTag)
    39  	}
    40  
    41  	var tag string
    42  	switch x := distributionRef.(type) {
    43  	case reference.Canonical:
    44  		tag = x.Digest().String()
    45  	case reference.NamedTagged:
    46  		tag = x.Tag()
    47  	}
    48  
    49  	ref := registry.ParseReference(tag)
    50  
    51  	// Resolve the Repository name from fqn to RepositoryInfo
    52  	repoInfo, err := registry.ParseRepositoryInfo(distributionRef)
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	authConfig := registry.ResolveAuthConfig(cli.configFile.AuthConfigs, repoInfo.Index)
    58  	requestPrivilege := cli.registryAuthenticationPrivilegedFunc(repoInfo.Index, "pull")
    59  
    60  	if isTrusted() && !ref.HasDigest() {
    61  		// Check if tag is digest
    62  		return cli.trustedPull(repoInfo, ref, authConfig, requestPrivilege)
    63  	}
    64  
    65  	return cli.imagePullPrivileged(authConfig, distributionRef.String(), "", requestPrivilege)
    66  }
    67  
    68  func (cli *DockerCli) imagePullPrivileged(authConfig types.AuthConfig, imageID, tag string, requestPrivilege client.RequestPrivilegeFunc) error {
    69  
    70  	encodedAuth, err := encodeAuthToBase64(authConfig)
    71  	if err != nil {
    72  		return err
    73  	}
    74  	options := types.ImagePullOptions{
    75  		ImageID:      imageID,
    76  		Tag:          tag,
    77  		RegistryAuth: encodedAuth,
    78  	}
    79  
    80  	responseBody, err := cli.client.ImagePull(options, requestPrivilege)
    81  	if err != nil {
    82  		return err
    83  	}
    84  	defer responseBody.Close()
    85  
    86  	return jsonmessage.DisplayJSONMessagesStream(responseBody, cli.out, cli.outFd, cli.isTerminalOut, nil)
    87  }