-
Notifications
You must be signed in to change notification settings - Fork 546
Expand file tree
/
Copy pathmain.go
More file actions
90 lines (79 loc) · 2.19 KB
/
main.go
File metadata and controls
90 lines (79 loc) · 2.19 KB
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
package main
import (
"fmt"
"os"
"regexp"
"strings"
"github.com/bmatcuk/doublestar/v4"
)
func parseDocsTables() map[string]bool {
tablesMap := make(map[string]bool)
tablesReadmes, err := doublestar.Glob(os.DirFS("../../"), "plugins/source/**/docs/tables/README.md", doublestar.WithFailOnPatternNotExist(), doublestar.WithFilesOnly())
if err != nil {
panic(err)
}
for _, readme := range tablesReadmes {
if strings.HasPrefix(readme, "plugins/source/test") {
continue
}
content, err := os.ReadFile("../../" + readme)
if err != nil {
panic(err)
}
lines := strings.Split(string(content), "\n")
for _, line := range lines {
pos1 := strings.Index(line, "[")
if pos1 == -1 {
continue
}
pos2 := strings.Index(line, "]")
table := line[pos1+1 : pos2]
tablesMap[table] = true
}
}
return tablesMap
}
func parseCodeTables() map[string]string {
tablesMap := make(map[string]string)
tableFiles, err := doublestar.Glob(os.DirFS("../../"), "plugins/source/**/resources/services/**/*.go", doublestar.WithFailOnPatternNotExist(), doublestar.WithFilesOnly())
if err != nil {
panic(err)
}
for _, tableFile := range tableFiles {
if strings.HasPrefix(tableFile, "plugins/source/test") {
continue
}
if strings.HasSuffix(tableFile, "_test.go") {
continue
}
content, err := os.ReadFile("../../" + tableFile)
if err != nil {
panic(err)
}
contentString := string(content)
if !strings.Contains(contentString, "*schema.Table") {
continue
}
tableNameRegex := regexp.MustCompile(`schema.Table[\s\S]+?Name\:.*?"(.*?)",`)
tableName := tableNameRegex.FindStringSubmatch(contentString)
if len(tableName) != 2 {
continue
}
tablesMap[tableName[1]] = tableFile
}
return tablesMap
}
func main() {
tablesFromReadmes := parseDocsTables()
tablesFromCode := parseCodeTables()
for table, file := range tablesFromCode {
if _, ok := tablesFromReadmes[table]; !ok {
fmt.Printf("- Table `%s` is declared in code but missing from README. Table declaration file: `%s`\n", table, file)
}
}
for table := range tablesFromReadmes {
if _, ok := tablesFromCode[table]; !ok {
fmt.Printf("- Table `%s` is in README but not declared in code\n", table)
}
}
}