forked from hasura/graphql-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.go
More file actions
73 lines (64 loc) · 1.9 KB
/
diff.go
File metadata and controls
73 lines (64 loc) · 1.9 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
package diff
import (
"fmt"
"io"
"path/filepath"
"strings"
"github.com/mgutz/ansi"
"github.com/hexops/gotextdiff"
"github.com/hexops/gotextdiff/myers"
"github.com/hexops/gotextdiff/span"
"github.com/gonvenience/ytbx"
"github.com/homeport/dyff/pkg/dyff"
)
func YamlDiff(fromFile, toFile ytbx.InputFile, writer io.Writer, fileName string) (int, error) {
report, err := dyff.CompareInputFiles(fromFile, toFile, dyff.IgnoreOrderChanges(true))
if err != nil {
return -1, fmt.Errorf("error while getting diff: %w", err)
}
reportWriter := &dyff.HumanReport{
Report: report,
DoNotInspectCerts: false,
NoTableStyle: false,
OmitHeader: true,
UseGoPatchPaths: false,
}
if len(report.Diffs) != 0 {
fmt.Fprintf(writer, "%s\n", fileName)
err = reportWriter.WriteReport(writer)
if err != nil {
return -1, fmt.Errorf("error while printing diff: %w", err)
}
}
return len(report.Diffs), nil
}
func IsYAMLFile(filename string) bool {
return filepath.Ext(filename) == ".yaml" || filepath.Ext(filename) == ".yml"
}
func MyersDiff(before, after, from, to string, writer io.Writer, disableColor bool) (int, error) {
edits := myers.ComputeEdits(span.URIFromPath("a.txt"), before, after)
text := fmt.Sprint(gotextdiff.ToUnified(from, to, before, edits))
lines := strings.Split(text, "\n")
if len(lines) <= 2 {
return 0, nil
}
for _, line := range lines[2:] {
if line == "" {
break
}
if (string)(line[0]) == "-" {
fmt.Fprintf(writer, "%s\n", MakeDiffLine(line, "red", disableColor))
} else if (string)(line[0]) == "+" {
fmt.Fprintf(writer, "%s\n", MakeDiffLine(line, "green", disableColor))
} else if (string)(line[0]) == "@" {
fmt.Fprintf(writer, "%s\n", MakeDiffLine(line, "cyan", disableColor))
}
}
return len(lines), nil
}
func MakeDiffLine(line, color string, disableColor bool) string {
if disableColor {
return line
}
return ansi.Color(line, color)
}