github.com/AliyunContainerService/cli@v0.0.0-20181009023821-814ced4b30d0/cli/command/image/push.go (about)

     1  package image
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/docker/cli/cli"
     7  	"github.com/docker/cli/cli/command"
     8  	"github.com/docker/distribution/reference"
     9  	"github.com/docker/docker/pkg/jsonmessage"
    10  	"github.com/docker/docker/registry"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  type pushOptions struct {
    15  	remote    string
    16  	untrusted bool
    17  }
    18  
    19  // NewPushCommand creates a new `docker push` command
    20  func NewPushCommand(dockerCli command.Cli) *cobra.Command {
    21  	var opts pushOptions
    22  
    23  	cmd := &cobra.Command{
    24  		Use:   "push [OPTIONS] NAME[:TAG]",
    25  		Short: "Push an image or a repository to a registry",
    26  		Args:  cli.ExactArgs(1),
    27  		RunE: func(cmd *cobra.Command, args []string) error {
    28  			opts.remote = args[0]
    29  			return RunPush(dockerCli, opts)
    30  		},
    31  	}
    32  
    33  	flags := cmd.Flags()
    34  
    35  	command.AddTrustSigningFlags(flags, &opts.untrusted, dockerCli.ContentTrustEnabled())
    36  
    37  	return cmd
    38  }
    39  
    40  // RunPush performs a push against the engine based on the specified options
    41  func RunPush(dockerCli command.Cli, opts pushOptions) error {
    42  	ref, err := reference.ParseNormalizedNamed(opts.remote)
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	// Resolve the Repository name from fqn to RepositoryInfo
    48  	repoInfo, err := registry.ParseRepositoryInfo(ref)
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	ctx := context.Background()
    54  
    55  	// Resolve the Auth config relevant for this server
    56  	authConfig := command.ResolveAuthConfig(ctx, dockerCli, repoInfo.Index)
    57  	requestPrivilege := command.RegistryAuthenticationPrivilegedFunc(dockerCli, repoInfo.Index, "push")
    58  
    59  	if !opts.untrusted {
    60  		return TrustedPush(ctx, dockerCli, repoInfo, ref, authConfig, requestPrivilege)
    61  	}
    62  
    63  	responseBody, err := imagePushPrivileged(ctx, dockerCli, authConfig, ref, requestPrivilege)
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	defer responseBody.Close()
    69  	return jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil)
    70  }