pull.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package client
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/docker/distribution/reference"
  6. "github.com/docker/docker/api/client/lib"
  7. "github.com/docker/docker/api/types"
  8. Cli "github.com/docker/docker/cli"
  9. "github.com/docker/docker/pkg/jsonmessage"
  10. flag "github.com/docker/docker/pkg/mflag"
  11. "github.com/docker/docker/registry"
  12. tagpkg "github.com/docker/docker/tag"
  13. )
  14. var errTagCantBeUsed = errors.New("tag can't be used with --all-tags/-a")
  15. // CmdPull pulls an image or a repository from the registry.
  16. //
  17. // Usage: docker pull [OPTIONS] IMAGENAME[:TAG|@DIGEST]
  18. func (cli *DockerCli) CmdPull(args ...string) error {
  19. cmd := Cli.Subcmd("pull", []string{"NAME[:TAG|@DIGEST]"}, Cli.DockerCommands["pull"].Description, true)
  20. allTags := cmd.Bool([]string{"a", "-all-tags"}, false, "Download all tagged images in the repository")
  21. addTrustedFlags(cmd, true)
  22. cmd.Require(flag.Exact, 1)
  23. cmd.ParseFlags(args, true)
  24. remote := cmd.Arg(0)
  25. distributionRef, err := reference.ParseNamed(remote)
  26. if err != nil {
  27. return err
  28. }
  29. var tag string
  30. switch x := distributionRef.(type) {
  31. case reference.Digested:
  32. if *allTags {
  33. return errTagCantBeUsed
  34. }
  35. tag = x.Digest().String()
  36. case reference.Tagged:
  37. if *allTags {
  38. return errTagCantBeUsed
  39. }
  40. tag = x.Tag()
  41. default:
  42. if !*allTags {
  43. tag = tagpkg.DefaultTag
  44. distributionRef, err = reference.WithTag(distributionRef, tag)
  45. if err != nil {
  46. return err
  47. }
  48. fmt.Fprintf(cli.out, "Using default tag: %s\n", tag)
  49. }
  50. }
  51. ref := registry.ParseReference(tag)
  52. // Resolve the Repository name from fqn to RepositoryInfo
  53. repoInfo, err := registry.ParseRepositoryInfo(distributionRef)
  54. if err != nil {
  55. return err
  56. }
  57. authConfig := registry.ResolveAuthConfig(cli.configFile.AuthConfigs, repoInfo.Index)
  58. requestPrivilege := cli.registryAuthenticationPrivilegedFunc(repoInfo.Index, "pull")
  59. if isTrusted() && !ref.HasDigest() {
  60. // Check if tag is digest
  61. return cli.trustedPull(repoInfo, ref, authConfig, requestPrivilege)
  62. }
  63. return cli.imagePullPrivileged(authConfig, distributionRef.String(), "", requestPrivilege)
  64. }
  65. func (cli *DockerCli) imagePullPrivileged(authConfig types.AuthConfig, imageID, tag string, requestPrivilege lib.RequestPrivilegeFunc) error {
  66. encodedAuth, err := encodeAuthToBase64(authConfig)
  67. if err != nil {
  68. return err
  69. }
  70. options := types.ImagePullOptions{
  71. ImageID: imageID,
  72. Tag: tag,
  73. RegistryAuth: encodedAuth,
  74. }
  75. responseBody, err := cli.client.ImagePull(options, requestPrivilege)
  76. if err != nil {
  77. return err
  78. }
  79. defer responseBody.Close()
  80. return jsonmessage.DisplayJSONMessagesStream(responseBody, cli.out, cli.outFd, cli.isTerminalOut)
  81. }