2016-09-08 13:11:39 -04:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2023-08-30 18:36:58 -04:00
|
|
|
"github.com/distribution/reference"
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/command/image"
|
2023-04-11 12:16:30 -04:00
|
|
|
registrytypes "github.com/docker/docker/api/types/registry"
|
2016-12-12 18:05:53 -05:00
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/registry"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2018-03-08 08:35:17 -05:00
|
|
|
type pushOptions struct {
|
|
|
|
name string
|
|
|
|
untrusted bool
|
|
|
|
}
|
|
|
|
|
2017-05-03 17:58:52 -04:00
|
|
|
func newPushCommand(dockerCli command.Cli) *cobra.Command {
|
2018-03-08 08:35:17 -05:00
|
|
|
var opts pushOptions
|
2016-09-08 13:11:39 -04:00
|
|
|
cmd := &cobra.Command{
|
2017-01-27 10:17:02 -05:00
|
|
|
Use: "push [OPTIONS] PLUGIN[:TAG]",
|
2016-11-14 11:38:06 -05:00
|
|
|
Short: "Push a plugin to a registry",
|
2016-09-08 13:11:39 -04:00
|
|
|
Args: cli.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2018-03-08 08:35:17 -05:00
|
|
|
opts.name = args[0]
|
|
|
|
return runPush(dockerCli, opts)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
}
|
2016-12-27 15:51:00 -05:00
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
2018-03-08 14:56:56 -05:00
|
|
|
command.AddTrustSigningFlags(flags, &opts.untrusted, dockerCli.ContentTrustEnabled())
|
2016-12-27 15:51:00 -05:00
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2018-03-08 08:35:17 -05:00
|
|
|
func runPush(dockerCli command.Cli, opts pushOptions) error {
|
|
|
|
named, err := reference.ParseNormalizedNamed(opts.name)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-11 16:54:52 -05:00
|
|
|
if _, ok := named.(reference.Canonical); ok {
|
2018-03-08 08:35:17 -05:00
|
|
|
return errors.Errorf("invalid name: %s", opts.name)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2017-01-11 16:54:52 -05:00
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
named = reference.TagNameOnly(named)
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
repoInfo, err := registry.ParseRepositoryInfo(named)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-10 11:24:07 -04:00
|
|
|
authConfig := command.ResolveAuthConfig(dockerCli.ConfigFile(), repoInfo.Index)
|
2023-04-11 12:16:30 -04:00
|
|
|
encodedAuth, err := registrytypes.EncodeAuthConfig(authConfig)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-11 16:54:52 -05:00
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
responseBody, err := dockerCli.Client().PluginPush(ctx, reference.FamiliarString(named), encodedAuth)
|
2016-12-12 18:05:53 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer responseBody.Close()
|
2016-12-27 15:51:00 -05:00
|
|
|
|
2018-03-08 14:56:56 -05:00
|
|
|
if !opts.untrusted {
|
2016-12-27 15:51:00 -05:00
|
|
|
return image.PushTrustedReference(dockerCli, repoInfo, named, authConfig, responseBody)
|
|
|
|
}
|
|
|
|
|
2016-12-12 18:05:53 -05:00
|
|
|
return jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|