graphql-engine/cli/commands/metadata.go

80 lines
2.3 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/hasura/graphql-engine/cli/migrate"
"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
)
// NewMetadataCmd returns the metadata command
2018-06-24 16:40:48 +03:00
func NewMetadataCmd(ec *cli.ExecutionContext) *cobra.Command {
v := viper.New()
2018-06-24 16:47:01 +03:00
metadataCmd := &cobra.Command{
Use: "metadata",
Aliases: []string{"md"},
Short: "Manage Hasura GraphQL Engine metadata saved in the database",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
cmd.Root().PersistentPreRun(cmd, args)
ec.Viper = v
err := ec.Prepare()
if err != nil {
return err
}
return ec.Validate()
},
2018-06-24 16:40:48 +03:00
SilenceUsage: true,
}
2018-06-24 16:47:01 +03:00
metadataCmd.AddCommand(
newMetadataDiffCmd(ec),
newMetadataExportCmd(ec),
newMetadataClearCmd(ec),
newMetadataReloadCmd(ec),
newMetadataApplyCmd(ec),
newMetadataInconsistencyCmd(ec),
2018-06-24 16:40:48 +03:00
)
metadataCmd.PersistentFlags().String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine")
metadataCmd.PersistentFlags().String("admin-secret", "", "admin secret for Hasura GraphQL Engine")
metadataCmd.PersistentFlags().String("access-key", "", "access key for Hasura GraphQL Engine")
metadataCmd.PersistentFlags().MarkDeprecated("access-key", "use --admin-secret instead")
v.BindPFlag("endpoint", metadataCmd.PersistentFlags().Lookup("endpoint"))
v.BindPFlag("admin_secret", metadataCmd.PersistentFlags().Lookup("admin-secret"))
v.BindPFlag("access_key", metadataCmd.PersistentFlags().Lookup("access-key"))
2018-06-24 16:47:01 +03:00
return metadataCmd
2018-06-24 16:40:48 +03:00
}
func executeMetadata(cmd string, t *migrate.Migrate, ec *cli.ExecutionContext) error {
switch cmd {
case "export":
files, err := t.ExportMetadata()
if err != nil {
return errors.Wrap(err, "cannot export metadata from server")
}
err = t.WriteMetadata(files)
if err != nil {
return errors.Wrap(err, "cannot write metadata")
}
case "clear":
err := t.ResetMetadata()
if err != nil {
return errors.Wrap(err, "cannot clear Metadata")
}
case "reload":
err := t.ReloadMetadata()
if err != nil {
return errors.Wrap(err, "cannot reload Metadata")
}
case "apply":
err := t.ApplyMetadata()
if err != nil {
return errors.Wrap(err, "cannot apply metadata on the database")
}
return nil
}
return nil
}