Tizen_4.0 base
[platform/upstream/docker-engine.git] / vendor / github.com / docker / cli / cli / command / image / pull.go
1 package image
2
3 import (
4         "fmt"
5         "strings"
6
7         "github.com/docker/cli/cli"
8         "github.com/docker/cli/cli/command"
9         "github.com/docker/distribution/reference"
10         "github.com/docker/docker/registry"
11         "github.com/pkg/errors"
12         "github.com/spf13/cobra"
13         "golang.org/x/net/context"
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 command.Cli) *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         command.AddTrustVerificationFlags(flags)
39
40         return cmd
41 }
42
43 func runPull(dockerCli command.Cli, opts pullOptions) error {
44         distributionRef, err := reference.ParseNormalizedNamed(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.TagNameOnly(distributionRef)
54                 if tagged, ok := distributionRef.(reference.Tagged); ok {
55                         fmt.Fprintf(dockerCli.Out(), "Using default tag: %s\n", tagged.Tag())
56                 }
57         }
58
59         // Resolve the Repository name from fqn to RepositoryInfo
60         repoInfo, err := registry.ParseRepositoryInfo(distributionRef)
61         if err != nil {
62                 return err
63         }
64
65         ctx := context.Background()
66
67         authConfig := command.ResolveAuthConfig(ctx, dockerCli, repoInfo.Index)
68         requestPrivilege := command.RegistryAuthenticationPrivilegedFunc(dockerCli, repoInfo.Index, "pull")
69
70         // Check if reference has a digest
71         _, isCanonical := distributionRef.(reference.Canonical)
72         if command.IsTrusted() && !isCanonical {
73                 err = trustedPull(ctx, dockerCli, repoInfo, distributionRef, authConfig, requestPrivilege)
74         } else {
75                 err = imagePullPrivileged(ctx, dockerCli, authConfig, reference.FamiliarString(distributionRef), requestPrivilege, opts.all)
76         }
77         if err != nil {
78                 if strings.Contains(err.Error(), "when fetching 'plugin'") {
79                         return errors.New(err.Error() + " - Use `docker plugin install`")
80                 }
81                 return err
82         }
83
84         return nil
85 }