forked from Tufin/oasdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
278 lines (247 loc) · 9.56 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"reflect"
"github.com/getkin/kin-openapi/openapi3"
"github.com/tufin/oasdiff/build"
"github.com/tufin/oasdiff/checker"
"github.com/tufin/oasdiff/checker/localizations"
"github.com/tufin/oasdiff/diff"
"github.com/tufin/oasdiff/load"
"github.com/tufin/oasdiff/report"
"gopkg.in/yaml.v3"
)
var base, revision, filter, filterExtension, format, lang, warnIgnoreFile, errIgnoreFile string
var prefix_base, prefix_revision, strip_prefix_base, strip_prefix_revision, prefix string
var excludeExamples, excludeDescription, summary, breakingOnly, failOnDiff, failOnWarns, version, composed, checkBreaking, excludeEndpoints bool
var deprecationDays int
const (
formatYAML = "yaml"
formatJSON = "json"
formatText = "text"
formatHTML = "html"
)
func init() {
flag.StringVar(&base, "base", "", "path or URL (or a glob in Composed mode) of original OpenAPI spec in YAML or JSON format")
flag.StringVar(&revision, "revision", "", "path or URL (or a glob in Composed mode) of revised OpenAPI spec in YAML or JSON format")
flag.BoolVar(&composed, "composed", false, "work in 'composed' mode, compare paths in all specs matching base and revision globs")
flag.StringVar(&prefix_base, "prefix-base", "", "if provided, paths in original (base) spec will be prefixed with the given prefix before comparison")
flag.StringVar(&prefix_revision, "prefix-revision", "", "if provided, paths in revised (revision) spec will be prefixed with the given prefix before comparison")
flag.StringVar(&strip_prefix_base, "strip-prefix-base", "", "if provided, this prefix will be stripped from paths in original (base) spec before comparison")
flag.StringVar(&strip_prefix_revision, "strip-prefix-revision", "", "if provided, this prefix will be stripped from paths in revised (revision) spec before comparison")
flag.StringVar(&prefix, "prefix", "", "deprecated. use -prefix-revision instead")
flag.StringVar(&filter, "filter", "", "if provided, diff will include only paths that match this regular expression")
flag.StringVar(&filterExtension, "filter-extension", "", "if provided, diff will exclude paths and operations with an OpenAPI Extension matching this regular expression")
flag.BoolVar(&excludeExamples, "exclude-examples", false, "ignore changes to examples")
flag.BoolVar(&excludeDescription, "exclude-description", false, "ignore changes to descriptions")
flag.BoolVar(&summary, "summary", false, "display a summary of the changes instead of the full diff")
flag.BoolVar(&breakingOnly, "breaking-only", false, "display breaking changes only (old method)")
flag.BoolVar(&checkBreaking, "check-breaking", false, "check for breaking changes (new method)")
flag.StringVar(&warnIgnoreFile, "warn-ignore", "", "the configuration file for ignoring warnings with -check-breaking")
flag.StringVar(&errIgnoreFile, "err-ignore", "", "the configuration file for ignoring errors with -check-breaking")
flag.IntVar(&deprecationDays, "deprecation-days", 0, "minimal number of days required between deprecating a resource and removing it without being considered 'breaking'")
flag.StringVar(&format, "format", formatYAML, "output format: yaml, json, text or html")
flag.StringVar(&lang, "lang", "en", "language for localized breaking changes checks errors")
flag.BoolVar(&failOnDiff, "fail-on-diff", false, "exit with return code 1 when any ERR-level breaking changes are found, used together with -check-breaking")
flag.BoolVar(&failOnWarns, "fail-on-warns", false, "exit with return code 1 when any WARN-level breaking changes are found, used together with -check-breaking and -fail-on-diff")
flag.BoolVar(&version, "version", false, "show version and quit")
flag.IntVar(&openapi3.CircularReferenceCounter, "max-circular-dep", 5, "maximum allowed number of circular dependencies between objects in OpenAPI specs")
flag.BoolVar(&excludeEndpoints, "exclude-endpoints", false, "exclude endpoints from output")
}
func validateFlags() bool {
if base == "" {
fmt.Fprintf(os.Stderr, "please specify the '-base' flag: the path of the original OpenAPI spec in YAML or JSON format\n")
return false
}
if revision == "" {
fmt.Fprintf(os.Stderr, "please specify the '-revision' flag: the path of the revised OpenAPI spec in YAML or JSON format\n")
return false
}
supportedFormats := map[string]bool{"yaml": true, "json": true, "text": true, "html": true}
if !supportedFormats[format] {
fmt.Fprintf(os.Stderr, "invalid format. Should be yaml, json text or html\n")
return false
}
if format == "json" && !excludeEndpoints {
fmt.Fprintf(os.Stderr, "json format requires the '-exclude-endpoints' flag\n")
return false
}
if prefix != "" {
if prefix_revision != "" {
fmt.Fprintf(os.Stderr, "'-prefix' and '-prefix_revision' can't be used simultaneously\n")
return false
}
prefix_revision = prefix
}
if failOnWarns {
if !checkBreaking || !failOnDiff {
fmt.Fprintf(os.Stderr, "'-fail-on-warns' is relevant only with '-check-breaking' and '-fail-on-diff'\n")
return false
}
}
return true
}
func main() {
flag.Parse()
if version {
fmt.Printf("oasdiff version: %s\n", build.Version)
os.Exit(0)
}
if !validateFlags() {
os.Exit(101)
}
loader := openapi3.NewLoader()
loader.IsExternalRefsAllowed = true
config := diff.NewConfig()
config.ExcludeExamples = excludeExamples
config.ExcludeDescription = excludeDescription
config.PathFilter = filter
config.FilterExtension = filterExtension
config.PathPrefixBase = prefix_base
config.PathPrefixRevision = prefix_revision
config.PathStripPrefixBase = strip_prefix_base
config.PathStripPrefixRevision = strip_prefix_revision
config.BreakingOnly = breakingOnly
config.DeprecationDays = deprecationDays
config.ExcludeEndpoints = excludeEndpoints
var diffReport *diff.Diff
var err error
var operationsSources *diff.OperationsSourcesMap
if checkBreaking {
config.IncludeExtensions.Add(checker.XStabilityLevelExtension)
config.IncludeExtensions.Add(diff.SunsetExtension)
config.IncludeExtensions.Add(checker.XExtensibleEnumExtension)
}
if composed {
s1, err := load.FromGlob(loader, base)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to load base spec from %q with %v\n", base, err)
os.Exit(102)
}
s2, err := load.FromGlob(loader, revision)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to load revision spec from %q with %v\n", revision, err)
os.Exit(103)
}
diffReport, operationsSources, err = diff.GetPathsDiff(config, s1, s2)
if err != nil {
fmt.Fprintf(os.Stderr, "diff failed with %v\n", err)
os.Exit(104)
}
} else {
s1, err := checker.LoadOpenAPISpecInfo(base)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to load base spec from %q with %v\n", base, err)
os.Exit(102)
}
s2, err := checker.LoadOpenAPISpecInfo(revision)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to load revision spec from %q with %v\n", revision, err)
os.Exit(103)
}
diffReport, operationsSources, err = diff.GetWithOperationsSourcesMap(config, s1, s2)
if err != nil {
fmt.Fprintf(os.Stderr, "diff failed with %v\n", err)
os.Exit(104)
}
}
if checkBreaking {
c := checker.DefaultChecks()
c.Localizer = *localizations.New(lang, "en")
errs := checker.CheckBackwardCompatibility(c, diffReport, operationsSources)
if warnIgnoreFile != "" {
errs, err = checker.ProcessIgnoredBackwardCompatibilityErrors(checker.WARN, errs, warnIgnoreFile)
if err != nil {
fmt.Fprintf(os.Stderr, "can't process warn ignore file %v\n", err)
os.Exit(121)
}
}
if errIgnoreFile != "" {
errs, err = checker.ProcessIgnoredBackwardCompatibilityErrors(checker.ERR, errs, errIgnoreFile)
if err != nil {
fmt.Fprintf(os.Stderr, "can't process err ignore file %v\n", err)
os.Exit(122)
}
}
// pretty output
if len(errs) > 0 {
fmt.Printf(c.Localizer.Get("messages.total-errors"), len(errs))
}
countWarns := 0
for _, bcerr := range errs {
if bcerr.Level == checker.WARN {
countWarns++
}
fmt.Printf("%s\n\n", bcerr.PrettyError(c.Localizer))
}
countErrs := len(errs) - countWarns
diffEmpty := countErrs == 0
if failOnWarns {
diffEmpty = len(errs) == 0
}
exitNormally(diffEmpty)
}
if summary {
if err = printYAML(diffReport.GetSummary()); err != nil {
fmt.Fprintf(os.Stderr, "failed to print summary with %v\n", err)
os.Exit(105)
}
exitNormally(diffReport.Empty())
}
switch {
case format == formatYAML:
if err = printYAML(diffReport); err != nil {
fmt.Fprintf(os.Stderr, "failed to print diff YAML with %v\n", err)
os.Exit(106)
}
case format == formatJSON:
if err = printJSON(diffReport); err != nil {
fmt.Fprintf(os.Stderr, "failed to print diff JSON with %v\n", err)
os.Exit(106)
}
case format == formatText:
fmt.Printf("%s", report.GetTextReportAsString(diffReport))
case format == formatHTML:
html, err := report.GetHTMLReportAsString(diffReport)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to generate HTML diff report with %v\n", err)
os.Exit(107)
}
fmt.Printf("%s", html)
default:
fmt.Fprintf(os.Stderr, "unknown output format %q\n", format)
os.Exit(108)
}
exitNormally(diffReport.Empty())
}
func exitNormally(diffEmpty bool) {
if failOnDiff && !diffEmpty {
os.Exit(1)
}
os.Exit(0)
}
func printYAML(output interface{}) error {
if reflect.ValueOf(output).IsNil() {
return nil
}
bytes, err := yaml.Marshal(output)
if err != nil {
return err
}
fmt.Printf("%s", bytes)
return nil
}
func printJSON(output interface{}) error {
if reflect.ValueOf(output).IsNil() {
return nil
}
bytes, err := json.MarshalIndent(output, "", " ")
if err != nil {
return err
}
fmt.Printf("%s\n", bytes)
return nil
}