diff --git a/CHANGELOG.md b/CHANGELOG.md index fd51735159d..3f269fa5f8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ (Add entries below in the order of server, console, cli, docs, others) - console: add foreign key CRUD functionality to ms sql server tables - console: allow tracking of custom SQL functions having composite type (rowtype) input arguments +- cli: add interactive prompt to get input when `--database-name` flag is missing ## v2.0.0-beta.1 diff --git a/cli/cli.go b/cli/cli.go index 3cda1c27880..60384b47870 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -331,6 +331,9 @@ type Config struct { // Version of the config. Version ConfigVersion `yaml:"version,omitempty"` + // DisableInteractive disables interactive prompt + DisableInteractive bool `yaml:"disable_interactive,omitempty"` + // ServerConfig to be used by CLI to contact server. ServerConfig `yaml:",inline"` @@ -820,7 +823,8 @@ func (ec *ExecutionContext) readConfig() error { } ec.Config = &Config{ - Version: ConfigVersion(v.GetInt("version")), + Version: ConfigVersion(v.GetInt("version")), + DisableInteractive: v.GetBool("disable_interactive"), ServerConfig: ServerConfig{ Endpoint: v.GetString("endpoint"), AdminSecret: adminSecret, diff --git a/cli/commands/migrate.go b/cli/commands/migrate.go index 91d2554d27e..083ead9fafb 100644 --- a/cli/commands/migrate.go +++ b/cli/commands/migrate.go @@ -50,12 +50,14 @@ func NewMigrateCmd(ec *cli.ExecutionContext) *cobra.Command { f.MarkDeprecated("access-key", "use --admin-secret instead") f.Bool("insecure-skip-tls-verify", false, "skip TLS verification and disable cert checking (default: false)") f.String("certificate-authority", "", "path to a cert file for the certificate authority") + f.Bool("disable-interactive", false, "disables interactive prompts (default: false)") util.BindPFlag(v, "endpoint", f.Lookup("endpoint")) util.BindPFlag(v, "admin_secret", f.Lookup("admin-secret")) util.BindPFlag(v, "access_key", f.Lookup("access-key")) util.BindPFlag(v, "insecure_skip_tls_verify", f.Lookup("insecure-skip-tls-verify")) util.BindPFlag(v, "certificate_authority", f.Lookup("certificate-authority")) + util.BindPFlag(v, "disable_interactive", f.Lookup("disable-interactive")) f.BoolVar(&ec.DisableAutoStateMigration, "disable-auto-state-migration", false, "after a config v3 update, disable automatically moving state from hdb_catalog.schema_migrations to catalog state") f.MarkHidden("disable-auto-state-migration") @@ -107,32 +109,49 @@ func executeStatus(t *migrate.Migrate) (*migrate.Status, error) { } func validateConfigV3Flags(cmd *cobra.Command, ec *cli.ExecutionContext) error { - if ec.Config.Version >= cli.V3 { - if !cmd.Flags().Changed("database-name") { - return errDatabaseNameNotSet{"--database-name flag is required"} - } else { - // if database-name flag is set - // find out the database kind by making a API call to server - // and update ec to include the database name and kind - sourceKind, err := metadatautil.GetSourceKind(ec.APIClient.V1Metadata.ExportMetadata, ec.Source.Name) - if err != nil { - return fmt.Errorf("determining database kind of %s: %w", ec.Source.Name, err) - } - if sourceKind == nil { - return fmt.Errorf("error determining database kind for %s, check if database exists on hasura", ec.Source.Name) - } - ec.Source.Kind = *sourceKind - - if !migrate.IsMigrationsSupported(*sourceKind) { - return fmt.Errorf("migrations on source %s of kind %s is not supported", ec.Source.Name, *sourceKind) - } - } - } else { - // for project using config older than v3, use PG source kind + // for project using config older than v3, use PG source kind + if ec.Config.Version < cli.V3 { ec.Source.Kind = hasura.SourceKindPG if err := scripts.CheckIfUpdateToConfigV3IsRequired(ec); err != nil { return err } + return nil + } + + // if --all-databases flag is present, ignore --database-name and showing UI prompt for choosing a single database + if cmd.Flags().Changed("all-databases") { + return nil + } + + // for project using config equal to or greater than v3 + // database-name flag is required when running in non-terminal mode + if (!ec.IsTerminal || ec.Config.DisableInteractive) && !cmd.Flags().Changed("database-name") { + return errDatabaseNameNotSet{"--database-name flag is required"} + } + + // prompt UI for choosing database if source name is not set + if ec.Source.Name == "" { + databaseName, err := metadatautil.DatabaseChooserUI(ec.APIClient.V1Metadata.ExportMetadata) + if err != nil { + return err + } + ec.Source.Name = databaseName + } + + // find out the database kind by making a API call to server + // and update ec to include the database name and kind + sourceKind, err := metadatautil.GetSourceKind(ec.APIClient.V1Metadata.ExportMetadata, ec.Source.Name) + if err != nil { + return fmt.Errorf("determining database kind of %s: %w", ec.Source.Name, err) + } + if sourceKind == nil { + return fmt.Errorf("error determining database kind for %s, check if database exists on hasura", ec.Source.Name) + } + ec.Source.Kind = *sourceKind + + // check if migration ops are supported for the database + if !migrate.IsMigrationsSupported(*sourceKind) { + return fmt.Errorf("migrations on source %s of kind %s is not supported", ec.Source.Name, *sourceKind) } return nil } diff --git a/cli/commands/migrate_apply.go b/cli/commands/migrate_apply.go index 74beb491058..df6436630c5 100644 --- a/cli/commands/migrate_apply.go +++ b/cli/commands/migrate_apply.go @@ -58,16 +58,7 @@ func newMigrateApplyCmd(ec *cli.ExecutionContext) *cobra.Command { hasura migrate apply --down all`, SilenceUsage: true, PreRunE: func(cmd *cobra.Command, args []string) error { - err := validateConfigV3Flags(cmd, ec) - // --database-name / --all-databases flag is required to be set - if _, ok := err.(errDatabaseNameNotSet); !ok { - return err - } else if ok { - if !cmd.Flags().Changed("all-databases") { - return fmt.Errorf("one of --database-name or --all-databases flag is required") - } - } - return nil + return validateConfigV3Flags(cmd, ec) }, RunE: func(cmd *cobra.Command, args []string) error { opts.Source = ec.Source diff --git a/cli/commands/scripts_update_config_v3.go b/cli/commands/scripts_update_config_v3.go index c4526fb855b..180827a33e9 100644 --- a/cli/commands/scripts_update_config_v3.go +++ b/cli/commands/scripts_update_config_v3.go @@ -2,6 +2,7 @@ package commands import ( "fmt" + "github.com/hasura/graphql-engine/cli/internal/scripts" "github.com/hasura/graphql-engine/cli/util" "github.com/spf13/afero" diff --git a/cli/commands/seed.go b/cli/commands/seed.go index e609bac4e3c..53375e673da 100644 --- a/cli/commands/seed.go +++ b/cli/commands/seed.go @@ -32,29 +32,44 @@ func NewSeedCmd(ec *cli.ExecutionContext) *cobra.Command { if err := ec.Validate(); err != nil { return err } - if ec.Config.Version >= cli.V3 { - if !cmd.Flags().Changed("database-name") { - return errors.New("--database-name flag is required") - } - sourceKind, err := metadatautil.GetSourceKind(ec.APIClient.V1Metadata.ExportMetadata, ec.Source.Name) - if err != nil { - return err - } - if sourceKind == nil { - return fmt.Errorf("cannot determine source kind for %v", ec.Source.Name) - } - ec.Source.Kind = *sourceKind - // check if seed ops are supported for the database - if !seed.IsSeedsSupported(*sourceKind) { - return fmt.Errorf("seed operations on database %s of kind %s is not supported", ec.Source.Name, *sourceKind) - } - } else { - // for project using config older than v3, use PG source kind + // for project using config older than v3, use PG source kind + if ec.Config.Version < cli.V3 { ec.Source.Kind = hasura.SourceKindPG if err := scripts.CheckIfUpdateToConfigV3IsRequired(ec); err != nil { return err } + return nil } + + // for project using config equal to or greater than v3 + // database-name flag is required when running in non-terminal mode + if (!ec.IsTerminal || ec.Config.DisableInteractive) && !cmd.Flags().Changed("database-name") { + return errors.New("--database-name flag is required") + } + + // prompt UI for choosing database if source name is not set + if ec.Source.Name == "" { + databaseName, err := metadatautil.DatabaseChooserUI(ec.APIClient.V1Metadata.ExportMetadata) + if err != nil { + return err + } + ec.Source.Name = databaseName + } + + sourceKind, err := metadatautil.GetSourceKind(ec.APIClient.V1Metadata.ExportMetadata, ec.Source.Name) + if err != nil { + return err + } + if sourceKind == nil { + return fmt.Errorf("cannot determine source kind for %v", ec.Source.Name) + } + ec.Source.Kind = *sourceKind + + // check if seed ops are supported for the database + if !seed.IsSeedsSupported(*sourceKind) { + return fmt.Errorf("seed operations on database %s of kind %s is not supported", ec.Source.Name, *sourceKind) + } + return nil }, } @@ -73,12 +88,14 @@ func NewSeedCmd(ec *cli.ExecutionContext) *cobra.Command { f.MarkDeprecated("access-key", "use --admin-secret instead") f.Bool("insecure-skip-tls-verify", false, "skip TLS verification and disable cert checking (default: false)") f.String("certificate-authority", "", "path to a cert file for the certificate authority") + f.Bool("disable-interactive", false, "disables interactive prompts (default: false)") util.BindPFlag(v, "endpoint", f.Lookup("endpoint")) util.BindPFlag(v, "admin_secret", f.Lookup("admin-secret")) util.BindPFlag(v, "access_key", f.Lookup("access-key")) util.BindPFlag(v, "insecure_skip_tls_verify", f.Lookup("insecure-skip-tls-verify")) util.BindPFlag(v, "certificate_authority", f.Lookup("certificate-authority")) + util.BindPFlag(v, "disable_interactive", f.Lookup("disable-interactive")) return seedCmd } diff --git a/cli/internal/metadatautil/sources.go b/cli/internal/metadatautil/sources.go index 60660996369..13835b9677c 100644 --- a/cli/internal/metadatautil/sources.go +++ b/cli/internal/metadatautil/sources.go @@ -1,6 +1,7 @@ package metadatautil import ( + "errors" "fmt" "io" "io/ioutil" @@ -8,6 +9,7 @@ import ( "github.com/goccy/go-yaml" "github.com/goccy/go-yaml/parser" "github.com/hasura/graphql-engine/cli/internal/hasura" + "github.com/hasura/graphql-engine/cli/util" ) func getMetadataAsYaml(exportMetadata func() (io.Reader, error)) ([]byte, error) { @@ -107,3 +109,19 @@ func GetSourcesAndKind(exportMetadata func() (io.Reader, error)) ([]Source, erro } return sources, nil } + +func DatabaseChooserUI(exportMetadata func() (io.Reader, error)) (string, error) { + sources, err := GetSources(exportMetadata) + if err != nil { + return "", fmt.Errorf("unable to get available databases: %w", err) + } + if len(sources) == 0 { + return "", errors.New("no connected databases found in the server") + } + databaseName, err := util.GetSelectPrompt("Select a database to use", sources) + if err != nil { + return "", fmt.Errorf("error in selecting a database to use: %w", err) + } + + return databaseName, nil +}