github.com/olljanat/moby@v1.13.1/cli/command/image/push.go (about)

     1  package image
     2  
     3  import (
     4  	"golang.org/x/net/context"
     5  
     6  	"github.com/docker/docker/cli"
     7  	"github.com/docker/docker/cli/command"
     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 *command.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  	command.AddTrustedFlags(flags, true)
    28  
    29  	return cmd
    30  }
    31  
    32  func runPush(dockerCli *command.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 := command.ResolveAuthConfig(ctx, dockerCli, repoInfo.Index)
    48  	requestPrivilege := command.RegistryAuthenticationPrivilegedFunc(dockerCli, repoInfo.Index, "push")
    49  
    50  	if command.IsTrusted() {
    51  		return trustedPush(ctx, dockerCli, repoInfo, ref, authConfig, requestPrivilege)
    52  	}
    53  
    54  	responseBody, err := imagePushPrivileged(ctx, dockerCli, authConfig, ref.String(), requestPrivilege)
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	defer responseBody.Close()
    60  	return jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil)
    61  }