pull.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package client
  2. import (
  3. "fmt"
  4. "net/url"
  5. Cli "github.com/docker/docker/cli"
  6. "github.com/docker/docker/graph/tags"
  7. flag "github.com/docker/docker/pkg/mflag"
  8. "github.com/docker/docker/pkg/parsers"
  9. "github.com/docker/docker/registry"
  10. )
  11. // CmdPull pulls an image or a repository from the registry.
  12. //
  13. // Usage: docker pull [OPTIONS] IMAGENAME[:TAG|@DIGEST]
  14. func (cli *DockerCli) CmdPull(args ...string) error {
  15. cmd := Cli.Subcmd("pull", []string{"NAME[:TAG|@DIGEST]"}, "Pull an image or a repository from a registry", true)
  16. allTags := cmd.Bool([]string{"a", "-all-tags"}, false, "Download all tagged images in the repository")
  17. addTrustedFlags(cmd, true)
  18. cmd.Require(flag.Exact, 1)
  19. cmd.ParseFlags(args, true)
  20. remote := cmd.Arg(0)
  21. taglessRemote, tag := parsers.ParseRepositoryTag(remote)
  22. if tag == "" && !*allTags {
  23. tag = tags.DefaultTag
  24. fmt.Fprintf(cli.out, "Using default tag: %s\n", tag)
  25. } else if tag != "" && *allTags {
  26. return fmt.Errorf("tag can't be used with --all-tags/-a")
  27. }
  28. ref := registry.ParseReference(tag)
  29. // Resolve the Repository name from fqn to RepositoryInfo
  30. repoInfo, err := registry.ParseRepositoryInfo(taglessRemote)
  31. if err != nil {
  32. return err
  33. }
  34. if isTrusted() && !ref.HasDigest() {
  35. // Check if tag is digest
  36. authConfig := registry.ResolveAuthConfig(cli.configFile, repoInfo.Index)
  37. return cli.trustedPull(repoInfo, ref, authConfig)
  38. }
  39. v := url.Values{}
  40. v.Set("fromImage", ref.ImageName(taglessRemote))
  41. _, _, err = cli.clientRequestAttemptLogin("POST", "/images/create?"+v.Encode(), nil, cli.out, repoInfo.Index, "pull")
  42. return err
  43. }