forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
page.go
163 lines (130 loc) · 3.34 KB
/
page.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
package apidocs
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"text/template"
"github.com/go-openapi/spec"
)
type Page struct {
s *spec.Swagger
Operations []operation
}
type Pages map[string]Page
func (page Page) Title() string {
return page.Operations[0].gvk.Version + "." + page.Operations[0].gvk.Kind
}
func (page Page) Description() (string, error) {
for _, def := range page.s.Definitions {
for _, defgvk := range GroupVersionKinds(def) {
if defgvk == page.Operations[0].gvk {
return def.Description, nil
}
}
}
return "", fmt.Errorf("definition for %s not found", page.Operations[0].gvk)
}
func (page Page) Schema() ([]*line, error) {
for defName, def := range page.s.Definitions {
for _, defgvk := range GroupVersionKinds(def) {
if defgvk == page.Operations[0].gvk {
return buildLines(page.s, page.s.Definitions[defName], ""), nil
}
}
}
return nil, fmt.Errorf("definition for %s not found", page.Operations[0].gvk)
}
func (page Page) OutputPath() string {
gvk := page.Operations[0].gvk
parts := strings.Split(strings.Trim(page.Operations[0].PathName, "/"), "/")
dir := parts[0]
if gvk.Group == "extensions" &&
gvk.Version == "v1" &&
gvk.Kind == "ReplicationController" {
dir = "api"
}
if gvk.Group != "" {
dir += "-" + gvk.Group
}
return filepath.Join(dir, gvk.Version+"."+gvk.Kind+".adoc")
}
func (page Page) ParentTopicName() string {
gvk := page.Operations[0].gvk
parts := strings.Split(strings.Trim(page.Operations[0].PathName, "/"), "/")
dir := parts[0]
if gvk.Group == "extensions" &&
gvk.Version == "v1" &&
gvk.Kind == "ReplicationController" {
dir = "api"
}
if gvk.Group != "" {
dir += "/" + gvk.Group
}
return "/" + dir + "/" + gvk.Version
}
func BuildPages(s *spec.Swagger) (Pages, error) {
pages := make(Pages, len(s.Paths.Paths))
for pathName, path := range s.Paths.Paths {
if strings.HasPrefix(pathName, "/api/v1/proxy/") { // deprecated API which requires additional logic, ignore it at least for now
continue
}
for opName, op := range Operations(path) {
o := operation{
s: s,
Path: path,
PathName: pathName,
Operation: op,
OpName: opName,
}
var err error
o.gvk, err = o.GVK()
if err != nil {
return nil, err
}
page, found := pages[o.gvk.String()]
if !found {
page = Page{s: s}
}
page.Operations = append(page.Operations, o)
pages[o.gvk.String()] = page
}
}
for key, page := range pages {
sort.Sort(byPathName(page.Operations))
sort.Stable(byNamespaced(page.Operations))
sort.Stable(byPlural(page.Operations))
sort.Stable(byOperationVerb(page.Operations))
sort.Stable(bySubresource(page.Operations))
sort.Stable(byProxy(page.Operations))
pages[key] = page
}
return pages, nil
}
func (pages Pages) Write(root string) error {
t, err := template.New("page.adoc").Funcs(template.FuncMap{
"FriendlyTypeName": FriendlyTypeName,
"EscapeMediaTypes": EscapeMediaTypes,
}).ParseGlob("pkg/apidocs/templates/*")
if err != nil {
return err
}
for _, page := range pages {
path := filepath.Join(root, page.OutputPath())
err = os.MkdirAll(filepath.Dir(path), 0777)
if err != nil {
return err
}
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
err = t.Execute(f, page)
if err != nil {
return err
}
}
return nil
}