graphql-engine/cli/commands/metadata.go
Kali Vara Purushotham Santhati 489835eb36 cli: metadata apply,export enhancements
- modified the `dry-run` flag of metadata apply command.
- added new flag `o` which takes "json" or "yaml" as parameters for metadata apply command.
- added new flag `o` which takes "json" or "yaml" as parameters for metadata export command. It outputs the metadata from server to stdout in form of  json or yaml and won't change the project's metadata.
- added deprecation warnings for `--from-file` flag
- added info message for describing change of behavior of `--dry-run` flag
- v3 metadata objects like `rest_endpoints` was also added to list of metadata objects in config v2 (fix)
- the order in which metadata objects are appended to metadata objects list matter when using `--dry-run` flag, refactored this order to match server metadata.
- `metadata apply` command can now accept json/yaml metadata from io pipe
- config v3 `metadata apply` didn't show any information to user when applied metadata is inconsistent, this is addressed.
- removed `github.com/ghodss/yaml` dependency from repo
- version metadata object was added twice during intialization (fix)

Co-authored-by: Aravind K P <8335904+scriptonist@users.noreply.github.com>
GitOrigin-RevId: 2316f519eb40645efd86ffee2a85d3c90543ec17
2021-05-14 19:09:59 +00:00

101 lines
2.8 KiB
Go

package commands
import (
"bytes"
"encoding/json"
"fmt"
"io"
"github.com/goccy/go-yaml"
"github.com/hasura/graphql-engine/cli"
"github.com/hasura/graphql-engine/cli/internal/scripts"
"github.com/hasura/graphql-engine/cli/util"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type rawOutputFormat string
const rawOutputFormatJSON rawOutputFormat = "json"
const rawOutputFormatYAML rawOutputFormat = "yaml"
// NewMetadataCmd returns the metadata command
func NewMetadataCmd(ec *cli.ExecutionContext) *cobra.Command {
v := viper.New()
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
}
err = ec.Validate()
if err != nil {
return err
}
return scripts.CheckIfUpdateToConfigV3IsRequired(ec)
},
SilenceUsage: true,
}
metadataCmd.AddCommand(
newMetadataDiffCmd(ec),
newMetadataExportCmd(ec),
newMetadataClearCmd(ec),
newMetadataReloadCmd(ec),
newMetadataApplyCmd(ec),
newMetadataInconsistencyCmd(ec),
)
f := metadataCmd.PersistentFlags()
f.String("endpoint", "", "http(s) endpoint for Hasura GraphQL engine")
f.String("admin-secret", "", "admin secret for Hasura GraphQL engine")
f.String("access-key", "", "access key for Hasura GraphQL engine")
f.MarkDeprecated("access-key", "use --admin-secret instead")
f.Bool("insecure-skip-tls-verify", false, "skip TLS verification and disable cert checking (default: false)")
f.String("certificate-authority", "", "path to a cert file for the certificate authority")
util.BindPFlag(v, "endpoint", f.Lookup("endpoint"))
util.BindPFlag(v, "admin_secret", f.Lookup("admin-secret"))
util.BindPFlag(v, "access_key", f.Lookup("access-key"))
util.BindPFlag(v, "insecure_skip_tls_verify", f.Lookup("insecure-skip-tls-verify"))
util.BindPFlag(v, "certificate_authority", f.Lookup("certificate-authority"))
return metadataCmd
}
func writeByOutputFormat(w io.Writer, b []byte, format rawOutputFormat) error {
switch format {
case rawOutputFormatJSON:
out := new(bytes.Buffer)
err := json.Indent(out, b, "", " ")
if err != nil {
return err
}
io.Copy(w, out)
case rawOutputFormatYAML:
o, err := yaml.JSONToYAML(b)
if err != nil {
return err
}
io.Copy(w, bytes.NewReader(o))
default:
return fmt.Errorf("output format '%v' is not supported. supported formats: %v, %v", format, rawOutputFormatJSON, rawOutputFormatYAML)
}
return nil
}
func isJSON(str []byte) bool {
var js json.RawMessage
return json.Unmarshal(str, &js) == nil
}
func isYAML(str []byte) bool {
var y yaml.MapSlice
return yaml.Unmarshal(str, &y) == nil
}