-
Notifications
You must be signed in to change notification settings - Fork 5
/
registry_resource_file.go
96 lines (70 loc) · 2.21 KB
/
registry_resource_file.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
package check
import (
"fmt"
"log"
"os"
"github.com/hashicorp/go-multierror"
)
type RegistryResourceFileOptions struct {
*FileOptions
Contents *ContentsOptions
FrontMatter *FrontMatterOptions
ProviderName string
}
type RegistryResourceFileCheck struct {
FileCheck
Options *RegistryResourceFileOptions
}
func NewRegistryResourceFileCheck(opts *RegistryResourceFileOptions) *RegistryResourceFileCheck {
check := &RegistryResourceFileCheck{
Options: opts,
}
if check.Options == nil {
check.Options = &RegistryResourceFileOptions{}
}
if check.Options.Contents == nil {
check.Options.Contents = &ContentsOptions{}
}
if check.Options.Contents.ProviderName == "" {
check.Options.Contents.ProviderName = check.Options.ProviderName
}
if check.Options.FileOptions == nil {
check.Options.FileOptions = &FileOptions{}
}
if check.Options.FrontMatter == nil {
check.Options.FrontMatter = &FrontMatterOptions{}
}
check.Options.FrontMatter.NoLayout = true
check.Options.FrontMatter.NoSidebarCurrent = true
return check
}
func (check *RegistryResourceFileCheck) Run(path string, exampleLanguage string) error {
fullpath := check.Options.FullPath(path)
log.Printf("[DEBUG] Checking file: %s", fullpath)
if err := RegistryFileExtensionCheck(path); err != nil {
return fmt.Errorf("%s: error checking file extension: %w", path, err)
}
if err := FileSizeCheck(fullpath); err != nil {
return fmt.Errorf("%s: error checking file size: %w", path, err)
}
content, err := os.ReadFile(fullpath)
if err != nil {
return fmt.Errorf("%s: error reading file: %w", path, err)
}
if err := NewFrontMatterCheck(check.Options.FrontMatter).Run(content); err != nil {
return fmt.Errorf("%s: error checking file frontmatter: %w", path, err)
}
if err := NewContentsCheck(check.Options.Contents).Run(fullpath, exampleLanguage); err != nil {
return fmt.Errorf("%s: error checking file contents: %w", path, err)
}
return nil
}
func (check *RegistryResourceFileCheck) RunAll(files []string, exampleLanguage string) error {
var result *multierror.Error
for _, file := range files {
if err := check.Run(file, exampleLanguage); err != nil {
result = multierror.Append(result, err)
}
}
return result.ErrorOrNil()
}