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/install.go
* /
import (
"fmt"
2021-06-16 14:44:15 +03:00
"github.com/hasura/graphql-engine/cli/v2"
2022-11-08 13:25:53 +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"
2022-11-08 13:25:53 +03:00
"github.com/hasura/graphql-engine/cli/v2/util"
2020-02-24 19:14:46 +03:00
"github.com/spf13/cobra"
)
func newPluginsInstallCmd ( ec * cli . ExecutionContext ) * cobra . Command {
opts := & PluginInstallOptions {
EC : ec ,
}
pluginsInstallCmd := & cobra . Command {
Use : "install [plugin-name]" ,
Short : "Install a plugin from the index" ,
2022-12-30 06:50:48 +03:00
Long : "To install plugins that extend the functionality of the Hasura CLI, you can use the install command. This command will install the plugin from the index and add it to your configuration file." ,
2020-02-24 19:14:46 +03:00
Example : ` # Install a plugin :
hasura plugins install [ plugin - name ] ` ,
SilenceUsage : true ,
Args : cobra . ExactArgs ( 1 ) ,
PreRunE : func ( cmd * cobra . Command , args [ ] string ) error {
2022-11-08 13:25:53 +03:00
op := genOpName ( cmd , "PreRunE" )
2020-02-24 19:14:46 +03:00
err := ec . Prepare ( )
if err != nil {
2022-11-08 13:25:53 +03:00
return errors . E ( op , err )
2020-02-24 19:14:46 +03:00
}
err = ec . PluginsConfig . Repo . EnsureUpdated ( )
if err != nil {
ec . Logger . Debugf ( "unable to update plugins index: got %v" , err )
}
return nil
} ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2022-11-08 13:25:53 +03:00
op := genOpName ( cmd , "RunE" )
2020-02-24 19:14:46 +03:00
opts . Name = args [ 0 ]
ec . Spin ( fmt . Sprintf ( "Installing plugin %q..." , opts . Name ) )
defer ec . Spinner . Stop ( )
err := opts . Run ( )
if err == plugins . ErrIsAlreadyInstalled {
ec . Spinner . Stop ( )
ec . Logger . WithField ( "name" , opts . Name ) . Infof ( "%q" , err )
return nil
}
if err != nil && err != plugins . ErrIsAlreadyInstalled {
2022-11-08 13:25:53 +03:00
return errors . E ( op , fmt . Errorf ( "failed to install plugin %q: %w" , opts . Name , err ) )
2020-02-24 19:14:46 +03:00
}
ec . Spinner . Stop ( )
ec . Logger . WithField ( "name" , opts . Name ) . Infoln ( "plugin installed" )
return nil
} ,
}
f := pluginsInstallCmd . Flags ( )
2020-04-07 17:12:14 +03:00
f . Var ( & opts . Version , "version" , "version to be installed" )
2020-02-24 19:14:46 +03:00
f . StringVar ( & opts . ManifestFile , "manifest-file" , "" , "(dev) speficy local manifest file" )
2021-10-13 17:38:07 +03:00
if err := f . MarkHidden ( "manifest-file" ) ; err != nil {
ec . Logger . WithError ( err ) . Errorf ( "error while using a dependency library" )
}
2020-02-24 19:14:46 +03:00
return pluginsInstallCmd
}
type PluginInstallOptions struct {
EC * cli . ExecutionContext
Name string
ManifestFile string
2020-04-07 17:12:14 +03:00
Version util . VersionFlag
2020-02-24 19:14:46 +03:00
}
func ( o * PluginInstallOptions ) Run ( ) error {
2022-11-08 13:25:53 +03:00
var op errors . Op = "commands.PluginInstallOptions.Run"
2020-06-03 11:31:40 +03:00
plugin , err := o . EC . PluginsConfig . GetPlugin ( o . Name , plugins . FetchOpts {
2020-04-13 06:16:06 +03:00
ManifestFile : o . ManifestFile ,
Version : o . Version . Version ,
} )
2020-04-22 12:15:42 +03:00
if err != nil {
2022-11-08 13:25:53 +03:00
return errors . E ( op , err )
}
if err := o . EC . PluginsConfig . Install ( plugin ) ; err != nil {
return errors . E ( op , err )
2020-04-22 12:15:42 +03:00
}
2022-11-08 13:25:53 +03:00
return nil
2020-02-24 19:14:46 +03:00
}