graphql-engine/cli/commands/metadata_apply.go

66 lines
1.8 KiB
Go
Raw Normal View History

2018-06-24 16:40:48 +03:00
package commands
import (
"github.com/hasura/graphql-engine/cli"
"github.com/pkg/errors"
2018-06-24 16:40:48 +03:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
2018-06-24 16:40:48 +03:00
)
func newMetadataApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
v := viper.New()
2018-06-24 16:40:48 +03:00
opts := &metadataApplyOptions{
EC: ec,
actionType: "apply",
}
metadataApplyCmd := &cobra.Command{
Use: "apply",
Short: "Apply Hasura metadata on a database",
Example: ` # Apply Hasura GraphQL Engine metadata present in metadata.[yaml|json] file:
2018-06-24 16:40:48 +03:00
hasura metadata apply`,
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
ec.Viper = v
return ec.Validate()
},
2018-06-24 16:40:48 +03:00
RunE: func(cmd *cobra.Command, args []string) error {
opts.EC.Spin("Applying metadata...")
err := opts.run()
opts.EC.Spinner.Stop()
if err != nil {
return errors.Wrap(err, "failed to apply metadata")
}
opts.EC.Logger.Info("Metadata applied")
return nil
2018-06-24 16:40:48 +03:00
},
}
f := metadataApplyCmd.Flags()
f.String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine")
f.String("admin-secret", "", "admin secret for Hasura GraphQL Engine")
f.String("access-key", "", "access key for Hasura GraphQL Engine")
f.MarkDeprecated("access-key", "use --admin-secret instead")
// need to create a new viper because https://github.com/spf13/viper/issues/233
v.BindPFlag("endpoint", f.Lookup("endpoint"))
v.BindPFlag("admin_secret", f.Lookup("admin-secret"))
v.BindPFlag("access_key", f.Lookup("access-key"))
2018-06-24 16:40:48 +03:00
return metadataApplyCmd
}
type metadataApplyOptions struct {
EC *cli.ExecutionContext
actionType string
}
func (o *metadataApplyOptions) run() error {
migrateDrv, err := newMigrate(o.EC.MigrationDir, o.EC.ServerConfig.ParsedEndpoint, o.EC.ServerConfig.AdminSecret, o.EC.Logger, o.EC.Version)
2018-06-24 16:40:48 +03:00
if err != nil {
return err
2018-06-24 16:40:48 +03:00
}
return executeMetadata(o.actionType, migrateDrv, o.EC)
2018-06-24 16:40:48 +03:00
}