graphql-engine/cli/commands/metadata_export.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

82 lines
2.3 KiB
Go
Raw Normal View History

2018-06-24 16:40:48 +03:00
package commands
import (
"io/ioutil"
"github.com/hasura/graphql-engine/cli/v2"
"github.com/pkg/errors"
2018-06-24 16:40:48 +03:00
"github.com/spf13/cobra"
)
const longHelpMetadataExportCmd = `Export Hasura metadata and save it in the` + " ``/metadata``" + ` directory.
The output is a bunch of yaml files which captures all the metadata required
by the GraphQL engine. This includes info about tables that are tracked,
permission rules, relationships and event triggers that are defined
on those tables`
func newMetadataExportCmd(ec *cli.ExecutionContext) *cobra.Command {
opts := &MetadataExportOptions{
EC: ec,
2018-06-24 16:40:48 +03:00
}
metadataExportCmd := &cobra.Command{
Use: "export",
Short: "Export Hasura GraphQL engine metadata from the database",
Example: ` # Export metadata and save it in migrations/metadata.yaml file:
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,
RunE: func(cmd *cobra.Command, args []string) error {
if len(opts.output) == 0 {
ec.Spin("Exporting metadata...")
}
err := opts.Run()
ec.Spinner.Stop()
if err != nil {
return err
}
if len(opts.output) == 0 {
ec.Logger.Info("Metadata exported")
}
return nil
2018-06-24 16:40:48 +03:00
},
Long: longHelpMetadataExportCmd,
2018-06-24 16:40:48 +03:00
}
f := metadataExportCmd.Flags()
f.StringVarP(&opts.output, "output", "o", "", `write metadata to standard output in given format for exported metadata (note: this won't modify project metadata) Allowed values: json, yaml")`)
2018-06-24 16:40:48 +03:00
return metadataExportCmd
}
type MetadataExportOptions struct {
2018-06-24 16:40:48 +03:00
EC *cli.ExecutionContext
output string
2018-06-24 16:40:48 +03:00
}
func (o *MetadataExportOptions) Run() error {
if len(o.output) != 0 {
return getMetadataFromServerAndWriteToStdoutByFormat(o.EC, rawOutputFormat(o.output))
}
return getMetadataModeHandler(o.EC.MetadataMode).Export(o)
}
func getMetadataFromServerAndWriteToStdoutByFormat(ec *cli.ExecutionContext, format rawOutputFormat) error {
metadataReader, err := cli.GetCommonMetadataOps(ec).ExportMetadata()
if err != nil {
return errors.Wrap(err, "failed to export metadata")
}
jsonMetadata, err := ioutil.ReadAll(metadataReader)
if err != nil {
return errors.Wrap(err, "reading metadata failed")
}
return writeByOutputFormat(ec.Stdout, jsonMetadata, format)
2018-06-24 16:40:48 +03:00
}