2018-06-24 16:40:48 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hasura/graphql-engine/cli"
|
2018-07-09 16:47:38 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/migrate"
|
|
|
|
"github.com/pkg/errors"
|
2018-06-24 16:40:48 +03:00
|
|
|
"github.com/spf13/cobra"
|
2020-02-24 19:14:46 +03:00
|
|
|
"github.com/spf13/viper"
|
2018-06-24 16:40:48 +03:00
|
|
|
)
|
|
|
|
|
2019-12-25 11:33:06 +03:00
|
|
|
// NewMetadataCmd returns the metadata command
|
2018-06-24 16:40:48 +03:00
|
|
|
func NewMetadataCmd(ec *cli.ExecutionContext) *cobra.Command {
|
2020-02-24 19:14:46 +03:00
|
|
|
v := viper.New()
|
2018-06-24 16:47:01 +03:00
|
|
|
metadataCmd := &cobra.Command{
|
2020-02-24 19:14:46 +03:00
|
|
|
Use: "metadata",
|
|
|
|
Aliases: []string{"md"},
|
|
|
|
Short: "Manage Hasura GraphQL Engine metadata saved in the database",
|
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
2020-03-26 06:08:54 +03:00
|
|
|
cmd.Root().PersistentPreRun(cmd, args)
|
2020-03-03 10:06:59 +03:00
|
|
|
ec.Viper = v
|
2020-02-24 19:14:46 +03:00
|
|
|
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(
|
2019-10-30 16:54:22 +03:00
|
|
|
newMetadataDiffCmd(ec),
|
2018-06-28 11:36:25 +03:00
|
|
|
newMetadataExportCmd(ec),
|
2019-03-29 08:14:56 +03:00
|
|
|
newMetadataClearCmd(ec),
|
2018-10-16 09:25:30 +03:00
|
|
|
newMetadataReloadCmd(ec),
|
2018-06-28 11:36:25 +03:00
|
|
|
newMetadataApplyCmd(ec),
|
2019-12-25 11:33:06 +03:00
|
|
|
newMetadataInconsistencyCmd(ec),
|
2018-06-24 16:40:48 +03:00
|
|
|
)
|
2020-02-24 19:14:46 +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
|
|
|
}
|
2018-07-09 16:47:38 +03:00
|
|
|
|
2019-03-20 08:10:06 +03:00
|
|
|
func executeMetadata(cmd string, t *migrate.Migrate, ec *cli.ExecutionContext) error {
|
2018-07-09 16:47:38 +03:00
|
|
|
switch cmd {
|
|
|
|
case "export":
|
2020-02-24 19:14:46 +03:00
|
|
|
files, err := t.ExportMetadata()
|
2018-07-09 16:47:38 +03:00
|
|
|
if err != nil {
|
2020-02-24 19:14:46 +03:00
|
|
|
return errors.Wrap(err, "cannot export metadata from server")
|
2018-07-09 16:47:38 +03:00
|
|
|
}
|
2020-02-24 19:14:46 +03:00
|
|
|
err = t.WriteMetadata(files)
|
2018-07-09 16:47:38 +03:00
|
|
|
if err != nil {
|
2020-02-24 19:14:46 +03:00
|
|
|
return errors.Wrap(err, "cannot write metadata")
|
2018-07-09 16:47:38 +03:00
|
|
|
}
|
2019-03-29 08:14:56 +03:00
|
|
|
case "clear":
|
2018-07-09 16:47:38 +03:00
|
|
|
err := t.ResetMetadata()
|
|
|
|
if err != nil {
|
2019-03-29 08:14:56 +03:00
|
|
|
return errors.Wrap(err, "cannot clear Metadata")
|
2018-07-09 16:47:38 +03:00
|
|
|
}
|
2018-10-16 09:25:30 +03:00
|
|
|
case "reload":
|
|
|
|
err := t.ReloadMetadata()
|
|
|
|
if err != nil {
|
2019-03-20 08:10:06 +03:00
|
|
|
return errors.Wrap(err, "cannot reload Metadata")
|
2018-10-16 09:25:30 +03:00
|
|
|
}
|
2018-07-09 16:47:38 +03:00
|
|
|
case "apply":
|
2020-02-24 19:14:46 +03:00
|
|
|
err := t.ApplyMetadata()
|
2018-07-09 16:47:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "cannot apply metadata on the database")
|
|
|
|
}
|
2019-03-20 08:10:06 +03:00
|
|
|
return nil
|
2018-07-09 16:47:38 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|