-
Notifications
You must be signed in to change notification settings - Fork 99
/
generate-docs.go
190 lines (158 loc) · 5.62 KB
/
generate-docs.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"sort"
"strings"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"sigs.k8s.io/yaml"
)
const (
crdFile = "config/crd/bases/v1/datadoghq.com_datadogagents.yaml"
headerFile = "hack/generate-docs/header.markdown"
footerFile = "hack/generate-docs/$VERSION_footer.markdown"
v2OverridesFile = "hack/generate-docs/v2alpha1_overrides.markdown"
docsFile = "docs/configuration.$VERSION.md"
)
type parameterDoc struct {
name string
description string
}
func main() {
crdYaml := mustReadFile(crdFile)
header := mustReadFile(headerFile)
crd := apiextensions.CustomResourceDefinition{}
err := yaml.Unmarshal(crdYaml, &crd)
if err != nil {
panic(fmt.Sprintf("cannot unmarshal yaml CRD: %s", err))
}
for _, crdVersion := range crd.Spec.Versions {
generateDoc(header, crdVersion, crdVersion.Name)
}
}
func generateDoc(header []byte, crd apiextensions.CustomResourceDefinitionVersion, version string) {
file := strings.Replace(docsFile, "$VERSION", version, 1)
footerFile := strings.Replace(footerFile, "$VERSION", version, 1)
footer := mustReadFile(footerFile)
f, err := os.OpenFile(file, os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0o644)
if err != nil {
panic(fmt.Sprintf("cannot write to file: %s", err))
}
defer func() {
if err := f.Close(); err != nil {
panic(fmt.Sprintf("cannot close file: %s", err))
}
}()
// Write header and example yaml
exampleYaml := mustReadFile(exampleFile(version))
mustWrite(f, header)
mustWriteString(f, "\n")
mustWrite(f, exampleYaml)
mustWriteString(f, "\n")
// Write prop content
var generator = map[string]func(*os.File, apiextensions.CustomResourceDefinitionVersion){
"v1alpha1": generateContent_v1alpha1,
"v2alpha1": generateContent_v2alpha1,
}
generator[version](f, crd)
// Write footer
mustWrite(f, footer)
}
func generateContent_v1alpha1(f *os.File, crd apiextensions.CustomResourceDefinitionVersion) {
writePropsTable(f, crd.Schema.OpenAPIV3Schema.Properties["spec"].Properties)
}
func generateContent_v2alpha1(f *os.File, crd apiextensions.CustomResourceDefinitionVersion) {
writePropsTable(f, crd.Schema.OpenAPIV3Schema.Properties["spec"].Properties)
overridesMarkdown := mustReadFile(v2OverridesFile)
mustWrite(f, overridesMarkdown)
mustWriteString(f, "\n")
mustWriteString(f, "| Parameter | Description |\n")
mustWriteString(f, "| --------- | ----------- |\n")
overrideProps := crd.Schema.OpenAPIV3Schema.Properties["spec"].Properties["override"]
writeOverridesRecursive(f, "[key]", overrideProps.AdditionalProperties.Schema.Properties)
}
func writePropsTable(f *os.File, props map[string]apiextensions.JSONSchemaProps) {
docs := getParameterDocs([]string{}, props)
sort.Slice(docs, func(i, j int) bool {
return docs[i].name < docs[j].name
})
mustWriteString(f, "| Parameter | Description |\n")
mustWriteString(f, "| --------- | ----------- |\n")
for _, doc := range docs {
mustWriteString(f, fmt.Sprintf("| %s | %s |\n", doc.name, doc.description))
}
}
func mustReadFile(path string) []byte {
f, err := os.Open(path)
if err != nil {
panic(fmt.Sprintf("cannot open file %q: %s", path, err))
}
defer func() {
if err = f.Close(); err != nil {
panic(fmt.Sprintf("cannot close file: %s", err))
}
}()
b, err := ioutil.ReadAll(f)
if err != nil {
panic(fmt.Sprintf("cannot read file %q: %s", path, err))
}
return b
}
func mustWrite(f io.Writer, b []byte) {
if _, err := f.Write(b); err != nil {
panic(fmt.Sprintf("cannot write to file: %s", err))
}
}
func mustWriteString(f io.StringWriter, b string) {
if _, err := f.WriteString(b); err != nil {
panic(fmt.Sprintf("cannot write to file: %s", err))
}
}
func getParameterDocs(path []string, props map[string]apiextensions.JSONSchemaProps) []parameterDoc {
parameterDocs := []parameterDoc{}
for name, prop := range props {
parameterDocs = append(parameterDocs, getParameterDoc(path, name, prop)...)
}
return parameterDocs
}
func getParameterDoc(path []string, name string, prop apiextensions.JSONSchemaProps) []parameterDoc {
path = append(path, name)
if len(prop.Properties) == 0 {
return []parameterDoc{
{
name: strings.Join(path, "."),
description: strings.ReplaceAll(prop.Description, "\n", ""),
},
}
}
return getParameterDocs(path, prop.Properties)
}
func exampleFile(version string) string {
return fmt.Sprintf("hack/generate-docs/%s_example.markdown", version)
}
func writeOverridesRecursive(f *os.File, prefix string, props map[string]apiextensions.JSONSchemaProps) {
docs := getParameterDocs([]string{}, props)
sort.Slice(docs, func(i, j int) bool {
return docs[i].name < docs[j].name
})
for _, doc := range docs {
if props[doc.name].Type == "array" {
// https://swagger.io/docs/specification/data-models/data-types/#array
arrayType := props[doc.name].Items.Schema.Type
propName := prefix + "." + doc.name
mustWriteString(f, fmt.Sprintf("| %s `[]%s` | %s |\n", propName, arrayType, doc.description))
} else if !strings.Contains(doc.name, ".") && props[doc.name].AdditionalProperties != nil {
// https://swagger.io/docs/specification/data-models/dictionaries/
mapKeyType := "string"
mapValueType := props[doc.name].AdditionalProperties.Schema.Type
propName := prefix + "." + doc.name
mustWriteString(f, fmt.Sprintf("| %s `map[%s]%s` | %s |\n", propName, mapKeyType, mapValueType, doc.description))
valueTypeProps := props[doc.name].AdditionalProperties.Schema.Properties
writeOverridesRecursive(f, prefix+"."+doc.name+".[key]", valueTypeProps)
} else {
mustWriteString(f, fmt.Sprintf("| %s | %s |\n", prefix+"."+doc.name, doc.description))
}
}
}