push.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package image
  2. import (
  3. "golang.org/x/net/context"
  4. "github.com/docker/docker/cli"
  5. "github.com/docker/docker/cli/command"
  6. "github.com/docker/docker/pkg/jsonmessage"
  7. "github.com/docker/docker/reference"
  8. "github.com/docker/docker/registry"
  9. "github.com/spf13/cobra"
  10. )
  11. // NewPushCommand creates a new `docker push` command
  12. func NewPushCommand(dockerCli *command.DockerCli) *cobra.Command {
  13. cmd := &cobra.Command{
  14. Use: "push [OPTIONS] NAME[:TAG]",
  15. Short: "Push an image or a repository to a registry",
  16. Args: cli.ExactArgs(1),
  17. RunE: func(cmd *cobra.Command, args []string) error {
  18. return runPush(dockerCli, args[0])
  19. },
  20. }
  21. flags := cmd.Flags()
  22. command.AddTrustedFlags(flags, true)
  23. return cmd
  24. }
  25. func runPush(dockerCli *command.DockerCli, remote string) error {
  26. ref, err := reference.ParseNamed(remote)
  27. if err != nil {
  28. return err
  29. }
  30. // Resolve the Repository name from fqn to RepositoryInfo
  31. repoInfo, err := registry.ParseRepositoryInfo(ref)
  32. if err != nil {
  33. return err
  34. }
  35. ctx := context.Background()
  36. // Resolve the Auth config relevant for this server
  37. authConfig := command.ResolveAuthConfig(ctx, dockerCli, repoInfo.Index)
  38. requestPrivilege := command.RegistryAuthenticationPrivilegedFunc(dockerCli, repoInfo.Index, "push")
  39. if command.IsTrusted() {
  40. return trustedPush(ctx, dockerCli, repoInfo, ref, authConfig, requestPrivilege)
  41. }
  42. responseBody, err := imagePushPrivileged(ctx, dockerCli, authConfig, ref.String(), requestPrivilege)
  43. if err != nil {
  44. return err
  45. }
  46. defer responseBody.Close()
  47. return jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil)
  48. }