github.com/portworx/docker@v1.12.1/api/client/image/pull.go (about) 1 package image 2 3 import ( 4 "errors" 5 "fmt" 6 7 "golang.org/x/net/context" 8 9 "github.com/docker/docker/api/client" 10 "github.com/docker/docker/cli" 11 "github.com/docker/docker/reference" 12 "github.com/docker/docker/registry" 13 "github.com/spf13/cobra" 14 ) 15 16 type pullOptions struct { 17 remote string 18 all bool 19 } 20 21 // NewPullCommand creates a new `docker pull` command 22 func NewPullCommand(dockerCli *client.DockerCli) *cobra.Command { 23 var opts pullOptions 24 25 cmd := &cobra.Command{ 26 Use: "pull [OPTIONS] NAME[:TAG|@DIGEST]", 27 Short: "Pull an image or a repository from a registry", 28 Args: cli.ExactArgs(1), 29 RunE: func(cmd *cobra.Command, args []string) error { 30 opts.remote = args[0] 31 return runPull(dockerCli, opts) 32 }, 33 } 34 35 flags := cmd.Flags() 36 37 flags.BoolVarP(&opts.all, "all-tags", "a", false, "Download all tagged images in the repository") 38 client.AddTrustedFlags(flags, true) 39 40 return cmd 41 } 42 43 func runPull(dockerCli *client.DockerCli, opts pullOptions) error { 44 distributionRef, err := reference.ParseNamed(opts.remote) 45 if err != nil { 46 return err 47 } 48 if opts.all && !reference.IsNameOnly(distributionRef) { 49 return errors.New("tag can't be used with --all-tags/-a") 50 } 51 52 if !opts.all && reference.IsNameOnly(distributionRef) { 53 distributionRef = reference.WithDefaultTag(distributionRef) 54 fmt.Fprintf(dockerCli.Out(), "Using default tag: %s\n", reference.DefaultTag) 55 } 56 57 var tag string 58 switch x := distributionRef.(type) { 59 case reference.Canonical: 60 tag = x.Digest().String() 61 case reference.NamedTagged: 62 tag = x.Tag() 63 } 64 65 registryRef := registry.ParseReference(tag) 66 67 // Resolve the Repository name from fqn to RepositoryInfo 68 repoInfo, err := registry.ParseRepositoryInfo(distributionRef) 69 if err != nil { 70 return err 71 } 72 73 ctx := context.Background() 74 75 authConfig := dockerCli.ResolveAuthConfig(ctx, repoInfo.Index) 76 requestPrivilege := dockerCli.RegistryAuthenticationPrivilegedFunc(repoInfo.Index, "pull") 77 78 if client.IsTrusted() && !registryRef.HasDigest() { 79 // Check if tag is digest 80 return dockerCli.TrustedPull(ctx, repoInfo, registryRef, authConfig, requestPrivilege) 81 } 82 83 return dockerCli.ImagePullPrivileged(ctx, authConfig, distributionRef.String(), requestPrivilege, opts.all) 84 85 }