-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
104 lines (94 loc) · 1.92 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
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"path"
"runtime"
"strings"
"github.com/jayvynl/goctl-openapi/oas3"
"github.com/zeromicro/go-zero/tools/goctl/plugin"
"gopkg.in/yaml.v2"
)
const Version = "v1.6.0"
var (
version = flag.Bool("version", false, `show version and exit.`)
output = flag.String("filename", "", `openapi file name, default "openapi.json", "-" will output to stdout.`)
format = flag.String("format", "", `serialization format, "json" or "yaml", default "json".`)
pretty = flag.Bool("pretty", false, `pretty print of json.`)
)
func main() {
flag.Parse()
if *version {
fmt.Printf("goctl-openapi %s %s/%s\n", Version, runtime.GOOS, runtime.GOARCH)
return
}
p, err := plugin.NewPlugin()
if err != nil {
fmt.Printf("goctl-openapi: %s\n", err)
return
}
var (
o = "openapi"
f = "json"
)
if *output != "" {
o = *output
}
if strings.HasSuffix(o, ".json") {
f = "json"
} else if strings.HasSuffix(o, ".yml") || strings.HasSuffix(o, ".yaml") {
f = "yaml"
} else {
if *format != "" {
switch *format {
case "json":
f = "json"
case "yaml", "yml":
f = "yaml"
default:
fmt.Println("goctl-openapi: format must be json or yaml")
return
}
}
if o != "-" {
o = fmt.Sprintf("%s.%s", o, f)
}
}
var w *os.File
if o == "-" {
w = os.Stdout
} else {
w, err = os.Create(path.Join(p.Dir, o))
if err != nil {
fmt.Printf("goctl-openapi: %s\n", err)
return
}
defer w.Close()
}
doc, err := oas3.GetDoc(p)
if err != nil {
fmt.Printf("goctl-openapi: %s\n", err)
return
}
if f == "json" {
encoder := json.NewEncoder(w)
if *pretty {
encoder.SetIndent("", " ")
}
err = encoder.Encode(doc)
if err != nil {
fmt.Printf("goctl-openapi: %s\n", err)
return
}
} else {
encoder := yaml.NewEncoder(w)
defer encoder.Close()
err = encoder.Encode(doc)
if err != nil {
fmt.Printf("goctl-openapi: %s\n", err)
return
}
}
}