github.com/thajeztah/cli@v0.0.0-20240223162942-dc6bfac81a8b/cli/command/plugin/upgrade.go (about)

     1  package plugin
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/distribution/reference"
     9  	"github.com/docker/cli/cli"
    10  	"github.com/docker/cli/cli/command"
    11  	"github.com/docker/docker/pkg/jsonmessage"
    12  	"github.com/pkg/errors"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  func newUpgradeCommand(dockerCli command.Cli) *cobra.Command {
    17  	var options pluginOptions
    18  	cmd := &cobra.Command{
    19  		Use:   "upgrade [OPTIONS] PLUGIN [REMOTE]",
    20  		Short: "Upgrade an existing plugin",
    21  		Args:  cli.RequiresRangeArgs(1, 2),
    22  		RunE: func(cmd *cobra.Command, args []string) error {
    23  			options.localName = args[0]
    24  			if len(args) == 2 {
    25  				options.remote = args[1]
    26  			}
    27  			return runUpgrade(cmd.Context(), dockerCli, options)
    28  		},
    29  		Annotations: map[string]string{"version": "1.26"},
    30  	}
    31  
    32  	flags := cmd.Flags()
    33  	loadPullFlags(dockerCli, &options, flags)
    34  	flags.BoolVar(&options.skipRemoteCheck, "skip-remote-check", false, "Do not check if specified remote plugin matches existing plugin image")
    35  	return cmd
    36  }
    37  
    38  func runUpgrade(ctx context.Context, dockerCli command.Cli, opts pluginOptions) error {
    39  	p, _, err := dockerCli.Client().PluginInspectWithRaw(ctx, opts.localName)
    40  	if err != nil {
    41  		return errors.Errorf("error reading plugin data: %v", err)
    42  	}
    43  
    44  	if p.Enabled {
    45  		return errors.Errorf("the plugin must be disabled before upgrading")
    46  	}
    47  
    48  	opts.localName = p.Name
    49  	if opts.remote == "" {
    50  		opts.remote = p.PluginReference
    51  	}
    52  	remote, err := reference.ParseNormalizedNamed(opts.remote)
    53  	if err != nil {
    54  		return errors.Wrap(err, "error parsing remote upgrade image reference")
    55  	}
    56  	remote = reference.TagNameOnly(remote)
    57  
    58  	old, err := reference.ParseNormalizedNamed(p.PluginReference)
    59  	if err != nil {
    60  		return errors.Wrap(err, "error parsing current image reference")
    61  	}
    62  	old = reference.TagNameOnly(old)
    63  
    64  	fmt.Fprintf(dockerCli.Out(), "Upgrading plugin %s from %s to %s\n", p.Name, reference.FamiliarString(old), reference.FamiliarString(remote))
    65  	if !opts.skipRemoteCheck && remote.String() != old.String() {
    66  		if !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), "Plugin images do not match, are you sure?") {
    67  			return errors.New("canceling upgrade request")
    68  		}
    69  	}
    70  
    71  	options, err := buildPullConfig(ctx, dockerCli, opts, "plugin upgrade")
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	responseBody, err := dockerCli.Client().PluginUpgrade(ctx, opts.localName, options)
    77  	if err != nil {
    78  		if strings.Contains(err.Error(), "target is image") {
    79  			return errors.New(err.Error() + " - Use `docker image pull`")
    80  		}
    81  		return err
    82  	}
    83  	defer responseBody.Close()
    84  	if err := jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil); err != nil {
    85  		return err
    86  	}
    87  	fmt.Fprintf(dockerCli.Out(), "Upgraded plugin %s to %s\n", opts.localName, opts.remote) // todo: return proper values from the API for this result
    88  	return nil
    89  }