forked from grokify/spectrum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspectrum.go
78 lines (65 loc) · 1.95 KB
/
spectrum.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
package main
import (
"fmt"
"log"
"strings"
"github.com/grokify/spectrum/openapi3"
"github.com/grokify/spectrum/openapi3/openapi3postman2"
"github.com/jessevdk/go-flags"
"github.com/pkg/errors"
)
// Convert yaml2json: https://github.com/bronze1man/yaml2json ... yaml2json_darwin_amd64
type Options struct {
OpenAPIFile string `short:"O" long:"openapiFile" description:"Input Swagger File" required:"true"`
Config string `short:"C" long:"config" description:"Spectrum Config File"`
PostmanBase string `short:"B" long:"basePostmanFile" description:"Basic Postman File"`
Postman string `short:"P" long:"postmanFile" description:"Output Postman File"`
XLSXFile string `short:"X" long:"xlsxFile" description:"Output XLSX File"`
}
func (opts *Options) TrimSpace() {
opts.Config = strings.TrimSpace(opts.Config)
opts.PostmanBase = strings.TrimSpace(opts.PostmanBase)
opts.Postman = strings.TrimSpace(opts.Postman)
opts.OpenAPIFile = strings.TrimSpace(opts.OpenAPIFile)
}
func main() {
var opts Options
_, err := flags.Parse(&opts)
if err != nil {
log.Fatal(err)
}
spec, err := openapi3.ReadFile(opts.OpenAPIFile, true)
if err != nil {
log.Fatal(err)
}
if len(opts.Postman) > 0 {
cfg3 := openapi3postman2.Configuration{}
if len(opts.Config) > 0 {
cfg3, err = openapi3postman2.ConfigurationReadFile(opts.Config)
if err != nil {
errors.Wrap(err, "openapi3postman2.ConfigurationReadFile")
log.Fatal(err)
}
}
conv := openapi3postman2.Converter{
Configuration: cfg3,
OpenAPISpec: spec}
err = conv.MergeConvert(
opts.OpenAPIFile,
opts.PostmanBase,
opts.Postman)
if err != nil {
errors.Wrap(err, "spectrum.main << conv.MergeConvert")
log.Fatal(err)
}
fmt.Printf("wrote Postman collection [%s]\n", opts.Postman)
}
if len(opts.XLSXFile) > 0 {
sm := openapi3.SpecMore{Spec: spec}
err := sm.WriteFileXLSX(opts.XLSXFile, nil, nil)
if err != nil {
log.Fatal(err)
}
}
fmt.Println("DONE")
}