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