forked from rancher/go-rancher
/
api_description_generator.go
165 lines (144 loc) · 5 KB
/
api_description_generator.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
164
165
package main
import (
"os"
"path"
"regexp"
"strings"
"text/template"
)
var (
descRegexp *regexp.Regexp = regexp.MustCompile(`<resource>`)
optionsRegexp *regexp.Regexp = regexp.MustCompile(`<list of options>`)
)
func generateDescriptionFile(emptyDesc bool, collectionOnly bool, showDesc bool, showActions bool, showResourceFields bool) error {
//func generateDescriptionFile(emptyDesc bool, collectionOnly bool) error {
schemas, err := readCattleSchema()
if err != nil {
return err
}
if err = readBlacklistFiles(); err != nil {
return err
}
genericDescMap := make(map[string]string)
err = readGenDescFile(genericDescMap)
if err != nil {
return err
}
for _, resourceSchema := range schemas.Data {
//Check if it's an invalid Resource Type
if blacklistTypes[resourceSchema.Id] {
continue
}
/*
//Only print out collection links for the collection yml
if collectionOnly {
// If it's not a collection or it's a blacklist Collection, skip
if _, isCollection := resourceSchema.Links["collection"]; !isCollection || isBlacklistCollection(resourceSchema.Id) {
continue
}
} else {
//Only add in actions and fields for non-collection only files
for actionName := range resourceSchema.ResourceActions {
if !isBlacklistAction(resourceSchema.Id, actionName) {
if emptyDesc {
resourceDescriptionsMap[resourceSchema.Id+"-resourceActions-"+actionName] = ""
//resourceDescriptionsMap[resourceSchema.Id+"-"+actionName] = ""
} else {
resourceDescriptionsMap[resourceSchema.Id+"-resourceActions-"+actionName] = "To " + actionName + " the " + resourceSchema.Id
}
}
}
for fieldName, field := range resourceSchema.ResourceFields {
if emptyDesc {
resourceDescriptionsMap[resourceSchema.Id+"-resourceFields-"+fieldName] = ""
//resourceDescriptionsMap[resourceSchema.Id+"-"+fieldName] = ""
} else {
//check if a generic desc exists
var description string
if genericDesc, ok := genericDescMap[fieldName]; ok {
description = descRegexp.ReplaceAllString(genericDesc, resourceSchema.Id)
description = optionsRegexp.ReplaceAllString(description, "["+strings.Join(field.Options, ", ")+"]")
}
//else {
//description = "The " + fieldName + " for the " + schema.Id
//}
resourceDescriptionsMap[resourceSchema.Id+"-resourceFields-"+fieldName] = description
}
}
}
resourceDescriptionsMap[resourceSchema.Id+"-description"] = ""
*/
visibleCollection := false
// If it's a collection or not a blacklist Collection, set true
if _, isCollection := resourceSchema.Links["collection"]; isCollection && !isBlacklistCollection(resourceSchema.Id) {
visibleCollection = true
}
//Only print out collection links
if collectionOnly && !visibleCollection {
continue
}
if showDesc {
resourceDescriptionsMap[resourceSchema.Id+"-description"] = ""
}
if showActions {
for actionName := range resourceSchema.ResourceActions {
if !isBlacklistAction(resourceSchema.Id, actionName) {
if emptyDesc {
resourceDescriptionsMap[resourceSchema.Id+"-resourceActions-"+actionName] = ""
//resourceDescriptionsMap[resourceSchema.Id+"-"+actionName] = ""
} else {
resourceDescriptionsMap[resourceSchema.Id+"-resourceActions-"+actionName] = "To " + actionName + " the " + resourceSchema.Id
}
}
}
}
if showResourceFields {
for fieldName, field := range resourceSchema.ResourceFields {
if emptyDesc {
resourceDescriptionsMap[resourceSchema.Id+"-resourceFields-"+fieldName] = ""
//resourceDescriptionsMap[resourceSchema.Id+"-"+fieldName] = ""
} else {
//check if a generic desc exists
var description string
if genericDesc, ok := genericDescMap[fieldName]; ok {
description = descRegexp.ReplaceAllString(genericDesc, resourceSchema.Id)
description = optionsRegexp.ReplaceAllString(description, strings.Join(field.Options, ", "))
} else {
//if enum, show list of options
if field.Type == "enum" {
description = "The options are " + strings.Join(field.Options, ", ") + "."
}
//description = "The " + fieldName + " for the " + schema.Id
}
resourceDescriptionsMap[resourceSchema.Id+"-resourceFields-"+fieldName] = description
}
}
}
}
if err = setupDirectory(apiOutputDir); err != nil {
return err
}
var filePrefix string
if collectionOnly {
filePrefix = "blank_collection_"
} else if emptyDesc {
if showResourceFields && !showDesc && !showActions {
filePrefix = "valid_example_"
} else {
filePrefix = "blank_"
}
}
output, err := os.Create(path.Join(apiInputDir, "/schema-check/"+filePrefix+"api_description.yml"))
if err != nil {
return err
}
defer output.Close()
data := map[string]interface{}{
"descriptionMap": resourceDescriptionsMap,
}
typeTemplate, err := template.New("apiDescription.template").ParseFiles("./templates/apiDescription.template")
if err != nil {
return err
}
return typeTemplate.Execute(output, data)
}