graphql-engine/cli/commands/plugins_install.go
Kali Vara Purushotham Santhati 94a3be3e6e cli: fix lint errors
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/1749
Co-authored-by: Aravind K P <8335904+scriptonist@users.noreply.github.com>
GitOrigin-RevId: 4515f7f2c58b7f28645b2c5a5d9842aa7a844eae
2021-10-13 14:39:15 +00:00

91 lines
2.4 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/install.go
*/
import (
"fmt"
"github.com/hasura/graphql-engine/cli/v2/util"
"github.com/hasura/graphql-engine/cli/v2"
"github.com/hasura/graphql-engine/cli/v2/plugins"
"github.com/pkg/errors"
"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",
Example: ` # Install a plugin:
hasura plugins install [plugin-name]`,
SilenceUsage: true,
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
err := ec.Prepare()
if err != nil {
return err
}
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 {
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 {
return errors.Wrapf(err, "failed to install plugin %q", opts.Name)
}
ec.Spinner.Stop()
ec.Logger.WithField("name", opts.Name).Infoln("plugin installed")
return nil
},
}
f := pluginsInstallCmd.Flags()
f.Var(&opts.Version, "version", "version to be installed")
f.StringVar(&opts.ManifestFile, "manifest-file", "", "(dev) speficy local manifest file")
if err := f.MarkHidden("manifest-file"); err != nil {
ec.Logger.WithError(err).Errorf("error while using a dependency library")
}
return pluginsInstallCmd
}
type PluginInstallOptions struct {
EC *cli.ExecutionContext
Name string
ManifestFile string
Version util.VersionFlag
}
func (o *PluginInstallOptions) Run() error {
plugin, err := o.EC.PluginsConfig.GetPlugin(o.Name, plugins.FetchOpts{
ManifestFile: o.ManifestFile,
Version: o.Version.Version,
})
if err != nil {
return err
}
return o.EC.PluginsConfig.Install(plugin)
}