-
Notifications
You must be signed in to change notification settings - Fork 3
/
checks.go
104 lines (90 loc) · 2.7 KB
/
checks.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
package doccompiler
import (
"fmt"
"github.com/pkg/errors"
"github.com/qlik-oss/gopherciser/generatedocs/pkg/common"
)
type check func(data *docData) []error
var checks = []check{
checkAllActionsDocumented,
checkAllConfigFieldsDocumented,
checkAllActionsInGroup,
}
func checkAndWarn(data *docData) {
for _, finding := range checkAll(data) {
fmt.Printf("WARNING: %v\n", finding)
}
}
func checkAll(data *docData) []error {
findings := []error{}
for _, check := range checks {
findings = append(findings, check(data)...)
}
return findings
}
func checkAllActionsDocumented(data *docData) []error {
findings := []error{}
allActions := common.ActionStrings()
for _, a := range allActions {
docEntry, ok := data.ActionMap[a]
if !ok {
findings = append(findings, errors.Errorf(`action "%s" is not documented`, a))
continue
}
if docEntry.Description == "" {
findings = append(findings, errors.Errorf(`action "%s" has no description`, a))
}
if docEntry.Examples == "" {
findings = append(findings, errors.Errorf(`action "%s" has no examples`, a))
}
}
return findings
}
func checkAllConfigFieldsDocumented(data *docData) []error {
findings := []error{}
// Get all config fields
expectedConfigFields, err := common.FieldsString()
if err != nil {
common.Exit(err, ExitCodeFailedConfigFields)
}
// Add documentation wrapping entire document as "main" entry into config map
expectedConfigFields = append(expectedConfigFields, "main")
for _, field := range expectedConfigFields {
docEntry, ok := data.ConfigMap[field]
if !ok {
findings = append(findings, errors.Errorf(`config field "%s" is not documented`, field))
continue
}
if docEntry.Description == "" {
findings = append(findings, errors.Errorf(`config field "%s" has no description`, field))
}
if docEntry.Examples == "" {
findings = append(findings, errors.Errorf(`config field "%s" has no examples`, field))
}
}
return findings
}
func checkAllActionsInGroup(data *docData) []error {
findings := []error{}
// map actions to groups
actionToGroups := map[string][]string{}
for _, action := range data.Actions {
actionToGroups[action] = []string{}
}
for _, group := range data.Groups {
for _, action := range group.Actions {
actionToGroups[action] = append(actionToGroups[action], group.Name)
}
}
// check action belongs to one and only one group
for action, groups := range actionToGroups {
lenGroups := len(groups)
switch {
case lenGroups == 0:
findings = append(findings, errors.Errorf(`action "%s" does not belong to a group`, action))
case lenGroups > 1:
findings = append(findings, errors.Errorf(`action "%s" belong to %d groups %v`, action, lenGroups, groups))
}
}
return findings
}