graphql-engine/cli/commands/plugins_upgrade.go
Aravind K P 3136ffad1b cli: update go.mod
>

### Description
Update `go.mod` to allow other packages to import [v2.0.0 versions](https://blog.golang.org/v2-go-modules).

### Changelog

- [x] `CHANGELOG.md` is updated with user-facing content relevant to this PR. If no changelog is required, then add the `no-changelog-required` label.

### Affected components

- [x] CLI

https://github.com/hasura/graphql-engine-mono/pull/1584

GitOrigin-RevId: a5d17ad20289d1cd7217763f56ef3ba6552d69c4
2021-06-16 11:45:07 +00:00

70 lines
1.8 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/pkg/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 {
return ec.Prepare()
},
RunE: func(cmd *cobra.Command, args []string) error {
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.Wrapf(err, "failed to upgrade plugin %q", opts.Name)
}
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) {
return o.EC.PluginsConfig.Upgrade(o.Name, o.Version.Version)
}