-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
218 lines (181 loc) · 4.08 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"github.com/yazgazan/jaydiff/diff"
)
const (
statusUsage = 2
statusReadError = 3
statusUnmarshalError = 4
statusDiffError = 5
statusDiffMismatch = 6
)
var (
// Version is replaced by the tag when creating a new release
Version = "dev"
)
func main() {
var (
err error
lhs, rhs interface{}
lhsCloser, rhsCloser io.Closer
)
conf := readConfig()
switch conf.Stream {
case true:
lhs, lhsCloser = parseStream(conf.Files.LHS, conf.StreamLines)
defer lhsCloser.Close()
rhs, rhsCloser = parseStream(conf.Files.RHS, conf.StreamLines)
defer rhsCloser.Close()
if conf.StreamValidate {
lhs = singleValueForValidate(lhs.(diff.Stream), rhs.(HasMore))
}
case false:
lhs = parseFile(conf.Files.LHS)
rhs = parseFile(conf.Files.RHS)
}
d, err := diff.Diff(lhs, rhs, conf.Opts()...)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: diff failed: %s\n", err)
os.Exit(statusDiffError)
}
d, err = pruneIgnore(
d,
conf.IgnoreExcess,
conf.IgnoreValues,
conf.StreamIgnoreExcess,
conf.Ignore,
)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: ignoring failed: %s\n", err)
os.Exit(statusDiffError)
}
if conf.OutputReport {
ss, err := diff.Report(d, diff.Output(conf.output))
if err != nil {
fmt.Fprintf(os.Stderr, "Error: Failed to generate report: %s\n", err)
os.Exit(statusDiffError)
}
for _, s := range ss {
fmt.Println(s)
}
} else {
fmt.Println(d.StringIndent("", "", diff.Output(conf.output)))
}
if d.Diff() != diff.Identical {
os.Exit(statusDiffMismatch)
}
}
func pruneIgnore(d diff.Differ, ingoreExcess, ignoreValues, streamIgnoreExcess bool, ignore ignorePatterns) (diff.Differ, error) {
return diff.Walk(d, func(parent diff.Differ, d diff.Differ, path string) (diff.Differ, error) {
if ignore.Match(path) {
return diff.Ignore()
}
if ((ingoreExcess && !diff.IsStream(parent)) || (streamIgnoreExcess && diff.IsStream(parent))) && diff.IsExcess(d) {
return diff.Ignore()
}
if ignoreValues && diff.IsScalar(d) && d.Diff() == diff.ContentDiffer {
return diff.Ignore()
}
return nil, nil
})
}
func parseFile(fname string) interface{} {
var err error
var val interface{}
b, err := ioutil.ReadFile(fname)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: cannot read %s\n", fname)
os.Exit(statusReadError)
}
err = json.Unmarshal(b, &val)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: cannot parse %s: %s\n", fname, err)
os.Exit(statusUnmarshalError)
}
return val
}
func parseStream(fname string, lineByLine bool) (diff.Stream, io.Closer) {
f, err := os.Open(fname)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: cannot open %s\n", fname)
os.Exit(statusReadError)
}
if lineByLine {
return &LineByLineJSONStream{
Scanner: bufio.NewScanner(f),
}, f
}
return &diff.JSONStream{
Decoder: json.NewDecoder(f),
}, f
}
type LineByLineJSONStream struct {
*bufio.Scanner
eof bool
i int
}
func (s *LineByLineJSONStream) NextValue() (interface{}, error) {
if s.eof {
return nil, io.EOF
}
if s.Scan() {
v, err := decodeJSON(s.Bytes())
if err != nil {
err = fmt.Errorf("decoding json line %d: %v", s.i, err)
}
s.i++
return v, err
}
err := s.Err()
if err == nil {
s.eof = true
return nil, io.EOF
}
return nil, err
}
func decodeJSON(b []byte) (interface{}, error) {
var v interface{}
err := json.Unmarshal(b, &v)
return v, err
}
type singleValueStream struct {
diff.Stream
partnerStream HasMore
value interface{}
valueSet bool
eof bool
}
type HasMore interface {
More() bool
}
func (s *singleValueStream) NextValue() (interface{}, error) {
if s.eof {
return nil, io.EOF
}
if !s.partnerStream.More() {
s.eof = true
return nil, io.EOF
}
if s.valueSet {
return s.value, nil
}
v, err := s.Stream.NextValue()
if err != nil {
return nil, err
}
s.valueSet = true
s.value = v
return v, nil
}
func singleValueForValidate(lhs diff.Stream, rhs HasMore) diff.Stream {
return &singleValueStream{
Stream: lhs,
partnerStream: rhs,
}
}