-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (108 loc) · 2.39 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
package main
import (
"flag"
"fmt"
"os"
"path"
"path/filepath"
"sort"
"github.com/go-openapi/loads"
"github.com/go-openapi/spec"
)
var version = "1.0.0"
type route struct {
Path string
Methods []string
}
type routes []route
func (r routes) Len() int {
return len(r)
}
func (r routes) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
func (r routes) Less(i, j int) bool {
return r[i].Path < r[j].Path
}
func main() {
var showVersion bool
var showHelp bool
var importPath string
flag.BoolVar(&showVersion, "v", false, "show version")
flag.BoolVar(&showVersion, "-version", false, "show version")
flag.BoolVar(&showHelp, "h", false, "show help")
flag.BoolVar(&showHelp, "-help", false, "show help")
flag.StringVar(&importPath, "f", "swagger.json", "import path your swagger.json")
flag.Parse()
if showHelp {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
return
}
if showVersion {
fmt.Println("version:", version)
return
}
cwd, err := os.Getwd()
if err != nil {
fmt.Println("failed to get working direcroty")
return
}
src := filepath.Join(cwd, importPath)
sw, err := loadSpec(src)
if err != nil {
fmt.Println(err)
return
}
var rs routes = make([]route, len(sw.Paths.Paths))
var i int
for path, pathItem := range sw.Paths.Paths {
r := route{Path: path}
if pathItem.Get != nil {
r.Methods = append(r.Methods, "GET")
}
if pathItem.Put != nil {
r.Methods = append(r.Methods, "PUT")
}
if pathItem.Post != nil {
r.Methods = append(r.Methods, "POST")
}
if pathItem.Delete != nil {
r.Methods = append(r.Methods, "DELETE")
}
if pathItem.Options != nil {
r.Methods = append(r.Methods, "OPTIONS")
}
if pathItem.Head != nil {
r.Methods = append(r.Methods, "HEAD")
}
if pathItem.Patch != nil {
r.Methods = append(r.Methods, "PATCH")
}
rs[i] = r
i++
}
sort.Sort(rs)
for _, s := range sw.Schemes {
fmt.Printf("%s://%s\n", s, path.Join(sw.Host, sw.BasePath))
}
for _, r := range rs {
for _, m := range r.Methods {
fmt.Printf("%6s\t%s\n", m, r.Path)
}
}
}
func loadSpec(src string) (*spec.Swagger, error) {
fi, err := os.Stat(src)
if err != nil {
return nil, fmt.Errorf("not exists a file %q", src)
}
if fi.IsDir() {
return nil, fmt.Errorf("expected %q to be a file not a directory", src)
}
sp, err := loads.Spec(src)
if err != nil {
return nil, err
}
return sp.Spec(), nil
}