mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-14 17:02:49 +03:00
be8b46d37a
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6736 GitOrigin-RevId: d155a2abce4dd8094ff071a8d05af4174a96f3b9
81 lines
2.1 KiB
Go
81 lines
2.1 KiB
Go
package commands
|
|
|
|
/*
|
|
some of the code here is borrowed from the krew codebse (kubernetes)
|
|
and the copyright belongs to the respective authors.
|
|
|
|
source: https://github.com/kubernetes-sigs/krew/blob/master/cmd/krew/cmd/upgrade.go
|
|
*/
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/hasura/graphql-engine/cli/v2"
|
|
"github.com/hasura/graphql-engine/cli/v2/plugins"
|
|
"github.com/hasura/graphql-engine/cli/v2/util"
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func newPluginsUpgradeCmd(ec *cli.ExecutionContext) *cobra.Command {
|
|
opts := &PluginUpgradeOptions{
|
|
EC: ec,
|
|
}
|
|
pluginsUpgradeCmd := &cobra.Command{
|
|
Use: "upgrade",
|
|
Short: "Upgrade a plugin to a newer version",
|
|
Example: ` # Upgrade a plugin to a newer version
|
|
hasura plugins upgrade [plugin-name]`,
|
|
SilenceUsage: true,
|
|
Args: cobra.ExactArgs(1),
|
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
|
op := genOpName(cmd, "PreRunE")
|
|
if err := ec.Prepare(); err != nil {
|
|
return errors.E(op, err)
|
|
}
|
|
return nil
|
|
},
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
op := genOpName(cmd, "RunE")
|
|
opts.Name = args[0]
|
|
ec.Spin(fmt.Sprintf("Upgrading plugin %q...", opts.Name))
|
|
defer ec.Spinner.Stop()
|
|
plugin, err := opts.Run()
|
|
if err != nil && err != plugins.ErrIsAlreadyUpgraded {
|
|
return errors.E(op, fmt.Errorf("failed to upgrade plugin %q: %w", opts.Name, err))
|
|
}
|
|
ec.Spinner.Stop()
|
|
ec.Logger.WithFields(logrus.Fields{
|
|
"name": opts.Name,
|
|
"version": plugin.Version,
|
|
}).Infoln("Plugin upgraded")
|
|
return nil
|
|
},
|
|
}
|
|
|
|
f := pluginsUpgradeCmd.Flags()
|
|
|
|
f.Var(&opts.Version, "version", "version to be upgraded")
|
|
|
|
return pluginsUpgradeCmd
|
|
}
|
|
|
|
type PluginUpgradeOptions struct {
|
|
EC *cli.ExecutionContext
|
|
|
|
Name string
|
|
Version util.VersionFlag
|
|
}
|
|
|
|
func (o *PluginUpgradeOptions) Run() (plugins.Plugin, error) {
|
|
var op errors.Op = "commands.PluginUpgradeOptions.Run"
|
|
p ,err := o.EC.PluginsConfig.Upgrade(o.Name, o.Version.Version)
|
|
if err != nil {
|
|
return p, errors.E(op, err)
|
|
}
|
|
return p, nil
|
|
}
|