graphql-engine/cli/commands/metadata.go

87 lines
2.1 KiB
Go
Raw Normal View History

2018-06-24 16:40:48 +03:00
package commands
import (
"encoding/json"
"io/ioutil"
"github.com/ghodss/yaml"
2018-06-24 16:40:48 +03:00
"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"
2018-06-24 16:47:01 +03:00
"github.com/spf13/viper"
2018-06-24 16:40:48 +03:00
)
func NewMetadataCmd(ec *cli.ExecutionContext) *cobra.Command {
2018-06-24 16:47:01 +03:00
v := viper.New()
metadataCmd := &cobra.Command{
2018-06-24 16:40:48 +03:00
Use: "metadata",
2018-09-20 04:30:37 +03:00
Short: "Manage Hasura GraphQL Engine metdata saved in the database",
2018-06-24 16:40:48 +03:00
SilenceUsage: true,
2018-06-24 16:47:01 +03:00
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
ec.Viper = v
2018-06-24 16:40:48 +03:00
return ec.Validate()
},
}
2018-06-24 16:47:01 +03:00
metadataCmd.AddCommand(
newMetadataExportCmd(ec),
newMetadataResetCmd(ec),
newMetadataApplyCmd(ec),
2018-06-24 16:40:48 +03:00
)
2018-06-24 16:47:01 +03:00
f := metadataCmd.PersistentFlags()
f.String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine")
f.String("access-key", "", "access key for Hasura GraphQL Engine")
// need to create a new viper because https://github.com/spf13/viper/issues/233
v.BindPFlag("endpoint", f.Lookup("endpoint"))
v.BindPFlag("access_key", f.Lookup("access-key"))
return metadataCmd
2018-06-24 16:40:48 +03:00
}
func executeMetadata(cmd string, t *migrate.Migrate, metadataPath string) error {
switch cmd {
case "export":
metaData, err := t.ExportMetadata()
if err != nil {
return errors.Wrap(err, "Cannot export metadata")
}
t, err := json.Marshal(metaData)
if err != nil {
return errors.Wrap(err, "Cannot Marshal metadata")
}
data, err := yaml.JSONToYAML(t)
if err != nil {
return err
}
err = ioutil.WriteFile(metadataPath, data, 0644)
if err != nil {
return errors.Wrap(err, "cannot save metadata")
}
case "reset":
err := t.ResetMetadata()
if err != nil {
return errors.Wrap(err, "Cannot reset Metadata")
}
case "apply":
data, err := ioutil.ReadFile(metadataPath)
if err != nil {
return errors.Wrap(err, "cannot read metadata file")
}
var q interface{}
err = yaml.Unmarshal(data, &q)
if err != nil {
return errors.Wrap(err, "cannot parse metadata file")
}
err = t.ApplyMetadata(q)
if err != nil {
return errors.Wrap(err, "cannot apply metadata on the database")
}
}
return nil
}