github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/cli/command/plugin/upgrade.go (about) 1 package plugin 2 3 import ( 4 "context" 5 "fmt" 6 "strings" 7 8 "github.com/docker/cli/cli" 9 "github.com/docker/cli/cli/command" 10 "github.com/docker/distribution/reference" 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(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(dockerCli command.Cli, opts pluginOptions) error { 39 ctx := context.Background() 40 p, _, err := dockerCli.Client().PluginInspectWithRaw(ctx, opts.localName) 41 if err != nil { 42 return errors.Errorf("error reading plugin data: %v", err) 43 } 44 45 if p.Enabled { 46 return errors.Errorf("the plugin must be disabled before upgrading") 47 } 48 49 opts.localName = p.Name 50 if opts.remote == "" { 51 opts.remote = p.PluginReference 52 } 53 remote, err := reference.ParseNormalizedNamed(opts.remote) 54 if err != nil { 55 return errors.Wrap(err, "error parsing remote upgrade image reference") 56 } 57 remote = reference.TagNameOnly(remote) 58 59 old, err := reference.ParseNormalizedNamed(p.PluginReference) 60 if err != nil { 61 return errors.Wrap(err, "error parsing current image reference") 62 } 63 old = reference.TagNameOnly(old) 64 65 fmt.Fprintf(dockerCli.Out(), "Upgrading plugin %s from %s to %s\n", p.Name, reference.FamiliarString(old), reference.FamiliarString(remote)) 66 if !opts.skipRemoteCheck && remote.String() != old.String() { 67 if !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), "Plugin images do not match, are you sure?") { 68 return errors.New("canceling upgrade request") 69 } 70 } 71 72 options, err := buildPullConfig(ctx, dockerCli, opts, "plugin upgrade") 73 if err != nil { 74 return err 75 } 76 77 responseBody, err := dockerCli.Client().PluginUpgrade(ctx, opts.localName, options) 78 if err != nil { 79 if strings.Contains(err.Error(), "target is image") { 80 return errors.New(err.Error() + " - Use `docker image pull`") 81 } 82 return err 83 } 84 defer responseBody.Close() 85 if err := jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil); err != nil { 86 return err 87 } 88 fmt.Fprintf(dockerCli.Out(), "Upgraded plugin %s to %s\n", opts.localName, opts.remote) // todo: return proper values from the API for this result 89 return nil 90 }