2018-06-24 16:40:48 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2021-05-14 22:09:01 +03:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2021-04-16 14:50:54 +03:00
|
|
|
"fmt"
|
2021-05-14 22:09:01 +03:00
|
|
|
"io"
|
2021-04-16 14:50:54 +03:00
|
|
|
|
2021-05-14 22:09:01 +03:00
|
|
|
"github.com/goccy/go-yaml"
|
2021-06-16 14:44:15 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2"
|
2022-11-08 13:54:33 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
|
2022-03-10 11:12:55 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/metadatautil"
|
2021-06-16 14:44:15 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/scripts"
|
|
|
|
"github.com/hasura/graphql-engine/cli/v2/util"
|
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
|
|
|
)
|
|
|
|
|
2021-05-14 22:09:01 +03:00
|
|
|
type rawOutputFormat string
|
|
|
|
|
|
|
|
const rawOutputFormatJSON rawOutputFormat = "json"
|
|
|
|
const rawOutputFormatYAML rawOutputFormat = "yaml"
|
|
|
|
|
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"},
|
2023-01-11 11:37:47 +03:00
|
|
|
Short: "Manage Hasura GraphQL Engine Metadata saved in the database",
|
2023-01-09 07:25:53 +03:00
|
|
|
Long: `This command allows you to manage the Hasura GraphQL Engine Metadata saved in the database via a collection of flags and subcommands.
|
|
|
|
|
|
|
|
Further reading:
|
|
|
|
- https://hasura.io/docs/latest/migrations-metadata-seeds/index/
|
|
|
|
`,
|
2020-02-24 19:14:46 +03:00
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
2022-11-08 13:54:33 +03:00
|
|
|
op := genOpName(cmd, "PersistentPreRunE")
|
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 {
|
2022-11-08 13:54:33 +03:00
|
|
|
return errors.E(op, err)
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
2021-02-17 07:20:19 +03:00
|
|
|
err = ec.Validate()
|
|
|
|
if err != nil {
|
2022-11-08 13:54:33 +03:00
|
|
|
return errors.E(op, err)
|
2021-02-17 07:20:19 +03:00
|
|
|
}
|
2022-11-08 13:54:33 +03:00
|
|
|
if err := scripts.CheckIfUpdateToConfigV3IsRequired(ec); err != nil {
|
|
|
|
return errors.E(op, err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-02-24 19:14:46 +03:00
|
|
|
},
|
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
|
|
|
|
2020-04-08 14:55:42 +03:00
|
|
|
f := metadataCmd.PersistentFlags()
|
2020-02-24 19:14:46 +03:00
|
|
|
|
2023-01-11 11:37:47 +03:00
|
|
|
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")
|
2021-10-13 17:38:07 +03:00
|
|
|
if err := f.MarkDeprecated("access-key", "use --admin-secret instead"); err != nil {
|
|
|
|
ec.Logger.WithError(err).Errorf("error while using a dependency library")
|
|
|
|
}
|
2020-04-28 14:59:57 +03:00
|
|
|
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")
|
2020-04-08 14:55:42 +03:00
|
|
|
|
|
|
|
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"))
|
2020-04-28 14:59:57 +03:00
|
|
|
util.BindPFlag(v, "insecure_skip_tls_verify", f.Lookup("insecure-skip-tls-verify"))
|
|
|
|
util.BindPFlag(v, "certificate_authority", f.Lookup("certificate-authority"))
|
2020-02-24 19:14:46 +03:00
|
|
|
|
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
|
|
|
|
2021-05-14 22:09:01 +03:00
|
|
|
func writeByOutputFormat(w io.Writer, b []byte, format rawOutputFormat) error {
|
2022-11-08 13:54:33 +03:00
|
|
|
var op errors.Op = "commands.writeByOutputFormat"
|
2021-05-14 22:09:01 +03:00
|
|
|
switch format {
|
|
|
|
case rawOutputFormatJSON:
|
|
|
|
out := new(bytes.Buffer)
|
|
|
|
err := json.Indent(out, b, "", " ")
|
2018-07-09 16:47:38 +03:00
|
|
|
if err != nil {
|
2022-11-08 13:54:33 +03:00
|
|
|
return errors.E(op, err)
|
2018-07-09 16:47:38 +03:00
|
|
|
}
|
2021-10-13 17:38:07 +03:00
|
|
|
_, err = io.Copy(w, out)
|
|
|
|
if err != nil {
|
2022-11-08 13:54:33 +03:00
|
|
|
return errors.E(op, fmt.Errorf("writing output failed: %w", err))
|
2021-10-13 17:38:07 +03:00
|
|
|
}
|
2021-05-14 22:09:01 +03:00
|
|
|
case rawOutputFormatYAML:
|
2022-03-10 11:12:55 +03:00
|
|
|
o, err := metadatautil.JSONToYAML(b)
|
2018-07-09 16:47:38 +03:00
|
|
|
if err != nil {
|
2022-11-08 13:54:33 +03:00
|
|
|
return errors.E(op, err)
|
2021-04-16 14:50:54 +03:00
|
|
|
}
|
2021-10-13 17:38:07 +03:00
|
|
|
_, err = io.Copy(w, bytes.NewReader(o))
|
|
|
|
if err != nil {
|
2022-11-08 13:54:33 +03:00
|
|
|
return errors.E(op, fmt.Errorf("writing output failed: %w", err))
|
2021-10-13 17:38:07 +03:00
|
|
|
}
|
2021-05-14 22:09:01 +03:00
|
|
|
default:
|
2022-11-08 13:54:33 +03:00
|
|
|
return errors.E(op, fmt.Errorf("output format '%v' is not supported. supported formats: %v, %v", format, rawOutputFormatJSON, rawOutputFormatYAML))
|
2018-07-09 16:47:38 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-05-14 22:09:01 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|