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