Skip to content

Commit

Permalink
cli: defer validating plugins index and actions codegen repo
Browse files Browse the repository at this point in the history
closes #7695

PR-URL: hasura/graphql-engine-mono#2647
GitOrigin-RevId: 675f8da
  • Loading branch information
scriptonist authored and hasura-bot committed Nov 10, 2021
1 parent 0b1ecbf commit 97134b4
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 88 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
- console: add comments to tracked functions
- metadata SDK: add type definitions for config v3

- cli: core CLI features are not blocked in environments without internet (#7695)

### Bug fixes and improvements
(Add entries below in the order of server, console, cli, docs, others)

Expand Down
56 changes: 9 additions & 47 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,27 +523,11 @@ func (ec *ExecutionContext) Prepare() error {
return errors.Wrap(err, "setting up global config failed")
}

// setup plugins path
err = ec.setupPlugins()
if err != nil {
return errors.Wrap(err, "setting up plugins path failed")
}

if !ec.proPluginVersionValidated {
ec.validateProPluginVersion()
ec.proPluginVersionValidated = true
}

err = ec.setupCodegenAssetsRepo()
if err != nil {
return errors.Wrap(err, "setting up codegen-assets repo failed")
}

err = ec.setupInitTemplatesRepo()
if err != nil {
return errors.Wrap(err, "setting up init-templates repo failed")
}

ec.LastUpdateCheckFile = filepath.Join(ec.GlobalConfigDir, LastUpdateCheckFileName)

// initialize a blank server config
Expand All @@ -568,9 +552,9 @@ func (ec *ExecutionContext) Prepare() error {
return nil
}

// setupPlugins create and returns the inferred paths for hasura. By default, it assumes
// SetupPlugins create and returns the inferred paths for hasura. By default, it assumes
// $HOME/.hasura as the base path
func (ec *ExecutionContext) setupPlugins() error {
func (ec *ExecutionContext) SetupPlugins() error {
base := filepath.Join(ec.GlobalConfigDir, "plugins")
base, err := filepath.Abs(base)
if err != nil {
Expand All @@ -586,6 +570,10 @@ func (ec *ExecutionContext) setupPlugins() error {
}

func (ec *ExecutionContext) validateProPluginVersion() {
if err := ec.SetupPlugins(); err != nil {
ec.Logger.Debugf("Validating installed pro plugin version failed: %v", err)
return
}
installedPlugins, err := ec.PluginsConfig.ListInstalledPlugins()
if err != nil {
return
Expand All @@ -606,7 +594,7 @@ func (ec *ExecutionContext) validateProPluginVersion() {
}
}

func (ec *ExecutionContext) setupCodegenAssetsRepo() error {
func (ec *ExecutionContext) SetupCodegenAssetsRepo() error {
base := filepath.Join(ec.GlobalConfigDir, util.ActionsCodegenDirName)
base, err := filepath.Abs(base)
if err != nil {
Expand All @@ -620,20 +608,6 @@ func (ec *ExecutionContext) setupCodegenAssetsRepo() error {
return nil
}

func (ec *ExecutionContext) setupInitTemplatesRepo() error {
base := filepath.Join(ec.GlobalConfigDir, util.InitTemplatesDirName)
base, err := filepath.Abs(base)
if err != nil {
return errors.Wrap(err, "cannot get absolute path")
}
ec.InitTemplatesRepo = util.NewGitUtil(util.InitTemplatesRepoURI, base, "")
ec.InitTemplatesRepo.Logger = ec.Logger
if ec.GlobalConfig.CLIEnvironment == ServerOnDockerEnvironment {
ec.InitTemplatesRepo.DisableCloneOrUpdate = true
}
return nil
}

func (ec *ExecutionContext) SetHGEHeaders(headers map[string]string) {
ec.HGEHeaders = headers
}
Expand All @@ -642,22 +616,10 @@ func (ec *ExecutionContext) SetHGEHeaders(headers map[string]string) {
// ExecutionDirectory to see if all the required files and directories are in
// place.
func (ec *ExecutionContext) Validate() error {
// ensure plugins index exists
err := ec.PluginsConfig.Repo.EnsureCloned()
if err != nil {
return errors.Wrap(err, "ensuring plugins index failed")
}

// ensure codegen-assets repo exists
err = ec.CodegenAssetsRepo.EnsureCloned()
if err != nil {
return errors.Wrap(err, "ensuring codegen-assets repo failed")
}

// validate execution directory
err = ec.validateDirectory()
err := ec.validateDirectory()
if err != nil {
return errors.Wrap(err, "validating current directory failed")
return fmt.Errorf("validating current directory failed: %w", err)
}

// load .env file
Expand Down
1 change: 1 addition & 0 deletions cli/commands/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func NewActionsCmd(ec *cli.ExecutionContext) *cobra.Command {
if ec.Config.Version < cli.V2 {
return fmt.Errorf("actions commands can be executed only when config version is greater than 1")
}

if ec.MetadataDir == "" {
return fmt.Errorf("actions commands can be executed only when metadata_dir is set in config")
}
Expand Down
10 changes: 10 additions & 0 deletions cli/commands/actions_codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ func newActionsCodegenCmd(ec *cli.ExecutionContext) *cobra.Command {
# Derive an action from a hasura operation
hasura actions codegen [action-name] --derive-from ""`,
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := ec.SetupCodegenAssetsRepo(); err != nil {
return fmt.Errorf("setting up codegen-assets repo failed (this is required for automatically generating actions code): %w", err)
}
// ensure codegen-assets repo exists
if err := ec.CodegenAssetsRepo.EnsureCloned(); err != nil {
return fmt.Errorf("pulling latest actions codegen files from internet failed: %w", err)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.actions = args
return opts.run()
Expand Down
16 changes: 15 additions & 1 deletion cli/commands/actions_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ func (o *actionsCreateOptions) run() error {
return nil
}

if err := o.EC.SetupCodegenAssetsRepo(); err != nil {
o.EC.Logger.Errorf("failed generating code: setting up codegen-assets repo failed (this is required for automatically generating actions code): %v", err)
o.EC.Logger.Errorf("retry operation with: 'hasura actions codegen %s'", o.name)
return nil
}
// ensure codegen-assets repo exists
if err := ec.CodegenAssetsRepo.EnsureCloned(); err != nil {
o.EC.Logger.Errorf("failed generating code: pulling latest actions codegen files from internet failed: %v", err)
o.EC.Logger.Errorf("retry operation with: 'hasura actions codegen %s'", o.name)
return nil
}

// construct derive payload to send to codegenerator
derivePayload := types.DerivePayload{
IntrospectionSchema: introSchema,
Expand All @@ -132,7 +144,9 @@ func (o *actionsCreateOptions) run() error {
o.EC.Spin(fmt.Sprintf(`Running "hasura actions codegen %s"...`, o.name))
err = actionCfg.Codegen(o.name, derivePayload)
if err != nil {
return errors.Wrap(err, "error in generating codegen")
o.EC.Spinner.Stop()
o.EC.Logger.Warn("codegen failed, retry with `hasura actions codegen`")
return err
}
o.EC.Spinner.Stop()
o.EC.Logger.Info("Codegen files generated at " + o.EC.Config.ActionConfig.Codegen.OutputDir)
Expand Down
14 changes: 14 additions & 0 deletions cli/commands/actions_use_codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ func newActionsUseCodegenCmd(ec *cli.ExecutionContext) *cobra.Command {
# Use a codegen with a starter kit
hasura actions use-codegen --with-starter-kit true`,
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := ec.SetupCodegenAssetsRepo(); err != nil {
return fmt.Errorf("setting up codegen-assets repo failed (this is required for automatically generating actions code): %w", err)
}

if err := ec.SetupCodegenAssetsRepo(); err != nil {
return fmt.Errorf("setting up codengen assets repo failed")
}
// ensure codegen-assets repo exists
if err := ec.CodegenAssetsRepo.EnsureCloned(); err != nil {
return fmt.Errorf("pulling latest actions codegen files from internet failed: %w", err)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return opts.run()
},
Expand Down
39 changes: 6 additions & 33 deletions cli/commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func NewInitCmd(ec *cli.ExecutionContext) *cobra.Command {
if opts.Version <= cli.V1 {
return fmt.Errorf("config v1 is deprecated, please consider using config v3")
}
return ec.PluginsConfig.Repo.EnsureCloned()
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 1 {
Expand All @@ -86,7 +86,11 @@ func NewInitCmd(ec *cli.ExecutionContext) *cobra.Command {
f.StringVar(&opts.Endpoint, "endpoint", "", "http(s) endpoint for Hasura GraphQL engine")
f.StringVar(&opts.AdminSecret, "admin-secret", "", "admin secret for Hasura GraphQL engine")
f.StringVar(&opts.AdminSecret, "access-key", "", "access key for Hasura GraphQL engine")
f.StringVar(&opts.Template, "install-manifest", "", "install manifest to be cloned")

f.String("install-manifest", "", "install manifest to be cloned")
if err := f.MarkDeprecated("install-manifest", "refer: https://github.com/hasura/graphql-engine/tree/stable/install-manifests"); err != nil {
ec.Logger.Debugf("failed marking depricated flag")
}
if err := f.MarkDeprecated("access-key", "use --admin-secret instead"); err != nil {
ec.Logger.WithError(err).Errorf("error while using a dependency library")
}
Expand All @@ -111,8 +115,6 @@ type InitOptions struct {
AdminSecret string
InitDir string
GetMetadataMigrations bool

Template string
}

func (o *InitOptions) InitRun() error {
Expand Down Expand Up @@ -165,12 +167,6 @@ func (o *InitOptions) InitRun() error {
}
}

// create template files
err = o.createTemplateFiles()
if err != nil {
return err
}

// create other required files, like config.yaml, migrations directory
err = o.createFiles()
if err != nil {
Expand Down Expand Up @@ -287,29 +283,6 @@ func (o *InitOptions) createFiles() error {
return nil
}

func (o *InitOptions) createTemplateFiles() error {
if o.Template == "" {
return nil
}
err := o.EC.InitTemplatesRepo.EnsureUpdated()
if err != nil {
return errors.Wrap(err, "error in updating init-templates repo")
}
templatePath := filepath.Join(o.EC.InitTemplatesRepo.Path, o.Template)
info, err := os.Stat(templatePath)
if err != nil {
return errors.Wrap(err, "template doesn't exist")
}
if !info.IsDir() {
return errors.Errorf("template should be a directory")
}
err = util.CopyDir(templatePath, filepath.Join(o.EC.ExecutionDirectory, "install-manifest"))
if err != nil {
return err
}
return nil
}

func (o *InitOptions) Run() error {
context := &initCtx{
ec: o.EC,
Expand Down
11 changes: 10 additions & 1 deletion cli/commands/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,16 @@ Please open pull requests against this repo to add new plugins`,
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
cmd.Root().PersistentPreRun(cmd, args)
return ec.PluginsConfig.Repo.EnsureCloned()
// setup plugins path
err := ec.SetupPlugins()
if err != nil {
return fmt.Errorf("setting up plugins path failed: %w", err)
}

if err := ec.PluginsConfig.Repo.EnsureCloned(); err != nil {
return fmt.Errorf("pulling latest plugins list from internet failed: %w", err)
}
return nil
},
}
pluginsCmd.AddCommand(
Expand Down
6 changes: 0 additions & 6 deletions cli/util/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ const (
ActionsCodegenDirName string = "actions-codegen-assets"
)

// Default init-templates repo constants
const (
InitTemplatesRepoURI string = "https://github.com/hasura/graphql-engine-install-manifests.git"
InitTemplatesDirName string = "init-templates"
)

type GitUtil struct {
URI string
Path string
Expand Down

0 comments on commit 97134b4

Please sign in to comment.