-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.go
162 lines (143 loc) · 3.34 KB
/
main.go
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
package main
import (
"fmt"
"io"
"os"
"sort"
"strings"
"github.com/r3labs/diff/v3"
"github.com/jessevdk/go-flags"
"github.com/logrusorgru/aurora"
"github.com/mattn/go-isatty"
"gopkg.in/yaml.v2"
)
var version = "latest"
func main() {
var opts struct {
NoColor bool `long:"no-color" description:"disable colored output" required:"false"`
Version func() `long:"version" description:"print version and exit"`
}
opts.Version = func() {
fmt.Fprintf(os.Stderr, "%v\n", version)
os.Exit(0)
}
files, err := flags.Parse(&opts)
if err != nil {
if err.(*flags.Error).Type == flags.ErrHelp {
os.Exit(0)
}
os.Exit(1)
}
if len(files) < 2 {
fmt.Fprintln(os.Stderr, "Two filenames must be supplied for comparison")
os.Exit(1)
} else if len(files) > 2 {
fmt.Fprintln(os.Stderr, "Too many command line options, yamldiff can only compare two files")
os.Exit(1)
}
formatter := newFormatter(opts.NoColor)
file1 := files[0]
file2 := files[1]
errors := stat(file1, file2)
failOnErr(formatter, errors...)
yaml1, err := unmarshal(file1)
if err != nil {
failOnErr(formatter, err)
}
yaml2, err := unmarshal(file2)
if err != nil {
failOnErr(formatter, err)
}
computedDiff := computeDiff(formatter, yaml1, yaml2)
if computedDiff != "" {
fmt.Println(computedDiff)
}
}
func stat(filenames ...string) []error {
var errs []error
for _, filename := range filenames {
if filename == "-" {
continue
}
_, err := os.Stat(filename)
if err != nil {
errs = append(errs, fmt.Errorf("cannot find file: %v. Does it exist", filename))
}
}
return errs
}
func unmarshal(filename string) (interface{}, error) {
var contents []byte
var err error
if filename == "-" {
contents, err = io.ReadAll(os.Stdin)
} else {
contents, err = os.ReadFile(filename)
}
if err != nil {
return nil, err
}
var ret interface{}
err = yaml.Unmarshal(contents, &ret)
if err != nil {
return nil, err
}
return ret, nil
}
func failOnErr(formatter aurora.Aurora, errs ...error) {
if len(errs) == 0 {
return
}
var errMessages []string
for _, err := range errs {
errMessages = append(errMessages, err.Error())
}
fmt.Fprintf(os.Stderr, "%v\n\n", formatter.Red(strings.Join(errMessages, "\n")))
os.Exit(1)
}
func computeDiff(formatter aurora.Aurora, a interface{}, b interface{}) string {
diffs := make([]string, 0)
differ, err := diff.NewDiffer(diff.AllowTypeMismatch(true))
if err != nil {
return err.Error()
}
changelog, err := differ.Diff(a, b)
if err != nil {
return err.Error()
}
for _, s := range changelog {
pathStr := strings.Join(s.Path, ".")
fromStr := formatter.Red(fmt.Sprintf("- %v", s.From))
toStr := formatter.Green(fmt.Sprintf("+ %v", s.To))
chunk := fmt.Sprintf("%s:\n%s\n%s\n", pathStr, fromStr, toStr)
diffs = appendSorted(diffs, chunk)
}
return strings.Join(diffs, "\n")
}
func newFormatter(noColor bool) aurora.Aurora {
var formatter aurora.Aurora
if noColor || !isTerminal() {
formatter = aurora.NewAurora(false)
} else {
formatter = aurora.NewAurora(true)
}
return formatter
}
func isTerminal() bool {
fd := os.Stdout.Fd()
switch {
case isatty.IsTerminal(fd):
return true
case isatty.IsCygwinTerminal(fd):
return true
default:
return false
}
}
func appendSorted(ss []string, s string) []string {
i := sort.SearchStrings(ss, s)
ss = append(ss, "")
copy(ss[i+1:], ss[i:])
ss[i] = s
return ss
}