-
Notifications
You must be signed in to change notification settings - Fork 5
/
file_mismatch.go
148 lines (111 loc) · 3.17 KB
/
file_mismatch.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
package check
import (
"fmt"
"log"
"github.com/hashicorp/go-multierror"
)
type FileMismatchOptions struct {
*FileOptions
IgnoreFileMismatch []string
IgnoreFileMissing []string
ProviderName string
ResourceType string
ResourceNames []string
}
type FileMismatchCheck struct {
Options *FileMismatchOptions
}
func NewFileMismatchCheck(opts *FileMismatchOptions) *FileMismatchCheck {
check := &FileMismatchCheck{
Options: opts,
}
if check.Options == nil {
check.Options = &FileMismatchOptions{}
}
if check.Options.FileOptions == nil {
check.Options.FileOptions = &FileOptions{}
}
return check
}
func (check *FileMismatchCheck) Run(files []string) error {
if len(files) == 0 {
log.Printf("[DEBUG] Skipping %s file mismatch checks due to missing file list", check.Options.ResourceType)
return nil
}
if len(check.Options.ResourceNames) == 0 {
log.Printf("[DEBUG] Skipping %s file mismatch checks, no resources found", check.Options.ResourceType)
return nil
}
var extraFiles []string
var missingFiles []string
for _, file := range files {
if fileHasResource(check.Options.ResourceNames, check.Options.ProviderName, file) {
continue
}
if check.IgnoreFileMismatch(file) {
continue
}
extraFiles = append(extraFiles, file)
}
for _, resourceName := range check.Options.ResourceNames {
if resourceHasFile(files, check.Options.ProviderName, resourceName) {
continue
}
if check.IgnoreFileMissing(resourceName) {
continue
}
missingFiles = append(missingFiles, resourceName)
}
var result *multierror.Error
for _, extraFile := range extraFiles {
err := fmt.Errorf("matching %s for documentation file (%s) not found, file is extraneous or incorrectly named", check.Options.ResourceType, extraFile)
result = multierror.Append(result, err)
}
for _, missingFile := range missingFiles {
err := fmt.Errorf("missing documentation file for %s: %s", check.Options.ResourceType, missingFile)
result = multierror.Append(result, err)
}
return result.ErrorOrNil()
}
func (check *FileMismatchCheck) IgnoreFileMismatch(file string) bool {
for _, ignoreResourceName := range check.Options.IgnoreFileMismatch {
if ignoreResourceName == fileResourceName(check.Options.ProviderName, file) {
return true
}
}
return false
}
func (check *FileMismatchCheck) IgnoreFileMissing(resourceName string) bool {
for _, ignoreResourceName := range check.Options.IgnoreFileMissing {
if ignoreResourceName == resourceName {
return true
}
}
return false
}
func fileHasResource(resourceNames []string, providerName, file string) bool {
for _, name := range resourceNames {
if name == fileResourceName(providerName, file) {
return true
}
}
return false
}
func fileResourceName(providerName, fileName string) string {
resourceSuffix := TrimFileExtension(fileName)
// providerName is empty for functions
if providerName == "" {
return resourceSuffix
}
return fmt.Sprintf("%s_%s", providerName, resourceSuffix)
}
func resourceHasFile(files []string, providerName, resourceName string) bool {
var found bool
for _, file := range files {
if fileResourceName(providerName, file) == resourceName {
found = true
break
}
}
return found
}