forked from oasdiff/oasdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
121 lines (104 loc) · 3.23 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
package main
import (
"flag"
"fmt"
"reflect"
"github.com/getkin/kin-openapi/openapi3"
"github.com/tufin/oasdiff/diff"
"github.com/tufin/oasdiff/load"
"github.com/tufin/oasdiff/report"
"gopkg.in/yaml.v3"
)
var base, revision, prefix, filter, format string
var excludeExamples, excludeDescription, summary, breakingOnly bool
const (
formatYAML = "yaml"
formatText = "text"
formatHTML = "html"
)
func init() {
flag.StringVar(&base, "base", "", "path of original OpenAPI spec in YAML or JSON format")
flag.StringVar(&revision, "revision", "", "path of revised OpenAPI spec in YAML or JSON format")
flag.StringVar(&prefix, "prefix", "", "path prefix that exists in base spec but not the revision (optional)")
flag.StringVar(&filter, "filter", "", "regex to filter result paths (optional)")
flag.BoolVar(&excludeExamples, "exclude-examples", false, "exclude changes to examples")
flag.BoolVar(&excludeDescription, "exclude-description", false, "exclude 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")
flag.StringVar(&format, "format", formatYAML, "output format: yaml, text or html")
}
func validateFlags() bool {
if base == "" {
fmt.Printf("please specify the '-base' flag: the path of the original OpenAPI spec in YAML or JSON format\n")
return false
}
if revision == "" {
fmt.Printf("please specify the '-revision' flag: the path of the revised OpenAPI spec in YAML or JSON format\n")
return false
}
supportedFormats := map[string]bool{"": true, "yaml": true, "text": true, "html": true}
if !supportedFormats[format] {
fmt.Printf("invalid format. Should be yaml, text or html\n")
return false
}
return true
}
func main() {
flag.Parse()
if !validateFlags() {
return
}
loader := openapi3.NewLoader()
loader.IsExternalRefsAllowed = true
s1, err := load.From(loader, base)
if err != nil {
fmt.Printf("failed to load base spec from %q with %v\n", base, err)
return
}
s2, err := load.From(loader, revision)
if err != nil {
fmt.Printf("failed to load revision spec from %q with %v\n", revision, err)
return
}
config := diff.NewConfig()
config.ExcludeExamples = excludeExamples
config.ExcludeDescription = excludeDescription
config.PathFilter = filter
config.PathPrefix = prefix
config.BreakingOnly = breakingOnly
diffReport, err := diff.Get(config, s1, s2)
if err != nil {
fmt.Printf("diff failed with %v\n", err)
return
}
if summary {
summary := diffReport.GetSummary()
printYAML(summary)
return
}
if format == formatYAML {
printYAML(diffReport)
} else if format == formatText {
fmt.Printf("%s", report.GetTextReportAsString(diffReport))
} else if format == formatHTML {
html, err := report.GetHTMLReportAsString(diffReport)
if err != nil {
fmt.Printf("failed to generate HTML with %v\n", err)
return
}
fmt.Printf("%s", html)
} else {
fmt.Printf("unknown format %q\n", format)
}
}
func printYAML(output interface{}) {
if reflect.ValueOf(output).IsNil() {
return
}
bytes, err := yaml.Marshal(output)
if err != nil {
fmt.Printf("failed to marshal result as %q with %v\n", format, err)
return
}
fmt.Printf("%s", bytes)
}