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"
2021-06-16 14:44:15 +03:00
"github.com/hasura/graphql-engine/cli/v2"
2022-12-30 06:50:48 +03:00
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
2021-06-16 14:44:15 +03:00
"github.com/hasura/graphql-engine/cli/v2/plugins"
"github.com/hasura/graphql-engine/cli/v2/util"
2022-11-08 12:57:24 +03:00
2020-02-24 19:14:46 +03:00
"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" ,
2022-12-30 06:50:48 +03:00
Long : "To upgrade a plugin, run the upgrade command with the name of the plugin as an argument. If unsure of the plugin's name, you can run the `Hasura plugins list` command to see a list of all the available plugins." ,
2020-02-24 19:14:46 +03:00
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 {
2022-11-08 12:57:24 +03:00
op := genOpName ( cmd , "PreRunE" )
if err := ec . Prepare ( ) ; err != nil {
return errors . E ( op , err )
}
return nil
2020-02-24 19:14:46 +03:00
} ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2022-11-08 12:57:24 +03:00
op := genOpName ( cmd , "RunE" )
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 {
2022-11-08 12:57:24 +03:00
return errors . E ( op , fmt . Errorf ( "failed to upgrade plugin %q: %w" , opts . Name , err ) )
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 ) {
2022-11-08 12:57:24 +03:00
var op errors . Op = "commands.PluginUpgradeOptions.Run"
2022-12-30 06:50:48 +03:00
p , err := o . EC . PluginsConfig . Upgrade ( o . Name , o . Version . Version )
if err != nil {
2022-11-08 12:57:24 +03:00
return p , errors . E ( op , err )
}
return p , nil
2020-04-07 17:12:14 +03:00
}