add project flag, change persistentprerun to prerun (close #552) (#557)

### Description

Adds `--project` flag to the CLI and changes all `PersistentPreRuns` to `PreRuns`

What component does this PR affect? 

- [ ] Server
- [ ] Console
- [x] CLI
- [ ] Docs
- [ ] Community Content
- [ ] Build System

### Related Issue

#552
This commit is contained in:
Shahidh K Muhammed
2018-09-27 19:27:17 +05:30
committed by GitHub
parent 1a0af29920
commit 330f90de40
9 changed files with 82 additions and 34 deletions

View File

@@ -16,7 +16,6 @@ import (
const (
defaultDirectory = "hasura"
MANIFESTS_DIR = "install-scripts"
)
// NewInitCmd is the definition for init command
@@ -62,12 +61,9 @@ type initOptions struct {
}
func (o *initOptions) run() error {
if o.EC.ExecutionDirectory == "" {
o.EC.ExecutionDirectory = o.InitDir
}
var dir string
// prompt for init directory if it's not set already
if len(o.InitDir) == 0 {
if o.InitDir == "" {
p := promptui.Prompt{
Label: "Name of project directory ",
Default: defaultDirectory,
@@ -77,10 +73,18 @@ func (o *initOptions) run() error {
return handlePromptError(err)
}
if strings.TrimSpace(r) != "" {
o.EC.ExecutionDirectory = r
dir = r
} else {
o.EC.ExecutionDirectory = defaultDirectory
dir = defaultDirectory
}
} else {
dir = o.InitDir
}
if o.EC.ExecutionDirectory == "" {
o.EC.ExecutionDirectory = dir
} else {
o.EC.ExecutionDirectory = filepath.Join(o.EC.ExecutionDirectory, dir)
}
var infoMsg string

View File

@@ -9,32 +9,19 @@ import (
"github.com/hasura/graphql-engine/cli/migrate"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func NewMetadataCmd(ec *cli.ExecutionContext) *cobra.Command {
v := viper.New()
metadataCmd := &cobra.Command{
Use: "metadata",
Short: "Manage Hasura GraphQL Engine metdata saved in the database",
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
ec.Viper = v
return ec.Validate()
},
}
metadataCmd.AddCommand(
newMetadataExportCmd(ec),
newMetadataResetCmd(ec),
newMetadataApplyCmd(ec),
)
f := metadataCmd.PersistentFlags()
f.String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine")
f.String("access-key", "", "access key for Hasura GraphQL Engine")
// need to create a new viper because https://github.com/spf13/viper/issues/233
v.BindPFlag("endpoint", f.Lookup("endpoint"))
v.BindPFlag("access_key", f.Lookup("access-key"))
return metadataCmd
}

View File

@@ -3,9 +3,11 @@ package commands
import (
"github.com/hasura/graphql-engine/cli"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func newMetadataApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
v := viper.New()
opts := &metadataApplyOptions{
EC: ec,
actionType: "apply",
@@ -17,11 +19,23 @@ func newMetadataApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
Example: ` # Apply Hasura GraphQL Engine metadata present in metadata.yaml file:
hasura metadata apply`,
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
ec.Viper = v
return ec.Validate()
},
RunE: func(cmd *cobra.Command, args []string) error {
return opts.run()
},
}
f := metadataApplyCmd.Flags()
f.String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine")
f.String("access-key", "", "access key for Hasura GraphQL Engine")
// need to create a new viper because https://github.com/spf13/viper/issues/233
v.BindPFlag("endpoint", f.Lookup("endpoint"))
v.BindPFlag("access_key", f.Lookup("access-key"))
return metadataApplyCmd
}

View File

@@ -3,9 +3,11 @@ package commands
import (
"github.com/hasura/graphql-engine/cli"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func newMetadataExportCmd(ec *cli.ExecutionContext) *cobra.Command {
v := viper.New()
opts := &metadataExportOptions{
EC: ec,
actionType: "export",
@@ -17,11 +19,23 @@ func newMetadataExportCmd(ec *cli.ExecutionContext) *cobra.Command {
Example: ` # Export metadata and save it in metadata.yaml file:
hasura metadata export`,
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
ec.Viper = v
return ec.Validate()
},
RunE: func(cmd *cobra.Command, args []string) error {
return opts.run()
},
}
f := metadataExportCmd.Flags()
f.String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine")
f.String("access-key", "", "access key for Hasura GraphQL Engine")
// need to create a new viper because https://github.com/spf13/viper/issues/233
v.BindPFlag("endpoint", f.Lookup("endpoint"))
v.BindPFlag("access_key", f.Lookup("access-key"))
return metadataExportCmd
}

View File

@@ -4,9 +4,11 @@ import (
"github.com/hasura/graphql-engine/cli"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func newMetadataResetCmd(ec *cli.ExecutionContext) *cobra.Command {
v := viper.New()
opts := &metadataResetOptions{
EC: ec,
actionType: "reset",
@@ -18,11 +20,23 @@ func newMetadataResetCmd(ec *cli.ExecutionContext) *cobra.Command {
Example: ` # Clean all the metadata information from database:
hasura metadata reset`,
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
ec.Viper = v
return ec.Validate()
},
RunE: func(cmd *cobra.Command, args []string) error {
return opts.run()
},
}
f := metadataResetCmd.Flags()
f.String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine")
f.String("access-key", "", "access key for Hasura GraphQL Engine")
// need to create a new viper because https://github.com/spf13/viper/issues/233
v.BindPFlag("endpoint", f.Lookup("endpoint"))
v.BindPFlag("access_key", f.Lookup("access-key"))
return metadataResetCmd
}

View File

@@ -14,32 +14,19 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func NewMigrateCmd(ec *cli.ExecutionContext) *cobra.Command {
v := viper.New()
migrateCmd := &cobra.Command{
Use: "migrate",
Short: "Manage migrations on the database",
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
ec.Viper = v
return ec.Validate()
},
}
migrateCmd.AddCommand(
newMigrateApplyCmd(ec),
newMigrateStatusCmd(ec),
newMigrateCreateCmd(ec),
)
f := migrateCmd.PersistentFlags()
f.String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine")
f.String("access-key", "", "access key for Hasura GraphQL Engine")
// need to create a new viper because https://github.com/spf13/viper/issues/233
v.BindPFlag("endpoint", f.Lookup("endpoint"))
v.BindPFlag("access_key", f.Lookup("access-key"))
return migrateCmd
}

View File

@@ -8,9 +8,11 @@ import (
migrate "github.com/hasura/graphql-engine/cli/migrate"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func newMigrateApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
v := viper.New()
opts := &migrateApplyOptions{
EC: ec,
}
@@ -18,6 +20,10 @@ func newMigrateApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
Use: "apply",
Short: "Apply migrations on the database",
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
ec.Viper = v
return ec.Validate()
},
RunE: func(cmd *cobra.Command, args []string) error {
return opts.run()
},
@@ -28,6 +34,13 @@ func newMigrateApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
f.StringVar(&opts.downMigration, "down", "", "apply all or N down migration steps")
f.StringVar(&opts.versionMigration, "version", "", "migrate the database to a specific version")
f.StringVar(&opts.migrationType, "type", "up", "type of migration (up, down) to be used with version flag")
f.String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine")
f.String("access-key", "", "access key for Hasura GraphQL Engine")
// need to create a new viper because https://github.com/spf13/viper/issues/233
v.BindPFlag("endpoint", f.Lookup("endpoint"))
v.BindPFlag("access_key", f.Lookup("access-key"))
return migrateApplyCmd
}

View File

@@ -10,9 +10,11 @@ import (
"github.com/hasura/graphql-engine/cli/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func newMigrateStatusCmd(ec *cli.ExecutionContext) *cobra.Command {
v := viper.New()
opts := &migrateStatusOptions{
EC: ec,
}
@@ -20,6 +22,10 @@ func newMigrateStatusCmd(ec *cli.ExecutionContext) *cobra.Command {
Use: "status",
Short: "Display current status of migrations on a database",
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
ec.Viper = v
return ec.Validate()
},
RunE: func(cmd *cobra.Command, args []string) error {
status, err := opts.run()
if err != nil {
@@ -31,6 +37,14 @@ func newMigrateStatusCmd(ec *cli.ExecutionContext) *cobra.Command {
},
}
f := migrateStatusCmd.Flags()
f.String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine")
f.String("access-key", "", "access key for Hasura GraphQL Engine")
// need to create a new viper because https://github.com/spf13/viper/issues/233
v.BindPFlag("endpoint", f.Lookup("endpoint"))
v.BindPFlag("access_key", f.Lookup("access-key"))
return migrateStatusCmd
}

View File

@@ -27,6 +27,7 @@ func init() {
)
f := rootCmd.PersistentFlags()
f.StringVar(&ec.LogLevel, "log-level", "INFO", "log level (DEBUG, INFO, WARN, ERROR, FATAL)")
f.StringVar(&ec.ExecutionDirectory, "project", "", "directory where commands are executed. (default: current dir)")
}
// Execute executes the command and returns the error