Files
docker-cli/cli/command/plugin/push.go
Sebastiaan van Stijn ec89d0ba67 cli/command/plugin: fix linting issues, and assorted cleanups
- fix various unhandled errors
- remove some locally defined option-types in favor of option-types
  defined by the client / api
- don't use unkeyed structs in tests, and add docs for some subtests
- fix some values in tests that triggered "spellcheck" warnings
- inline vars / functions that only had a single use.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit c6f935eba5)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-22 09:54:50 +02:00

77 lines
1.9 KiB
Go

package plugin
import (
"context"
"github.com/distribution/reference"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/trust"
"github.com/docker/cli/internal/jsonstream"
"github.com/docker/cli/internal/registry"
registrytypes "github.com/docker/docker/api/types/registry"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
type pushOptions struct {
name string
untrusted bool
}
func newPushCommand(dockerCli command.Cli) *cobra.Command {
var opts pushOptions
cmd := &cobra.Command{
Use: "push [OPTIONS] PLUGIN[:TAG]",
Short: "Push a plugin to a registry",
Args: cli.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.name = args[0]
return runPush(cmd.Context(), dockerCli, opts)
},
}
flags := cmd.Flags()
flags.BoolVar(&opts.untrusted, "disable-content-trust", !dockerCli.ContentTrustEnabled(), "Skip image signing")
return cmd
}
func runPush(ctx context.Context, dockerCli command.Cli, opts pushOptions) error {
named, err := reference.ParseNormalizedNamed(opts.name)
if err != nil {
return err
}
if _, ok := named.(reference.Canonical); ok {
return errors.Errorf("invalid name: %s", opts.name)
}
named = reference.TagNameOnly(named)
indexInfo := registry.NewIndexInfo(named)
authConfig := command.ResolveAuthConfig(dockerCli.ConfigFile(), indexInfo)
encodedAuth, err := registrytypes.EncodeAuthConfig(authConfig)
if err != nil {
return err
}
responseBody, err := dockerCli.Client().PluginPush(ctx, reference.FamiliarString(named), encodedAuth)
if err != nil {
return err
}
defer func() {
_ = responseBody.Close()
}()
if !opts.untrusted {
repoInfo := &trust.RepositoryInfo{
Name: reference.TrimNamed(named),
Index: indexInfo,
}
return trust.PushTrustedReference(ctx, dockerCli, repoInfo, named, authConfig, responseBody, command.UserAgent())
}
return jsonstream.Display(ctx, responseBody, dockerCli.Out())
}