github.com/portworx/docker@v1.12.1/api/client/image/push.go (about)

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