/
docs_check.go
324 lines (317 loc) · 10.5 KB
/
docs_check.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
)
func main() {
docsFileName := strings.TrimSpace(os.Args[1])
docsFile, err := os.OpenFile(docsFileName, os.O_RDONLY, 0644)
if err != nil {
log.Printf("open docs file %s failed. Error:%s", docsFileName, err)
os.Exit(1)
}
defer docsFile.Close()
scanner := bufio.NewScanner(docsFile)
docsFileNameParts := strings.Split(docsFileName, "/")
resourceName := "alicloud_" + strings.TrimSuffix(docsFileNameParts[len(docsFileNameParts)-1], ".html.markdown")
exitCode := 0
fmt.Printf("\n==> Checking docs content of %s ...", docsFileName)
titleCheck := false
titleDescriptionCheck := false
descriptionCheck := false
versionChecked := false
exampleCheck := false
exampleBlockOpen := false
argumentCheck := false
attributesCheck := false
importCheck := false
importBlockOpen := false
timeoutCheck := false
line := 0
for scanner.Scan() {
line += 1
text := scanner.Text()
if line == 1 {
titleCheck = true
if text != "---" {
fmt.Printf("\n[Error] line %d: Expected: %s. Got: %s.", line, "---", text)
exitCode = 1
}
continue
}
if titleCheck {
if strings.HasPrefix(text, "layout:") && text != "layout: \"alicloud\"" {
fmt.Printf("\n[Error] line %d: Expected: %s. Got: %s.", line, "layout: \"alicloud\"", text)
exitCode = 1
continue
}
if strings.HasPrefix(text, "page_title:") && text != "page_title: \"Alicloud: "+resourceName+"\"" {
fmt.Printf("\n[Error] line %d: Expected: %s. Got: %s.", line, "page_title: \"Alicloud: "+resourceName, text)
exitCode = 1
continue
}
if strings.HasPrefix(text, "description:") {
titleDescriptionCheck = true
if text != "description: |-" {
fmt.Printf("\n[Error] line %d: Expected: %s. Got: %s.", line, "description: |- ", text)
exitCode = 1
}
continue
}
if titleDescriptionCheck {
titleDescriptionCheck = false
if !strings.HasPrefix(text, " ") {
fmt.Printf("\n[Error] line %d: Expected: %s. Got: %s.", line, " "+text, text)
exitCode = 1
}
continue
}
if text == "---" {
titleCheck = false
descriptionCheck = true
continue
}
}
if descriptionCheck {
if strings.HasPrefix(text, "# alicloud") || strings.HasSuffix(text, resourceName) {
if titleCheck {
fmt.Printf("\n[Error] line %d: docs title has not been closed.", line)
exitCode = 1
}
if strings.Contains(text, "\\") {
fmt.Printf("\n[WARNING] line %d: please remove the \\", line)
text = strings.Replace(text, "\\", "", -1)
}
if text != "# "+resourceName {
fmt.Printf("\n[Error] line %d: Expected: %s. Got: %s.", line, "# "+resourceName, text)
exitCode = 1
}
continue
}
if strings.Contains(text, "https://help.aliyun.com/") {
fmt.Printf("\n[Error] line %d: Expected: an international site link: %s. Got: %s.", line, "https://www.alibabacloud.com/help", "https://help.aliyun.com/")
exitCode = 1
continue
}
if strings.Contains(text, "Available since v") || strings.Contains(text, "Deprecated since v") {
versionChecked = true
for _, v := range []string{"Available since v", "Deprecated since v"} {
if strings.Contains(text, v) && !strings.HasPrefix(text, "-> **NOTE:** "+v) {
parts := strings.Split(text, v)
fmt.Printf("\n[Error] line %d: Expected: %s Got: %s", line, "-> **NOTE:** "+v+strings.Replace(parts[1], "+", "", -1), text)
exitCode = 1
}
}
continue
}
if strings.Contains(text, "Available in v") || strings.Contains(text, "Deprecated from v") {
versionChecked = true
for _, v := range []string{"Available in v", "Deprecated from v"} {
if strings.Contains(text, v) {
parts := strings.Split(text, v)
v = strings.Replace(strings.Replace(v, "in", "since", -1), "from", "since", -1)
fmt.Printf("\n[Error] line %d: Expected: %s Got: %s", line, parts[0]+v+strings.Replace(parts[1], "+", "", -1), text)
exitCode = 1
}
}
continue
}
if strings.HasPrefix(text, "## Example Usage") || strings.HasSuffix(text, "Example Usage") {
descriptionCheck = false
exampleCheck = true
if !versionChecked {
fmt.Printf("\n[Error] line %d: missing available or deprecated verison info.", line)
exitCode = 1
}
if text != "## Example Usage" {
fmt.Printf("\n[Error] line %d: Expected: %s. Got: %s.", line, "## Example Usage", text)
exitCode = 1
}
continue
}
}
if exampleCheck {
if strings.HasPrefix(text, "```") {
if !exampleBlockOpen {
exampleBlockOpen = true
} else {
exampleBlockOpen = false
if text != "```" {
fmt.Printf("\n[Error] line %d: Expected: %s. Got: %s.", line, "```", text)
exitCode = 1
}
}
if exampleBlockOpen && text != "```terraform" {
fmt.Printf("\n[Error] line %d: Expected: %s. Got: %s.", line, "```terraform", text)
exitCode = 1
}
continue
}
if exampleBlockOpen && strings.Contains(text, "test") {
text = strings.TrimSpace(text)
parts := strings.Split(text, "=")
if key := strings.TrimSpace(parts[0]); key == "default" || strings.HasSuffix(key, "name") {
if value := strings.TrimSpace(parts[1]); strings.Contains(value, "test") {
fmt.Printf("\n[Error] line %d: avoid using 'test' as soon as possable in the example", line)
exitCode = 1
}
}
}
if exampleBlockOpen && strings.Contains(text, "depends_on") {
text = strings.TrimSpace(text)
parts := strings.Split(text, "=")
if key := strings.TrimSpace(parts[0]); key == "depends_on" {
fmt.Printf("\n[Error] line %d: avoid using 'depends_on' as soon as possable in the example", line)
exitCode = 1
}
}
if strings.HasPrefix(text, "## Argument Reference") || strings.HasSuffix(text, "Argument Reference") {
exampleCheck = false
argumentCheck = true
if exampleBlockOpen {
fmt.Printf("\n[Error] line %d: example block has not been closed.", line)
exitCode = 1
}
if text != "## Argument Reference" {
fmt.Printf("\n[Error] line %d: Expected: %s. Got: %s.", line, "## Argument Reference", text)
exitCode = 1
}
continue
}
if !argumentCheck && strings.HasPrefix(text, "## Attributes Reference") || strings.HasSuffix(text, "Attributes Reference") {
exampleCheck = false
attributesCheck = true
if text != "## Attributes Reference" {
fmt.Printf("\n[Error] line %d: Expected: %s. Got: %s.", line, "## Attributes Reference", text)
exitCode = 1
}
continue
}
}
if argumentCheck {
if strings.HasPrefix(text, "* `") {
parts := strings.Split(strings.Split(text, ")")[0], "(")
for i, tag := range strings.Split(parts[len(parts)-1], ",") {
if strings.HasPrefix(tag, " ") {
if i == 0 {
fmt.Printf("\n[Error] line %d: please remove redundant space prefix for %s. ", line, tag)
exitCode = 1
}
} else if i > 0 {
fmt.Printf("\n[Error] line %d: missing space prefix for %s. ", line, tag)
exitCode = 1
}
if strings.HasSuffix(tag, " ") {
fmt.Printf("\n[Error] line %d: please remove redundant space suffix for %s.", line, tag)
exitCode = 1
}
}
continue
}
if strings.HasPrefix(text, "###") {
parts := strings.Split(text, " ")
block := strings.ToLower(parts[len(parts)-1])
if len(parts) > 2 ||
(strings.HasPrefix(block, "`") && !strings.HasSuffix(block, "`")) ||
(!strings.HasPrefix(block, "`") && strings.HasSuffix(block, "`")) {
block = strings.Trim(block, "`")
fmt.Printf("\n[Error] line %d: Expected: %s. Got: %s.", line, "### `"+block+"`", text)
exitCode = 1
}
block = strings.Trim(block, "`")
blockParts := strings.Split(block, "-")
blockLink := fmt.Sprintf("[`%s`](#%s)", blockParts[len(blockParts)-1], block)
docsContent, err := os.ReadFile(docsFileName)
if err != nil {
fmt.Printf("\n[Error] reading docs file %s failed. Error: %s", docsFileName, err)
exitCode = 1
} else if !strings.Contains(string(docsContent), blockLink) {
fmt.Printf("\n[Error] line %d: missing link for block `%s`. Expected link like: See %s below.", line, block, blockLink)
exitCode = 1
}
continue
}
if strings.HasPrefix(text, "## Argument Reference") || strings.HasSuffix(text, "Argument Reference") {
argumentCheck = false
attributesCheck = true
if text != "## Attributes Reference" {
fmt.Printf("\n[Error] line %d: Expected: %s. Got: %s.", line, "## Attributes Reference", text)
exitCode = 1
}
continue
}
if strings.HasPrefix(text, "## Attributes Reference") || strings.HasSuffix(text, "Attributes Reference") {
argumentCheck = false
attributesCheck = true
if text != "## Attributes Reference" {
fmt.Printf("\n[Error] line %d: Expected: %s. Got: %s.", line, "## Attributes Reference", text)
exitCode = 1
}
continue
}
}
if attributesCheck {
if strings.HasPrefix(text, "## Timeout") || strings.HasSuffix(text, "Timeouts") || strings.HasSuffix(text, "Timeout") {
attributesCheck = false
timeoutCheck = true
if text != "## Timeouts" {
fmt.Printf("\n[Error] line %d: Expected: %s. Got: %s.", line, "## Timeouts", text)
exitCode = 1
}
continue
}
if strings.HasPrefix(text, "## Import") || strings.HasSuffix(text, "Import") {
attributesCheck = false
importCheck = true
if text != "## Import" {
fmt.Printf("\n[Error] line %d: Expected: %s. Got: %s.", line, "## Import", text)
exitCode = 1
}
continue
}
}
if timeoutCheck {
if strings.HasPrefix(text, "## Import") || strings.HasSuffix(text, "Import") {
timeoutCheck = false
importCheck = true
if text != "## Import" {
fmt.Printf("\n[Error] line %d: Expected: %s. Got: %s.", line, "## Import", text)
exitCode = 1
}
continue
}
}
if importCheck {
if !importBlockOpen && strings.Contains(text, "terraform import") {
fmt.Printf("\n[Error] line %d: Expected: %s.", line-1, "```shell")
exitCode = 1
break
}
if strings.HasPrefix(text, "```") {
if !importBlockOpen {
importBlockOpen = true
} else {
importBlockOpen = false
if text != "```" {
fmt.Printf("\n[Error] line %d: Expected: %s. Got: %s.", line, "```", text)
exitCode = 1
}
}
if importBlockOpen && text != "```shell" {
fmt.Printf("\n[Error] line %d: Expected: %s. Got: %s.", line, "```shell", text)
exitCode = 1
}
continue
}
}
}
if importBlockOpen {
fmt.Println("\n[Error] Error: import block has not been closed.")
exitCode = 1
}
fmt.Println("\n--- Finished!\n")
os.Exit(exitCode)
}