github.com/AliyunContainerService/cli@v0.0.0-20181009023821-814ced4b30d0/cli/command/image/pull.go (about) 1 package image 2 3 import ( 4 "context" 5 "fmt" 6 "strings" 7 8 "github.com/docker/cli/cli" 9 "github.com/docker/cli/cli/command" 10 "github.com/docker/cli/cli/trust" 11 "github.com/docker/distribution/reference" 12 "github.com/pkg/errors" 13 "github.com/spf13/cobra" 14 ) 15 16 // PullOptions defines what and how to pull 17 type PullOptions struct { 18 remote string 19 all bool 20 platform string 21 untrusted bool 22 } 23 24 // NewPullCommand creates a new `docker pull` command 25 func NewPullCommand(dockerCli command.Cli) *cobra.Command { 26 var opts PullOptions 27 28 cmd := &cobra.Command{ 29 Use: "pull [OPTIONS] NAME[:TAG|@DIGEST]", 30 Short: "Pull an image or a repository from a registry", 31 Args: cli.ExactArgs(1), 32 RunE: func(cmd *cobra.Command, args []string) error { 33 opts.remote = args[0] 34 return RunPull(dockerCli, opts) 35 }, 36 } 37 38 flags := cmd.Flags() 39 40 flags.BoolVarP(&opts.all, "all-tags", "a", false, "Download all tagged images in the repository") 41 42 command.AddPlatformFlag(flags, &opts.platform) 43 command.AddTrustVerificationFlags(flags, &opts.untrusted, dockerCli.ContentTrustEnabled()) 44 45 return cmd 46 } 47 48 // RunPull performs a pull against the engine based on the specified options 49 func RunPull(cli command.Cli, opts PullOptions) error { 50 distributionRef, err := reference.ParseNormalizedNamed(opts.remote) 51 switch { 52 case err != nil: 53 return err 54 case opts.all && !reference.IsNameOnly(distributionRef): 55 return errors.New("tag can't be used with --all-tags/-a") 56 case !opts.all && reference.IsNameOnly(distributionRef): 57 distributionRef = reference.TagNameOnly(distributionRef) 58 if tagged, ok := distributionRef.(reference.Tagged); ok { 59 fmt.Fprintf(cli.Out(), "Using default tag: %s\n", tagged.Tag()) 60 } 61 } 62 63 ctx := context.Background() 64 imgRefAndAuth, err := trust.GetImageReferencesAndAuth(ctx, nil, AuthResolver(cli), distributionRef.String()) 65 if err != nil { 66 return err 67 } 68 69 // Check if reference has a digest 70 _, isCanonical := distributionRef.(reference.Canonical) 71 if !opts.untrusted && !isCanonical { 72 err = trustedPull(ctx, cli, imgRefAndAuth, opts.platform) 73 } else { 74 err = imagePullPrivileged(ctx, cli, imgRefAndAuth, opts.all, opts.platform) 75 } 76 if err != nil { 77 if strings.Contains(err.Error(), "when fetching 'plugin'") { 78 return errors.New(err.Error() + " - Use `docker plugin install`") 79 } 80 return err 81 } 82 return nil 83 }