2019-10-30 16:54:22 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2021-07-23 12:49:44 +03:00
|
|
|
"fmt"
|
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/projectmetadata"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/aryann/difflib"
|
|
|
|
|
|
|
|
"github.com/hasura/graphql-engine/cli/v2"
|
|
|
|
"github.com/hexops/gotextdiff"
|
|
|
|
"github.com/hexops/gotextdiff/myers"
|
|
|
|
"github.com/hexops/gotextdiff/span"
|
|
|
|
"github.com/mgutz/ansi"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"gopkg.in/yaml.v2"
|
2019-10-30 16:54:22 +03:00
|
|
|
)
|
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
type MetadataDiffOptions struct {
|
2021-06-18 09:24:16 +03:00
|
|
|
EC *cli.ExecutionContext
|
|
|
|
Output io.Writer
|
|
|
|
Args []string
|
|
|
|
DiffType string
|
|
|
|
DisableColor bool
|
2020-02-24 19:14:46 +03:00
|
|
|
// two Metadata to diff, 2nd is server if it's empty
|
|
|
|
Metadata [2]string
|
2019-10-30 16:54:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func newMetadataDiffCmd(ec *cli.ExecutionContext) *cobra.Command {
|
2020-02-24 19:14:46 +03:00
|
|
|
opts := &MetadataDiffOptions{
|
2019-10-30 16:54:22 +03:00
|
|
|
EC: ec,
|
2021-07-16 08:26:00 +03:00
|
|
|
Output: ec.Stdout,
|
2019-10-30 16:54:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
metadataDiffCmd := &cobra.Command{
|
|
|
|
Use: "diff [file1] [file2]",
|
|
|
|
Short: "(PREVIEW) Show a highlighted diff of Hasura metadata",
|
|
|
|
Long: `(PREVIEW) Show changes between two different sets of Hasura metadata.
|
2021-03-15 18:40:52 +03:00
|
|
|
By default, it shows changes between the exported metadata file and server metadata`,
|
2019-10-30 16:54:22 +03:00
|
|
|
Example: ` # NOTE: This command is in preview, usage and diff format may change.
|
|
|
|
|
|
|
|
# Show changes between server metadata and the exported metadata file:
|
|
|
|
hasura metadata diff
|
|
|
|
|
|
|
|
# Show changes between server metadata and that in local_metadata.yaml:
|
|
|
|
hasura metadata diff local_metadata.yaml
|
|
|
|
|
|
|
|
# Show changes between metadata from metadata.yaml and metadata_old.yaml:
|
2019-12-12 08:16:36 +03:00
|
|
|
hasura metadata diff metadata.yaml metadata_old.yaml
|
|
|
|
|
2021-03-15 18:40:52 +03:00
|
|
|
# Apply admin secret for Hasura GraphQL engine:
|
2019-12-12 08:16:36 +03:00
|
|
|
hasura metadata diff --admin-secret "<admin-secret>"
|
|
|
|
|
2021-06-17 13:15:22 +03:00
|
|
|
# For unified diff as the default diff just outputs only the difference:
|
|
|
|
hasura metadata diff --type "unified-common"
|
|
|
|
|
2019-12-12 08:16:36 +03:00
|
|
|
# Diff metadata on a different Hasura instance:
|
|
|
|
hasura metadata diff --endpoint "<endpoint>"`,
|
2019-10-30 16:54:22 +03:00
|
|
|
Args: cobra.MaximumNArgs(2),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-02-24 19:14:46 +03:00
|
|
|
opts.Args = args
|
|
|
|
return opts.Run()
|
2019-10-30 16:54:22 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-06-17 13:15:22 +03:00
|
|
|
f := metadataDiffCmd.Flags()
|
|
|
|
|
|
|
|
f.StringVar(&opts.DiffType, "type", "default", fmt.Sprintf(`specify a type of diff [allowed values: %v]`, DifftypeUnifiedCommon))
|
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
return metadataDiffCmd
|
|
|
|
}
|
2019-10-30 16:54:22 +03:00
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
func (o *MetadataDiffOptions) runv2(args []string) error {
|
|
|
|
messageFormat := "Showing diff between %s and %s..."
|
|
|
|
message := ""
|
2021-07-23 12:49:44 +03:00
|
|
|
metadataHandler := projectmetadata.NewHandlerFromEC(o.EC)
|
2021-07-16 15:58:09 +03:00
|
|
|
from := "project"
|
|
|
|
to := "server"
|
2020-02-24 19:14:46 +03:00
|
|
|
switch len(args) {
|
|
|
|
case 0:
|
|
|
|
o.Metadata[0] = o.EC.MetadataDir
|
2021-07-16 15:58:09 +03:00
|
|
|
from = "project"
|
2020-02-24 19:14:46 +03:00
|
|
|
case 1:
|
|
|
|
// 1 arg, diff given directory and the metadata on server
|
|
|
|
err := checkDir(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.Metadata[0] = args[0]
|
2021-07-16 15:58:09 +03:00
|
|
|
from = o.Metadata[0]
|
2020-02-24 19:14:46 +03:00
|
|
|
case 2:
|
|
|
|
err := checkDir(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.Metadata[0] = args[0]
|
2021-07-16 15:58:09 +03:00
|
|
|
from = o.Metadata[0]
|
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
err = checkDir(args[1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.Metadata[1] = args[1]
|
2021-07-16 15:58:09 +03:00
|
|
|
to = o.Metadata[1]
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
2021-07-16 15:58:09 +03:00
|
|
|
message = fmt.Sprintf(messageFormat, from, to)
|
2020-02-24 19:14:46 +03:00
|
|
|
o.EC.Logger.Info(message)
|
|
|
|
var oldYaml, newYaml []byte
|
|
|
|
if o.Metadata[1] == "" {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "*")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
2021-07-23 12:49:44 +03:00
|
|
|
metadataHandler.SetMetadataObjects(projectmetadata.GetMetadataObjectsWithDir(o.EC, tmpDir))
|
2021-04-01 08:13:24 +03:00
|
|
|
var files map[string][]byte
|
|
|
|
files, err = metadataHandler.ExportMetadata()
|
2020-02-24 19:14:46 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-01 08:13:24 +03:00
|
|
|
err = metadataHandler.WriteMetadata(files)
|
2020-02-24 19:14:46 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2021-07-23 12:49:44 +03:00
|
|
|
metadataHandler.SetMetadataObjects(projectmetadata.GetMetadataObjectsWithDir(o.EC, o.Metadata[1]))
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
2019-10-30 16:54:22 +03:00
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
// build server metadata
|
2021-04-01 08:13:24 +03:00
|
|
|
serverMeta, err := metadataHandler.BuildMetadata()
|
2020-02-24 19:14:46 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newYaml, err = yaml.Marshal(serverMeta)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "cannot unmarshall server metadata")
|
|
|
|
}
|
|
|
|
|
|
|
|
// build local metadata
|
2021-07-23 12:49:44 +03:00
|
|
|
metadataHandler.SetMetadataObjects(projectmetadata.GetMetadataObjectsWithDir(o.EC, o.Metadata[0]))
|
2021-04-01 08:13:24 +03:00
|
|
|
localMeta, err := metadataHandler.BuildMetadata()
|
2020-02-24 19:14:46 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
oldYaml, err = yaml.Marshal(localMeta)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "cannot unmarshal local metadata")
|
|
|
|
}
|
|
|
|
|
2021-07-16 15:58:09 +03:00
|
|
|
err = printDiff(string(oldYaml), string(newYaml), from, to, o.Output, o.DiffType, o.DisableColor)
|
2021-06-17 13:15:22 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-24 19:14:46 +03:00
|
|
|
return nil
|
2019-10-30 16:54:22 +03:00
|
|
|
}
|
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
func (o *MetadataDiffOptions) Run() error {
|
2021-01-18 20:11:05 +03:00
|
|
|
if o.EC.Config.Version >= cli.V2 && o.EC.MetadataDir != "" {
|
2020-02-24 19:14:46 +03:00
|
|
|
return o.runv2(o.Args)
|
2021-04-01 08:13:24 +03:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("metadata diff for config %d not supported", o.EC.Config.Version)
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
|
|
|
}
|
2021-06-18 09:24:16 +03:00
|
|
|
|
2021-06-17 13:15:22 +03:00
|
|
|
type Difftype string
|
2021-06-18 09:24:16 +03:00
|
|
|
|
2021-06-17 13:15:22 +03:00
|
|
|
const DifftypeUnifiedCommon Difftype = "unified-common"
|
|
|
|
|
2021-07-16 15:58:09 +03:00
|
|
|
func printDiff(before, after, from, to string, writer io.Writer, difftype string, disableColor bool) error {
|
2021-06-18 09:24:16 +03:00
|
|
|
diffType := Difftype(difftype)
|
2021-06-17 13:15:22 +03:00
|
|
|
switch diffType {
|
|
|
|
case DifftypeUnifiedCommon:
|
2021-07-16 15:58:09 +03:00
|
|
|
printDiffv1(before, after, writer)
|
2021-06-17 13:15:22 +03:00
|
|
|
default:
|
2021-07-16 15:58:09 +03:00
|
|
|
return printDiffv2(before, after, from, to, writer, disableColor)
|
2021-06-17 13:15:22 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-24 19:14:46 +03:00
|
|
|
|
2021-07-16 15:58:09 +03:00
|
|
|
func printDiffv2(before, after, from, to string, writer io.Writer, disableColor bool) error {
|
2021-06-17 13:15:22 +03:00
|
|
|
edits := myers.ComputeEdits(span.URIFromPath("a.txt"), before, after)
|
2021-07-16 15:58:09 +03:00
|
|
|
text := fmt.Sprint(gotextdiff.ToUnified(from, to, before, edits))
|
2021-06-18 09:24:16 +03:00
|
|
|
makeDiffLine := func(line, color string) string {
|
|
|
|
if disableColor {
|
|
|
|
return line
|
|
|
|
}
|
|
|
|
return ansi.Color(line, color)
|
|
|
|
}
|
2021-06-17 13:15:22 +03:00
|
|
|
lines := strings.Split(text, "\n")
|
|
|
|
for _, line := range lines {
|
|
|
|
if line == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if (string)(line[0]) == "-" {
|
2021-07-16 15:58:09 +03:00
|
|
|
fmt.Fprintf(writer, "%s\n", makeDiffLine(line, "red"))
|
2021-06-17 13:15:22 +03:00
|
|
|
} else if (string)(line[0]) == "+" {
|
2021-07-16 15:58:09 +03:00
|
|
|
fmt.Fprintf(writer, "%s\n", makeDiffLine(line, "yellow"))
|
2021-06-17 13:15:22 +03:00
|
|
|
} else if (string)(line[0]) == "@" {
|
2021-07-16 15:58:09 +03:00
|
|
|
fmt.Fprintf(writer, "%s\n", makeDiffLine(line, "cyan"))
|
2019-10-30 16:54:22 +03:00
|
|
|
}
|
|
|
|
}
|
2021-06-17 13:15:22 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func printDiffv1(before, after string, to io.Writer) {
|
2021-06-18 09:24:16 +03:00
|
|
|
diffs := difflib.Diff(strings.Split(before, "\n"), strings.Split(after, "\n"))
|
|
|
|
|
|
|
|
for _, diff := range diffs {
|
|
|
|
text := diff.Payload
|
|
|
|
switch diff.Delta {
|
|
|
|
case difflib.RightOnly:
|
|
|
|
fmt.Fprintf(to, "%s\n", ansi.Color(text, "green"))
|
|
|
|
case difflib.LeftOnly:
|
|
|
|
fmt.Fprintf(to, "%s\n", ansi.Color(text, "red"))
|
|
|
|
case difflib.Common:
|
|
|
|
fmt.Fprintf(to, "%s\n", text)
|
|
|
|
}
|
|
|
|
}
|
2019-10-30 16:54:22 +03:00
|
|
|
}
|
2020-02-24 19:14:46 +03:00
|
|
|
|
|
|
|
func checkDir(path string) error {
|
|
|
|
file, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !file.IsDir() {
|
|
|
|
return fmt.Errorf("metadata diff only works with folder but got file %s", path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|