github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/api/client/push.go (about)

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