github.com/xeptore/docker-cli@v20.10.14+incompatible/cli/command/plugin/push.go (about)

     1  package plugin
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/docker/cli/cli"
     7  	"github.com/docker/cli/cli/command"
     8  	"github.com/docker/cli/cli/command/image"
     9  	"github.com/docker/distribution/reference"
    10  	"github.com/docker/docker/pkg/jsonmessage"
    11  	"github.com/docker/docker/registry"
    12  	"github.com/pkg/errors"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  type pushOptions struct {
    17  	name      string
    18  	untrusted bool
    19  }
    20  
    21  func newPushCommand(dockerCli command.Cli) *cobra.Command {
    22  	var opts pushOptions
    23  	cmd := &cobra.Command{
    24  		Use:   "push [OPTIONS] PLUGIN[:TAG]",
    25  		Short: "Push a plugin to a registry",
    26  		Args:  cli.ExactArgs(1),
    27  		RunE: func(cmd *cobra.Command, args []string) error {
    28  			opts.name = 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  func runPush(dockerCli command.Cli, opts pushOptions) error {
    41  	named, err := reference.ParseNormalizedNamed(opts.name)
    42  	if err != nil {
    43  		return err
    44  	}
    45  	if _, ok := named.(reference.Canonical); ok {
    46  		return errors.Errorf("invalid name: %s", opts.name)
    47  	}
    48  
    49  	named = reference.TagNameOnly(named)
    50  
    51  	ctx := context.Background()
    52  
    53  	repoInfo, err := registry.ParseRepositoryInfo(named)
    54  	if err != nil {
    55  		return err
    56  	}
    57  	authConfig := command.ResolveAuthConfig(ctx, dockerCli, repoInfo.Index)
    58  
    59  	encodedAuth, err := command.EncodeAuthToBase64(authConfig)
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	responseBody, err := dockerCli.Client().PluginPush(ctx, reference.FamiliarString(named), encodedAuth)
    65  	if err != nil {
    66  		return err
    67  	}
    68  	defer responseBody.Close()
    69  
    70  	if !opts.untrusted {
    71  		repoInfo.Class = "plugin"
    72  		return image.PushTrustedReference(dockerCli, repoInfo, named, authConfig, responseBody)
    73  	}
    74  
    75  	return jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil)
    76  }