mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 09:22:43 +03:00
b75ab7233e
#### Issue: https://github.com/hasura/graphql-engine-mono/issues/2179 ### Problem: 1. There are few warnings when tests are executed with cli if it is built with `race` flag It is because of a race condition between stdin in migrate_delete for the prompt and stdout for other commands. 2. The integration test of the console used to behave as follows: ``` - Initially there won't be any wg.adds in wait-group so the second go-routine created in console-test (integration-test) - It will send the interrupt signal to the channel before the server has been started - So practically the server won't start ``` 3. The read and write for consoleopts.WG has been happening in different go-routines without control or lock system(reading and writing into same memory location without lock system). So there is a chance of data-race(warning for data-race while integration tests are executed) 4. Data-race errors from `promptui` package ### Solution: 1. Use `--force` flag to avoid getting the input from the prompt in `migrate_delete_test.go` 2. Introduced two fields in server and console opts to let know other go-routines whether the server has been started or not 3. To avoid data-race above which are shared b/w go-routines to know the status of servers has been operated atomically. 4. using new package https://github.com/AlecAivazis/survey https://github.com/hasura/graphql-engine-mono/pull/2231 GitOrigin-RevId: 387eb1be74f24dda34bb3588314b5a909adac227
142 lines
4.1 KiB
Go
142 lines
4.1 KiB
Go
package commands
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/hasura"
|
|
|
|
"github.com/hasura/graphql-engine/cli/v2"
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/metadataobject/actions"
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/metadataobject/actions/types"
|
|
"github.com/hasura/graphql-engine/cli/v2/util"
|
|
"github.com/pkg/errors"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
func newActionsCreateCmd(ec *cli.ExecutionContext, v *viper.Viper) *cobra.Command {
|
|
opts := &actionsCreateOptions{
|
|
EC: ec,
|
|
}
|
|
actionsCreateCmd := &cobra.Command{
|
|
Use: "create [action-name]",
|
|
Short: "Create a Hasura action",
|
|
Example: ` # Create a Hasura action
|
|
hasura actions create [action-name]
|
|
|
|
# Create a Hasura action with codegen
|
|
hasura actions create [action-name] --with-codegen
|
|
|
|
# Create a Hasura action by deriving from a hasura operation
|
|
hasura actions create [action-name] --derive-from ''
|
|
|
|
# Create a Hasura action with a different kind or webhook
|
|
hasura actions create [action-name] --kind [synchronous|asynchronous] --webhook [http://localhost:3000]`,
|
|
SilenceUsage: true,
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
opts.name = args[0]
|
|
return opts.run()
|
|
},
|
|
}
|
|
|
|
f := actionsCreateCmd.Flags()
|
|
|
|
f.StringVar(&opts.deriveFrom, "derive-from", "", "derive action from a Hasura operation")
|
|
f.BoolVar(&opts.withCodegen, "with-codegen", false, "create action along with codegen")
|
|
f.String("kind", "", "kind to use in action")
|
|
f.String("webhook", "", "webhook to use in action")
|
|
|
|
// bind to viper
|
|
util.BindPFlag(v, "actions.kind", f.Lookup("kind"))
|
|
util.BindPFlag(v, "actions.handler_webhook_baseurl", f.Lookup("webhook"))
|
|
|
|
return actionsCreateCmd
|
|
}
|
|
|
|
type actionsCreateOptions struct {
|
|
EC *cli.ExecutionContext
|
|
|
|
name string
|
|
deriveFrom string
|
|
withCodegen bool
|
|
}
|
|
|
|
func (o *actionsCreateOptions) run() error {
|
|
var introSchema hasura.IntrospectionSchema
|
|
var err error
|
|
if o.deriveFrom != "" {
|
|
o.deriveFrom = strings.TrimSpace(o.deriveFrom)
|
|
o.EC.Spin("Deriving a Hasura operation...")
|
|
introSchema, err = o.EC.APIClient.V1Graphql.GetIntrospectionSchema()
|
|
if err != nil {
|
|
return errors.Wrap(err, "error in fetching introspection schema")
|
|
}
|
|
o.EC.Spinner.Stop()
|
|
}
|
|
|
|
// create new action
|
|
o.EC.Spin("Creating the action...")
|
|
actionCfg := actions.New(o.EC, o.EC.MetadataDir)
|
|
o.EC.Spinner.Stop()
|
|
err = actionCfg.Create(o.name, introSchema, o.deriveFrom)
|
|
if err != nil {
|
|
return errors.Wrap(err, "error in creating action")
|
|
}
|
|
opts := &MetadataApplyOptions{
|
|
EC: o.EC,
|
|
}
|
|
err = opts.Run()
|
|
if err != nil {
|
|
return errors.Wrap(err, "error in applying metadata")
|
|
}
|
|
o.EC.Logger.WithField("name", o.name).Infoln("action created")
|
|
|
|
// if codegen config not present, skip codegen
|
|
if o.EC.Config.ActionConfig.Codegen.Framework == "" {
|
|
if o.withCodegen {
|
|
return fmt.Errorf(`Could not find codegen config. For adding codegen config, run:
|
|
|
|
hasura actions use-codegen`)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// if with-codegen flag not present, ask them if they want to codegen
|
|
var confirmation bool
|
|
if !o.withCodegen {
|
|
confirmation, err = util.GetYesNoPrompt("Do you want to generate " + o.EC.Config.ActionConfig.Codegen.Framework + " code for this action and the custom types?")
|
|
if err != nil {
|
|
return errors.Wrap(err, "error in getting user input")
|
|
}
|
|
}
|
|
|
|
if !confirmation {
|
|
infoMsg := fmt.Sprintf(`You skipped codegen. For getting codegen for this action, run:
|
|
|
|
hasura actions codegen %s
|
|
`, o.name)
|
|
o.EC.Logger.Info(infoMsg)
|
|
return nil
|
|
}
|
|
|
|
// construct derive payload to send to codegenerator
|
|
derivePayload := types.DerivePayload{
|
|
IntrospectionSchema: introSchema,
|
|
Operation: o.deriveFrom,
|
|
ActionName: o.name,
|
|
}
|
|
|
|
// Run codegen
|
|
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.Info("Codegen files generated at " + o.EC.Config.ActionConfig.Codegen.OutputDir)
|
|
return nil
|
|
|
|
}
|