forked from terakilobyte/checker
/
collectRST.go
229 lines (197 loc) · 5.45 KB
/
collectRST.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
package collectors
import (
"os"
"path/filepath"
"regexp"
"strings"
"github.com/MongoCaleb/checker/internal/parsers/rst"
"github.com/MongoCaleb/checker/internal/sources"
iowrap "github.com/spf13/afero"
log "github.com/sirupsen/logrus"
)
var (
FS iowrap.Fs
FSUtil *iowrap.Afero
basepath string
sharedConstantRegex = regexp.MustCompile(`\{\+([[:alnum:]\p{P}\p{S}]+)\+\}`)
)
func init() {
FS = iowrap.NewOsFs()
FSUtil = &iowrap.Afero{Fs: FS}
}
func exists(path string) bool {
if _, err := FS.Stat(path); os.IsNotExist(err) {
log.Errorf("%s does not exist", path)
return false
}
return true
}
func snootyTomlExists(path string) bool {
return exists(filepath.Join(path, "snooty.toml"))
}
func sourceDirectoryExists(path string) bool {
return exists(filepath.Join(path, "source"))
}
func GatherFiles(path string) []string {
basepath = path
if !snootyTomlExists(path) || !sourceDirectoryExists(path) {
log.Panic("snooty.toml or source directory does not exist")
}
files := make([]string, 0)
// TODO: make this passable as a flag with these defaults
exts := []string{".rst", ".txt", ".yml", ".yaml"}
validExt := func(s string) bool {
for _, ext := range exts {
if strings.Contains(s, ext) {
return true
}
}
return false
}
err := FSUtil.Walk(basepath, func(path string, info os.FileInfo, err error) error {
if info.IsDir() && info.Name() == "draft" {
return filepath.SkipDir
}
if validExt(filepath.Ext(path)) {
files = append(files, path)
}
return nil
})
if err != nil {
log.Panic(err)
}
return files
}
func gather(files []string, fn func(filename string, data []byte)) {
for _, file := range files {
dat, err := FSUtil.ReadFile(file)
if err != nil {
log.Panic(err)
}
fileName := strings.Replace(file, basepath, "", 1)
fn(fileName, dat)
}
}
type RstRoleMap map[rst.RstRole]string
func GatherRoles(files []string) RstRoleMap {
roles := make(map[rst.RstRole]string, len(files))
gather(files, func(filename string, data []byte) {
for _, role := range rst.ParseForRoles(data) {
roles[role] = filename
}
})
return roles
}
func (r *RstRoleMap) Get(key string) (*rst.RstRole, bool) {
for k := range *r {
if k.Target == key {
return &k, true
}
}
return nil, false
}
func (r *RstRoleMap) Union(other RstRoleMap) *RstRoleMap {
for k, v := range other {
(*r)[k] = v
}
return r
}
func GatherConstants(files []string) map[rst.RstConstant]string {
consts := make(map[rst.RstConstant]string, len(files))
gather(files, func(filename string, data []byte) {
for _, con := range rst.ParseForConstants(data) {
consts[con] = filename
}
})
return consts
}
func GatherHTTPLinks(files []string) map[rst.RstHTTPLink]string {
links := make(map[rst.RstHTTPLink]string, len(files))
gather(files, func(filename string, data []byte) {
for _, link := range rst.ParseForHTTPLinks(data) {
links[link] = filename
}
})
return links
}
type RefTargetMap map[rst.RefTarget]string
func GatherLocalRefs(files []string) RefTargetMap {
refs := make(map[rst.RefTarget]string, len(files))
gather(files, func(filename string, data []byte) {
for _, ref := range rst.ParseForLocalRefs(data) {
refs[ref] = filename
}
})
return refs
}
func (r *RefTargetMap) Get(ref *rst.RstRole) (*rst.RefTarget, bool) {
for k := range *r {
if k.Name == ref.Target {
return &k, true
}
}
return nil, false
}
func (r *RefTargetMap) Union(other RefTargetMap) *RefTargetMap {
for k, v := range other {
(*r)[k] = v
}
return r
}
// Snooty does magic and converts tsl to ssl in refs, so we have to mimic it
func (r RefTargetMap) SSLToTLS() RefTargetMap {
for k, v := range r {
if strings.Contains(k.Name, "ssl") {
tlsK := rst.RefTarget{Name: strings.Replace(k.Name, "ssl", "tls", 1)}
r[tlsK] = v
}
}
return r
}
func GatherSharedIncludes(files []string) []rst.SharedInclude {
includes := make([]rst.SharedInclude, 0)
gather(files, func(filename string, data []byte) {
includes = append(includes, rst.ParseForSharedIncludes(data)...)
})
return includes
}
func GatherSharedRefs(input []byte, defs sources.TomlConfig) RstRoleMap {
roles := make(RstRoleMap, len(input))
for _, role := range rst.ParseForRoles(input) {
allFound := sharedConstantRegex.FindAllString(role.Target, -1)
for _, match := range allFound {
for _, inner := range sharedConstantRegex.FindAllStringSubmatch(match, -1) {
role.Target = strings.Replace(role.Target, inner[0], defs.Constants[inner[1]], 1)
}
}
roles[role] = "shared"
}
return roles
}
func GatherSharedLocalRefs(input []byte, defs sources.TomlConfig) RefTargetMap {
refs := make(map[rst.RefTarget]string, len(input))
for _, ref := range rst.ParseForLocalRefs(input) {
allFound := sharedConstantRegex.FindAllString(ref.Name, -1)
for _, match := range allFound {
for _, inner := range sharedConstantRegex.FindAllStringSubmatch(match, -1) {
ref.Name = strings.Replace(ref.Name, inner[0], defs.Constants[inner[1]], 1)
}
}
refs[ref] = "shared"
}
return refs
}
func (r RstRoleMap) ConvertConstants(defs *sources.TomlConfig) RstRoleMap {
for k, v := range r {
allFound := sharedConstantRegex.FindAllString(k.Target, -1)
for _, match := range allFound {
for _, inner := range sharedConstantRegex.FindAllStringSubmatch(match, -1) {
delete(r, k)
k.Target = strings.Replace(k.Target, inner[0], defs.Constants[inner[1]], 1)
k.Name = strings.Replace(k.Name, inner[0], defs.Constants[inner[1]], 1)
r[k] = v
}
}
}
return r
}