2018-06-24 16:40:48 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hasura/graphql-engine/cli"
|
2019-04-03 08:52:03 +03:00
|
|
|
"github.com/pkg/errors"
|
2018-06-24 16:40:48 +03:00
|
|
|
"github.com/spf13/cobra"
|
2018-09-27 16:57:17 +03:00
|
|
|
"github.com/spf13/viper"
|
2018-06-24 16:40:48 +03:00
|
|
|
)
|
|
|
|
|
2018-10-08 10:24:43 +03:00
|
|
|
const longHelpMetadataExportCmd = `Export Hasura metadata and save it in migrations/metadata.yaml file.
|
2019-02-14 12:37:47 +03:00
|
|
|
The output is a yaml file which captures all the metadata required
|
2018-10-08 10:24:43 +03:00
|
|
|
by GraphQL Engine. This includes info about tables that are tracked,
|
2019-02-14 12:37:47 +03:00
|
|
|
permission rules, relationships and event triggers that are defined
|
2018-10-08 10:24:43 +03:00
|
|
|
on those tables.`
|
|
|
|
|
2018-06-28 11:36:25 +03:00
|
|
|
func newMetadataExportCmd(ec *cli.ExecutionContext) *cobra.Command {
|
2018-09-27 16:57:17 +03:00
|
|
|
v := viper.New()
|
2018-06-24 16:40:48 +03:00
|
|
|
opts := &metadataExportOptions{
|
|
|
|
EC: ec,
|
|
|
|
actionType: "export",
|
|
|
|
}
|
|
|
|
|
|
|
|
metadataExportCmd := &cobra.Command{
|
|
|
|
Use: "export",
|
|
|
|
Short: "Export Hasura GraphQL Engine metadata from the database",
|
2018-10-08 10:24:43 +03:00
|
|
|
Example: ` # Export metadata and save it in migrations/metadata.yaml file:
|
2019-12-12 08:16:36 +03:00
|
|
|
hasura metadata export
|
|
|
|
|
|
|
|
# Use with admin secret:
|
|
|
|
hasura metadata export --admin-secret "<admin-secret>"
|
|
|
|
|
|
|
|
# Export metadata to another instance specified by the flag:
|
|
|
|
hasura metadata export --endpoint "<endpoint>"`,
|
2018-06-24 16:40:48 +03:00
|
|
|
SilenceUsage: true,
|
2018-09-27 16:57:17 +03:00
|
|
|
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 {
|
2019-04-03 08:52:03 +03:00
|
|
|
opts.EC.Spin("Exporting metadata...")
|
|
|
|
err := opts.run()
|
|
|
|
opts.EC.Spinner.Stop()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to export metadata")
|
|
|
|
}
|
|
|
|
opts.EC.Logger.Info("Metadata exported")
|
|
|
|
return nil
|
2018-06-24 16:40:48 +03:00
|
|
|
},
|
2018-10-08 10:24:43 +03:00
|
|
|
Long: longHelpMetadataExportCmd,
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
|
|
|
|
2018-09-27 16:57:17 +03:00
|
|
|
f := metadataExportCmd.Flags()
|
|
|
|
f.String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine")
|
2019-02-14 12:37:47 +03:00
|
|
|
f.String("admin-secret", "", "admin secret for Hasura GraphQL Engine")
|
2018-09-27 16:57:17 +03:00
|
|
|
f.String("access-key", "", "access key for Hasura GraphQL Engine")
|
2019-02-14 12:37:47 +03:00
|
|
|
f.MarkDeprecated("access-key", "use --admin-secret instead")
|
2018-09-27 16:57:17 +03:00
|
|
|
|
|
|
|
// need to create a new viper because https://github.com/spf13/viper/issues/233
|
|
|
|
v.BindPFlag("endpoint", f.Lookup("endpoint"))
|
2019-02-14 12:37:47 +03:00
|
|
|
v.BindPFlag("admin_secret", f.Lookup("admin-secret"))
|
2018-09-27 16:57:17 +03:00
|
|
|
v.BindPFlag("access_key", f.Lookup("access-key"))
|
|
|
|
|
2018-06-24 16:40:48 +03:00
|
|
|
return metadataExportCmd
|
|
|
|
}
|
|
|
|
|
|
|
|
type metadataExportOptions struct {
|
|
|
|
EC *cli.ExecutionContext
|
|
|
|
|
|
|
|
actionType string
|
|
|
|
}
|
|
|
|
|
2018-06-28 11:36:25 +03:00
|
|
|
func (o *metadataExportOptions) run() error {
|
2019-09-18 08:36:16 +03:00
|
|
|
migrateDrv, err := newMigrate(o.EC.MigrationDir, o.EC.ServerConfig.ParsedEndpoint, o.EC.ServerConfig.AdminSecret, o.EC.Logger, o.EC.Version, true)
|
2018-06-24 16:40:48 +03:00
|
|
|
if err != nil {
|
2018-07-09 16:47:38 +03:00
|
|
|
return err
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
2019-03-20 08:10:06 +03:00
|
|
|
return executeMetadata(o.actionType, migrateDrv, o.EC)
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|