/
main.go
122 lines (102 loc) · 2.56 KB
/
main.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
package main
import (
"bufio"
"bytes"
"flag"
"os"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
func main() {
sourceFlag := flag.Bool("source", false, "include location of linter code that failed assertion")
flag.Parse()
var err error
pass := true
for _, filename := range flag.Args() {
var filePass bool
filePass, err = checkFile(filename, guessPluginType(filename), *sourceFlag)
if err != nil {
panic(err)
}
pass = pass && filePass
}
if !pass {
os.Exit(1)
}
}
type ruleFunc func(*T, ast.Node) error
type rulesMap map[plugin][]ruleFunc
var rules rulesMap
func init() {
rules = make(rulesMap)
//rules for all plugin types
all := []ruleFunc{
firstSection,
noLongLinesInParagraphs(80),
configSection,
relativeTelegrafLinks,
}
for i := pluginInput; i <= pluginParser; i++ {
rules[i] = all
}
inputRules := []ruleFunc{
requiredSectionsClose([]string{
"Example Output",
"Metrics",
}),
}
rules[pluginInput] = append(rules[pluginInput], inputRules...)
}
func checkFile(filename string, pluginType plugin, sourceFlag bool) (bool, error) {
md, err := os.ReadFile(filename)
if err != nil {
return false, err
}
// Goldmark returns locations as offsets. We want line
// numbers. Find the newlines in the file so we can translate
// later.
scanner := bufio.NewScanner(bytes.NewReader(md))
scanner.Split(bufio.ScanRunes)
offset := 0
newlineOffsets := []int{}
for scanner.Scan() {
if scanner.Text() == "\n" {
newlineOffsets = append(newlineOffsets, offset)
}
offset++
}
p := goldmark.DefaultParser()
// We need goldmark to parse tables, otherwise they show up as
// paragraphs. Since tables often have long lines and we check for long
// lines in paragraphs, without table parsing there are false positive long
// lines in tables.
//
// The tableParagraphTransformer is an extension and not part of the default
// parser so we add it. There may be an easier way to do it, but this works:
p.AddOptions(
parser.WithParagraphTransformers(
util.Prioritized(extension.NewTableParagraphTransformer(), 99),
),
)
r := text.NewReader(md)
root := p.Parse(r)
rules := rules[pluginType]
tester := T{
filename: filename,
markdown: md,
newlineOffsets: newlineOffsets,
sourceFlag: sourceFlag,
}
for _, rule := range rules {
err = rule(&tester, root)
if err != nil {
return false, err
}
}
tester.printPassFail()
return tester.pass(), nil
}