2020-02-24 19:14:46 +03:00
|
|
|
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"
|
|
|
|
"github.com/hasura/graphql-engine/cli/plugins"
|
2020-04-07 17:12:14 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/util"
|
2020-02-24 19:14:46 +03:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newPluginsUpgradeCmd(ec *cli.ExecutionContext) *cobra.Command {
|
2020-04-07 17:12:14 +03:00
|
|
|
opts := &PluginUpgradeOptions{
|
|
|
|
EC: ec,
|
|
|
|
}
|
2020-02-24 19:14:46 +03:00
|
|
|
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 {
|
|
|
|
return ec.Prepare()
|
|
|
|
},
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-04-07 17:12:14 +03:00
|
|
|
opts.Name = args[0]
|
|
|
|
ec.Spin(fmt.Sprintf("Upgrading plugin %q...", opts.Name))
|
2020-02-24 19:14:46 +03:00
|
|
|
defer ec.Spinner.Stop()
|
2020-04-07 17:12:14 +03:00
|
|
|
plugin, err := opts.Run()
|
2020-02-24 19:14:46 +03:00
|
|
|
if err != nil && err != plugins.ErrIsAlreadyUpgraded {
|
2020-04-07 17:12:14 +03:00
|
|
|
return errors.Wrapf(err, "failed to upgrade plugin %q", opts.Name)
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
|
|
|
ec.Spinner.Stop()
|
|
|
|
ec.Logger.WithFields(logrus.Fields{
|
2020-04-07 17:12:14 +03:00
|
|
|
"name": opts.Name,
|
2020-02-24 19:14:46 +03:00
|
|
|
"version": plugin.Version,
|
|
|
|
}).Infoln("Plugin upgraded")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2020-04-07 17:12:14 +03:00
|
|
|
|
|
|
|
f := pluginsUpgradeCmd.Flags()
|
|
|
|
|
|
|
|
f.Var(&opts.Version, "version", "version to be upgraded")
|
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
return pluginsUpgradeCmd
|
|
|
|
}
|
2020-04-07 17:12:14 +03:00
|
|
|
|
|
|
|
type PluginUpgradeOptions struct {
|
|
|
|
EC *cli.ExecutionContext
|
|
|
|
|
|
|
|
Name string
|
|
|
|
Version util.VersionFlag
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *PluginUpgradeOptions) Run() (plugins.Plugin, error) {
|
|
|
|
return o.EC.PluginsConfig.Upgrade(o.Name, o.Version.Version)
|
|
|
|
}
|