github.com/chenchun/docker@v1.3.2-0.20150629222414-20467faf132b/api/client/pull.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  
     7  	"github.com/docker/docker/graph/tags"
     8  	flag "github.com/docker/docker/pkg/mflag"
     9  	"github.com/docker/docker/pkg/parsers"
    10  	"github.com/docker/docker/registry"
    11  	"github.com/docker/docker/utils"
    12  )
    13  
    14  // CmdPull pulls an image or a repository from the registry.
    15  //
    16  // Usage: docker pull [OPTIONS] IMAGENAME[:TAG|@DIGEST]
    17  func (cli *DockerCli) CmdPull(args ...string) error {
    18  	cmd := cli.Subcmd("pull", []string{"NAME[:TAG|@DIGEST]"}, "Pull an image or a repository from the registry", true)
    19  	allTags := cmd.Bool([]string{"a", "-all-tags"}, false, "Download all tagged images in the repository")
    20  	cmd.Require(flag.Exact, 1)
    21  
    22  	cmd.ParseFlags(args, true)
    23  
    24  	var (
    25  		v         = url.Values{}
    26  		remote    = cmd.Arg(0)
    27  		newRemote = remote
    28  	)
    29  	taglessRemote, tag := parsers.ParseRepositoryTag(remote)
    30  	if tag == "" && !*allTags {
    31  		newRemote = utils.ImageReference(taglessRemote, tags.DEFAULTTAG)
    32  	}
    33  	if tag != "" && *allTags {
    34  		return fmt.Errorf("tag can't be used with --all-tags/-a")
    35  	}
    36  
    37  	v.Set("fromImage", newRemote)
    38  
    39  	// Resolve the Repository name from fqn to RepositoryInfo
    40  	repoInfo, err := registry.ParseRepositoryInfo(taglessRemote)
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	_, _, err = cli.clientRequestAttemptLogin("POST", "/images/create?"+v.Encode(), nil, cli.out, repoInfo.Index, "pull")
    46  	return err
    47  }