github.com/xeptore/docker-cli@v20.10.14+incompatible/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 quiet bool 22 untrusted bool 23 } 24 25 // NewPullCommand creates a new `docker pull` command 26 func NewPullCommand(dockerCli command.Cli) *cobra.Command { 27 var opts PullOptions 28 29 cmd := &cobra.Command{ 30 Use: "pull [OPTIONS] NAME[:TAG|@DIGEST]", 31 Short: "Pull an image or a repository from a registry", 32 Args: cli.ExactArgs(1), 33 RunE: func(cmd *cobra.Command, args []string) error { 34 opts.remote = args[0] 35 return RunPull(dockerCli, opts) 36 }, 37 } 38 39 flags := cmd.Flags() 40 41 flags.BoolVarP(&opts.all, "all-tags", "a", false, "Download all tagged images in the repository") 42 flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Suppress verbose output") 43 44 command.AddPlatformFlag(flags, &opts.platform) 45 command.AddTrustVerificationFlags(flags, &opts.untrusted, dockerCli.ContentTrustEnabled()) 46 47 return cmd 48 } 49 50 // RunPull performs a pull against the engine based on the specified options 51 func RunPull(cli command.Cli, opts PullOptions) error { 52 distributionRef, err := reference.ParseNormalizedNamed(opts.remote) 53 switch { 54 case err != nil: 55 return err 56 case opts.all && !reference.IsNameOnly(distributionRef): 57 return errors.New("tag can't be used with --all-tags/-a") 58 case !opts.all && reference.IsNameOnly(distributionRef): 59 distributionRef = reference.TagNameOnly(distributionRef) 60 if tagged, ok := distributionRef.(reference.Tagged); ok && !opts.quiet { 61 fmt.Fprintf(cli.Out(), "Using default tag: %s\n", tagged.Tag()) 62 } 63 } 64 65 ctx := context.Background() 66 imgRefAndAuth, err := trust.GetImageReferencesAndAuth(ctx, nil, AuthResolver(cli), distributionRef.String()) 67 if err != nil { 68 return err 69 } 70 71 // Check if reference has a digest 72 _, isCanonical := distributionRef.(reference.Canonical) 73 if !opts.untrusted && !isCanonical { 74 err = trustedPull(ctx, cli, imgRefAndAuth, opts) 75 } else { 76 err = imagePullPrivileged(ctx, cli, imgRefAndAuth, opts) 77 } 78 if err != nil { 79 if strings.Contains(err.Error(), "when fetching 'plugin'") { 80 return errors.New(err.Error() + " - Use `docker plugin install`") 81 } 82 return err 83 } 84 fmt.Fprintln(cli.Out(), imgRefAndAuth.Reference().String()) 85 return nil 86 }