graphql-engine/cli/commands/metadata_apply.go

83 lines
1.8 KiB
Go
Raw Normal View History

2018-06-24 16:40:48 +03:00
package commands
import (
"os"
2020-04-07 12:23:20 +03:00
"github.com/hasura/graphql-engine/cli/migrate"
2018-06-24 16:40:48 +03:00
"github.com/hasura/graphql-engine/cli"
"github.com/pkg/errors"
2018-06-24 16:40:48 +03:00
"github.com/spf13/cobra"
)
func newMetadataApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
opts := &MetadataApplyOptions{
2018-06-24 16:40:48 +03:00
EC: ec,
ActionType: "apply",
2018-06-24 16:40:48 +03:00
}
metadataApplyCmd := &cobra.Command{
Use: "apply",
Short: "Apply Hasura metadata on a database",
Example: ` # Apply Hasura GraphQL Engine metadata present in metadata.[yaml|json] file:
hasura metadata apply
# Use with admin secret:
hasura metadata apply --admin-secret "<admin-secret>"
# Apply metadata to an instance specified by the flag:
hasura metadata apply --endpoint "<endpoint>"`,
2018-06-24 16:40:48 +03:00
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if opts.dryRun {
o := &MetadataDiffOptions{
EC: ec,
Output: os.Stdout,
Args: []string{},
}
return o.Run()
}
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.BoolVar(&opts.FromFile, "from-file", false, "apply metadata from migrations/metadata.[yaml|json]")
f.BoolVar(&opts.dryRun, "dry-run", false, "show a diff instead of applying the metadata")
2018-06-24 16:40:48 +03:00
return metadataApplyCmd
}
type MetadataApplyOptions struct {
2018-06-24 16:40:48 +03:00
EC *cli.ExecutionContext
ActionType string
FromFile bool
dryRun bool
2018-06-24 16:40:48 +03:00
}
func (o *MetadataApplyOptions) Run() error {
if o.FromFile {
actualMetadataDir := o.EC.MetadataDir
o.EC.MetadataDir = ""
defer func() {
o.EC.MetadataDir = actualMetadataDir
}()
}
2020-04-07 12:23:20 +03:00
migrateDrv, err := migrate.NewMigrate(o.EC, true)
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
}