Repository navigation
Expand file tree
/
Copy pathmetadata_diff.go
More file actions
198 lines (168 loc) · 5.85 KB
/
Copy pathmetadata_diff.go
File metadata and controls
198 lines (168 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package commands
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"strings"
goyaml "github.com/goccy/go-yaml"
diffpkg "github.com/hasura/graphql-engine/cli/v2/internal/diff"
"github.com/hasura/graphql-engine/cli/v2/internal/projectmetadata"
"github.com/pkg/errors"
"github.com/aryann/difflib"
"github.com/hasura/graphql-engine/cli/v2"
"github.com/mgutz/ansi"
"github.com/spf13/cobra"
v2yaml "gopkg.in/yaml.v2"
)
type MetadataDiffOptions struct {
EC *cli.ExecutionContext
Output io.Writer
Args []string
DiffType string
DisableColor bool
// two Metadata to diff, 2nd is server if it's empty
Metadata [2]string
}
func newMetadataDiffCmd(ec *cli.ExecutionContext) *cobra.Command {
opts := &MetadataDiffOptions{
EC: ec,
Output: ec.Stdout,
}
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.
By default, it shows changes between the exported metadata file and server metadata`,
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
# Apply admin secret for Hasura GraphQL engine:
hasura metadata diff --admin-secret "<admin-secret>"
# Specify a diff type
hasura metadata diff --type "unified-json"
hasura metadata diff --type "json"
# Diff metadata on a different Hasura instance:
hasura metadata diff --endpoint "<endpoint>"`,
Args: cobra.MaximumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
opts.Args = args
opts.DisableColor = ec.NoColor
return opts.Run()
},
}
f := metadataDiffCmd.Flags()
f.StringVar(&opts.DiffType, "type", "", fmt.Sprintf(`specify a type of diff [allowed values: %v, %v, %v]`, DifftypeUnifiedJSON, DifftypeYAML, DifftypeJSON))
return metadataDiffCmd
}
func (o *MetadataDiffOptions) Run() error {
if o.EC.Config.Version >= cli.V2 && o.EC.MetadataDir != "" {
return getMetadataModeHandler(o.EC.MetadataMode).Diff(o)
} else {
return fmt.Errorf("metadata diff for config %d not supported", o.EC.Config.Version)
}
}
type DiffType string
const DifftypeUnifiedJSON DiffType = "unified-json"
const DifftypeYAML DiffType = "yaml"
const DifftypeJSON DiffType = "json"
const zeroDifferencesFound = "zero differences found"
type printGeneratedMetadataFileDiffOpts struct {
projectMetadataHandler *projectmetadata.Handler
// actual directory paths to project directory
fromProjectDirectory string
toProjectDirectory string
// friendly names if any to both from and to directories
// for example the diff can between the current project directory and server
fromFriendlyName string
toFriendlyName string
disableColor bool
diffType DiffType
metadataMode cli.MetadataMode
writer io.Writer
ec *cli.ExecutionContext
}
func printGeneratedMetadataFileDiffBetweenProjectDirectories(opts printGeneratedMetadataFileDiffOpts) error {
// build server metadata
opts.projectMetadataHandler.SetMetadataObjects(projectmetadata.GetMetadataObjectsWithDir(opts.ec, opts.toProjectDirectory))
serverMeta, err := opts.projectMetadataHandler.BuildMetadata()
if err != nil {
return err
}
newYaml, err := v2yaml.Marshal(serverMeta)
if err != nil {
return errors.Wrap(err, "cannot unmarshall server metadata")
}
opts.projectMetadataHandler.SetMetadataObjects(projectmetadata.GetMetadataObjectsWithDir(opts.ec, opts.fromProjectDirectory))
localMeta, err := opts.projectMetadataHandler.BuildMetadata()
if err != nil {
return err
}
oldYaml, err := v2yaml.Marshal(localMeta)
if err != nil {
return errors.Wrap(err, "cannot unmarshal local metadata")
}
switch opts.diffType {
case DifftypeJSON:
newJson, err := goyaml.YAMLToJSON(newYaml)
if err != nil {
return fmt.Errorf("cannot unmarshal local metadata to json: %w", err)
}
oldJson, err := goyaml.YAMLToJSON(oldYaml)
if err != nil {
return fmt.Errorf("cannot unmarshal server metadata to json: %w", err)
}
var newJsonBuf, oldJsonBuf bytes.Buffer
err = json.Indent(&newJsonBuf, newJson, "", " ")
if err != nil {
return fmt.Errorf("cannot indent server localmetadata to json: %w", err)
}
err = json.Indent(&oldJsonBuf, oldJson, "", " ")
if err != nil {
return fmt.Errorf("cannot indent server metadata to json: %w", err)
}
return printMyersDiff(string(newYaml), string(oldYaml), opts.toFriendlyName, opts.fromFriendlyName, opts.writer, opts.disableColor)
case DifftypeYAML:
return printMyersDiff(string(newYaml), string(oldYaml), opts.toFriendlyName, opts.fromFriendlyName, opts.writer, opts.disableColor)
case DifftypeUnifiedJSON:
printUnifiedJSONDiff(string(newYaml), string(oldYaml), opts.writer)
}
return nil
}
func printMyersDiff(before, after, from, to string, writer io.Writer, disableColor bool) error {
fmt.Fprintf(writer, "- %s\n", diffpkg.MakeDiffLine(from, "red", disableColor))
fmt.Fprintf(writer, "+ %s\n", diffpkg.MakeDiffLine(to, "green", disableColor))
count, err := diffpkg.MyersDiff(before, after, from, to, writer, disableColor)
if err != nil {
return err
}
if count == 0 {
fmt.Fprintln(writer, zeroDifferencesFound)
}
return nil
}
func printUnifiedJSONDiff(before, after string, to io.Writer) {
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)
}
}
}
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
}