github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/api/client/push.go (about)

     1  package client
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  
     7  	Cli "github.com/docker/docker/cli"
     8  	"github.com/docker/docker/pkg/jsonmessage"
     9  	flag "github.com/docker/docker/pkg/mflag"
    10  	"github.com/docker/docker/reference"
    11  	"github.com/docker/docker/registry"
    12  	"github.com/docker/engine-api/client"
    13  	"github.com/docker/engine-api/types"
    14  )
    15  
    16  // CmdPush pushes an image or repository to the registry.
    17  //
    18  // Usage: docker push NAME[:TAG]
    19  func (cli *DockerCli) CmdPush(args ...string) error {
    20  	cmd := Cli.Subcmd("push", []string{"NAME[:TAG]"}, Cli.DockerCommands["push"].Description, true)
    21  	addTrustedFlags(cmd, false)
    22  	cmd.Require(flag.Exact, 1)
    23  
    24  	cmd.ParseFlags(args, true)
    25  
    26  	ref, err := reference.ParseNamed(cmd.Arg(0))
    27  	if err != nil {
    28  		return err
    29  	}
    30  
    31  	var tag string
    32  	switch x := ref.(type) {
    33  	case reference.Canonical:
    34  		return errors.New("cannot push a digest reference")
    35  	case reference.NamedTagged:
    36  		tag = x.Tag()
    37  	}
    38  
    39  	// Resolve the Repository name from fqn to RepositoryInfo
    40  	repoInfo, err := registry.ParseRepositoryInfo(ref)
    41  	if err != nil {
    42  		return err
    43  	}
    44  	// Resolve the Auth config relevant for this server
    45  	authConfig := registry.ResolveAuthConfig(cli.configFile.AuthConfigs, repoInfo.Index)
    46  
    47  	requestPrivilege := cli.registryAuthenticationPrivilegedFunc(repoInfo.Index, "push")
    48  	if isTrusted() {
    49  		return cli.trustedPush(repoInfo, tag, authConfig, requestPrivilege)
    50  	}
    51  
    52  	responseBody, err := cli.imagePushPrivileged(authConfig, ref.Name(), tag, requestPrivilege)
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	defer responseBody.Close()
    58  
    59  	return jsonmessage.DisplayJSONMessagesStream(responseBody, cli.out, cli.outFd, cli.isTerminalOut, nil)
    60  }
    61  
    62  func (cli *DockerCli) imagePushPrivileged(authConfig types.AuthConfig, imageID, tag string, requestPrivilege client.RequestPrivilegeFunc) (io.ReadCloser, error) {
    63  	encodedAuth, err := encodeAuthToBase64(authConfig)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  	options := types.ImagePushOptions{
    68  		ImageID:      imageID,
    69  		Tag:          tag,
    70  		RegistryAuth: encodedAuth,
    71  	}
    72  
    73  	return cli.client.ImagePush(options, requestPrivilege)
    74  }