push.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package image
  2. import (
  3. "golang.org/x/net/context"
  4. "github.com/docker/docker/api/client"
  5. "github.com/docker/docker/cli"
  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 *client.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. client.AddTrustedFlags(flags, true)
  23. return cmd
  24. }
  25. func runPush(dockerCli *client.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 := dockerCli.ResolveAuthConfig(ctx, repoInfo.Index)
  38. requestPrivilege := dockerCli.RegistryAuthenticationPrivilegedFunc(repoInfo.Index, "push")
  39. if client.IsTrusted() {
  40. return dockerCli.TrustedPush(ctx, repoInfo, ref, authConfig, requestPrivilege)
  41. }
  42. responseBody, err := dockerCli.ImagePushPrivileged(ctx, authConfig, ref.String(), requestPrivilege)
  43. if err != nil {
  44. return err
  45. }
  46. defer responseBody.Close()
  47. return jsonmessage.DisplayJSONMessagesStream(responseBody, dockerCli.Out(), dockerCli.OutFd(), dockerCli.IsTerminalOut(), nil)
  48. }