github.com/olljanat/moby@v1.13.1/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  	// Resolve the Repository name from fqn to RepositoryInfo
    59  	repoInfo, err := registry.ParseRepositoryInfo(distributionRef)
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	ctx := context.Background()
    65  
    66  	authConfig := command.ResolveAuthConfig(ctx, dockerCli, repoInfo.Index)
    67  	requestPrivilege := command.RegistryAuthenticationPrivilegedFunc(dockerCli, repoInfo.Index, "pull")
    68  
    69  	// Check if reference has a digest
    70  	_, isCanonical := distributionRef.(reference.Canonical)
    71  	if command.IsTrusted() && !isCanonical {
    72  		err = trustedPull(ctx, dockerCli, repoInfo, distributionRef, authConfig, requestPrivilege)
    73  	} else {
    74  		err = imagePullPrivileged(ctx, dockerCli, authConfig, distributionRef.String(), requestPrivilege, opts.all)
    75  	}
    76  	if err != nil {
    77  		if strings.Contains(err.Error(), "target is plugin") {
    78  			return errors.New(err.Error() + " - Use `docker plugin install`")
    79  		}
    80  		return err
    81  	}
    82  
    83  	return nil
    84  }