github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/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/docker/pkg/jsonmessage"
    10  	"github.com/khulnasoft/cli/cli"
    11  	"github.com/khulnasoft/cli/cli/command"
    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 r, err := command.PromptForConfirmation(ctx, dockerCli.In(), dockerCli.Out(), "Plugin images do not match, are you sure?"); !r || err != nil {
    67  			if err != nil {
    68  				return errors.Wrap(err, "canceling upgrade request")
    69  			}
    70  			return errors.New("canceling upgrade request")
    71  		}
    72  	}
    73  
    74  	options, err := buildPullConfig(ctx, dockerCli, opts, "plugin upgrade")
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	responseBody, err := dockerCli.Client().PluginUpgrade(ctx, opts.localName, options)
    80  	if err != nil {
    81  		if strings.Contains(err.Error(), "target is image") {
    82  			return errors.New(err.Error() + " - Use `docker image pull`")
    83  		}
    84  		return err
    85  	}
    86  	defer responseBody.Close()
    87  	if err := jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil); err != nil {
    88  		return err
    89  	}
    90  	fmt.Fprintf(dockerCli.Out(), "Upgraded plugin %s to %s\n", opts.localName, opts.remote) // todo: return proper values from the API for this result
    91  	return nil
    92  }