2018-06-24 16:40:48 +03:00
package commands
import (
2022-11-14 10:28:23 +03:00
"fmt"
2021-05-14 22:09:01 +03:00
"io/ioutil"
2021-06-16 14:44:15 +03:00
"github.com/hasura/graphql-engine/cli/v2"
2022-11-14 10:28:23 +03:00
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
2018-06-24 16:40:48 +03:00
"github.com/spf13/cobra"
)
2021-03-15 18:40:52 +03:00
const longHelpMetadataExportCmd = ` Export Hasura metadata and save it in the ` + " ``/metadata``" + ` directory .
2022-12-30 06:50:48 +03:00
The output is a collection 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 . `
2018-10-08 10:24:43 +03:00
2018-06-28 11:36:25 +03:00
func newMetadataExportCmd ( ec * cli . ExecutionContext ) * cobra . Command {
2020-02-24 19:14:46 +03:00
opts := & MetadataExportOptions {
2021-05-14 22:09:01 +03:00
EC : ec ,
2018-06-24 16:40:48 +03:00
}
metadataExportCmd := & cobra . Command {
Use : "export" ,
2022-12-30 06:50:48 +03:00
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 ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2022-11-14 10:28:23 +03:00
op := genOpName ( cmd , "RunE" )
2021-09-29 14:11:24 +03:00
if len ( opts . output ) == 0 {
ec . Spin ( "Exporting metadata..." )
}
2020-02-24 19:14:46 +03:00
err := opts . Run ( )
2021-09-29 14:11:24 +03:00
ec . Spinner . Stop ( )
2019-04-03 08:52:03 +03:00
if err != nil {
2022-11-14 10:28:23 +03:00
return errors . E ( op , err )
2019-04-03 08:52:03 +03:00
}
2021-09-29 14:11:24 +03:00
if len ( opts . output ) == 0 {
ec . Logger . Info ( "Metadata exported" )
}
2019-04-03 08:52:03 +03:00
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
}
2021-05-14 22:09:01 +03:00
f := metadataExportCmd . Flags ( )
2021-09-29 14:11:24 +03:00
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") ` )
2021-05-14 22:09:01 +03:00
2018-06-24 16:40:48 +03:00
return metadataExportCmd
}
2020-02-24 19:14:46 +03:00
type MetadataExportOptions struct {
2018-06-24 16:40:48 +03:00
EC * cli . ExecutionContext
2021-05-14 22:09:01 +03:00
output string
2018-06-24 16:40:48 +03:00
}
2020-02-24 19:14:46 +03:00
func ( o * MetadataExportOptions ) Run ( ) error {
2022-11-14 10:28:23 +03:00
var op errors . Op = "commands.MetadataExportOptions.Run"
2021-05-14 22:09:01 +03:00
if len ( o . output ) != 0 {
2022-11-14 10:28:23 +03:00
if err := getMetadataFromServerAndWriteToStdoutByFormat ( o . EC , rawOutputFormat ( o . output ) ) ; err != nil {
return errors . E ( op , err )
}
return nil
2021-05-14 22:09:01 +03:00
}
2022-11-14 10:28:23 +03:00
if err := getMetadataModeHandler ( o . EC . MetadataMode ) . Export ( o ) ; err != nil {
return errors . E ( op , err )
}
return nil
2021-05-14 22:09:01 +03:00
}
func getMetadataFromServerAndWriteToStdoutByFormat ( ec * cli . ExecutionContext , format rawOutputFormat ) error {
2022-11-14 10:28:23 +03:00
var op errors . Op = "commands.getMetadataFromServerAndWriteToStdoutByFormat"
2021-05-14 22:09:01 +03:00
metadataReader , err := cli . GetCommonMetadataOps ( ec ) . ExportMetadata ( )
if err != nil {
2022-11-14 10:28:23 +03:00
return errors . E ( op , fmt . Errorf ( "failed to export metadata: %w" , err ) )
2021-05-14 22:09:01 +03:00
}
jsonMetadata , err := ioutil . ReadAll ( metadataReader )
if err != nil {
2022-11-14 10:28:23 +03:00
return errors . E ( op , fmt . Errorf ( "reading metadata failed: %w" , err ) )
}
if err := writeByOutputFormat ( ec . Stdout , jsonMetadata , format ) ; err != nil {
return errors . E ( op , err )
2021-05-14 22:09:01 +03:00
}
2022-11-14 10:28:23 +03:00
return nil
2018-06-24 16:40:48 +03:00
}