-
Notifications
You must be signed in to change notification settings - Fork 2
/
cato.go
261 lines (223 loc) · 6.29 KB
/
cato.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
package cato
import (
"bufio"
"bytes"
"fmt"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"os"
"path"
"path/filepath"
"reflect"
"regexp"
"strings"
"github.com/cs3org/cato/exporter"
_ "github.com/cs3org/cato/exporter/drivers/loader"
"github.com/cs3org/cato/exporter/drivers/registry"
"github.com/cs3org/cato/resources"
)
type structInfo struct {
StructDef *ast.StructType
StructName string
}
var namedTags = []string{"xml", "mapstructure", "json"}
func listGoFiles(rootPath string) ([]string, error) {
goFileRegex, _ := regexp.Compile(`^.+\.go$`)
fileList := []string{}
err := filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if goFileRegex.MatchString(info.Name()) {
fileList = append(fileList, path)
}
return nil
})
if err != nil {
return nil, err
}
return fileList, nil
}
func getLineInitialPositions(filePath string) ([]int, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer f.Close()
charCount := 1
initPositions := []int{charCount}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
charCount = charCount + len(scanner.Text()) + 1
initPositions = append(initPositions, charCount)
}
if err := scanner.Err(); err != nil {
return nil, err
}
return initPositions, nil
}
func getLineNumber(lineNos []int, pos int) (int, error) {
for i, n := range lineNos {
if pos <= n {
return i, nil
}
}
return -1, fmt.Errorf("position exceeds total characters in the file")
}
func getNestedConfigDefaults(configs map[string][]*resources.FieldInfo) string {
defaults := ""
for _, fields := range configs {
for _, f := range fields {
defaults += f.FieldName + " = " + f.DefaultValue + "\n"
}
}
return defaults
}
func parseStruct(structDef *ast.StructType, catoTag, rootPath, filePath string, fset *token.FileSet, lineNos []int) ([]*resources.FieldInfo, error) {
configs := []*resources.FieldInfo{}
for _, field := range structDef.Fields.List {
if field.Tag != nil {
tag := reflect.StructTag(strings.Trim(field.Tag.Value, "`"))
configTag := tag.Get(catoTag)
if configTag != "" {
// get field.Type as string
var typeNameBuf bytes.Buffer
err := printer.Fprint(&typeNameBuf, fset, field.Type)
if err != nil {
return nil, fmt.Errorf("error decoding struct field name: %w", err)
}
var fieldName string
for _, namedTag := range namedTags {
if t := tag.Get(namedTag); t != "" {
fieldName = strings.Split(t, ",")[0]
}
}
if fieldName == "" {
fieldName = field.Names[0].Name
}
var desc string
if field.Doc != nil {
comments := []string{}
for _, c := range field.Doc.List {
c.Text = strings.ReplaceAll(c.Text, "//", "")
c.Text = strings.ReplaceAll(c.Text, "/*", "")
c.Text = strings.ReplaceAll(c.Text, "*/", "")
c.Text = strings.Join(strings.Fields(c.Text), " ")
comments = append(comments, c.Text)
}
desc = strings.Join(comments, " ")
}
var defaultVal string
switch splitVals := strings.Split(configTag, ";"); len(splitVals) {
case 1:
defaultVal = splitVals[0]
case 2:
defaultVal = splitVals[0]
desc = splitVals[1]
case 3:
fieldName = splitVals[0]
defaultVal = splitVals[1]
desc = splitVals[2]
}
if strings.HasPrefix(defaultVal, "url:") {
driverName := strings.Split(path.Base(strings.TrimPrefix(defaultVal, "url:")), ".")[0]
configs, err := getConfigsToDocument(path.Join(rootPath, strings.TrimPrefix(defaultVal, "url:")), catoTag, rootPath)
if err != nil {
return nil, err
}
defaultVal = "url:" + driverName + ":" + getNestedConfigDefaults(configs)
} else if typeNameBuf.String() == "string" {
defaultVal = fmt.Sprintf("\"%s\"", defaultVal)
}
lineNumber, err := getLineNumber(lineNos, int(field.Pos()))
if err != nil {
return nil, err
}
configs = append(configs, &resources.FieldInfo{
FieldName: fieldName,
DefaultValue: defaultVal,
Description: desc,
DataType: typeNameBuf.String(),
LineNumber: lineNumber,
})
}
}
}
return configs, nil
}
func getConfigsToDocument(filePath, catoTag, rootPath string) (map[string][]*resources.FieldInfo, error) {
fset := token.NewFileSet()
fileTree, err := parser.ParseFile(fset, filePath, nil, parser.ParseComments)
if err != nil {
return nil, err
}
lineNos, err := getLineInitialPositions(filePath)
if err != nil {
return nil, err
}
structList := []*structInfo{}
configs := map[string][]*resources.FieldInfo{}
ast.Inspect(fileTree, func(node ast.Node) bool {
spec, ok := node.(*ast.TypeSpec)
if !ok {
return true
}
s, ok := spec.Type.(*ast.StructType)
if !ok {
return true
}
structList = append(structList, &structInfo{s, spec.Name.Name})
return false
})
for _, s := range structList {
c, err := parseStruct(s.StructDef, catoTag, rootPath, filePath, fset, lineNos)
if err != nil {
return nil, err
}
if len(c) > 0 {
configs[s.StructName] = c
}
}
return configs, nil
}
func getDriver(c *resources.CatoConfig) (exporter.ConfigExporter, error) {
if f, ok := registry.NewFuncs[c.Driver]; ok {
return f(c.DriverConfig[c.Driver])
}
return nil, fmt.Errorf("driver not found: %s", c.Driver)
}
func GenerateDocumentation(rootPath string, conf *resources.CatoConfig) (map[string]map[string][]*resources.FieldInfo, error) {
if rootPath == "" {
return nil, fmt.Errorf("cato: root path can't be empty")
}
if conf.CustomTag == "" {
conf.CustomTag = "docs"
}
fileList, err := listGoFiles(rootPath)
if err != nil {
return nil, fmt.Errorf("cato: error listing root path: %w", err)
}
exporterDriver, err := getDriver(conf)
exportConfigs := true
if err != nil {
// We don't export configs in this case
exportConfigs = false
}
filesConfigs := map[string]map[string][]*resources.FieldInfo{}
for _, file := range fileList {
configs, err := getConfigsToDocument(file, conf.CustomTag, rootPath)
if err != nil {
return nil, fmt.Errorf("cato: error parsing go file: %w", err)
}
if exportConfigs && len(configs) > 0 {
err = exporterDriver.ExportConfigs(configs, file, rootPath)
if err != nil {
return nil, fmt.Errorf("cato: error writing documentation: %w", err)
}
filesConfigs[file] = configs
}
}
return filesConfigs, nil
}