/
legacy_index_file.go
85 lines (62 loc) · 1.83 KB
/
legacy_index_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
package check
import (
"fmt"
"log"
"os"
"github.com/hashicorp/go-multierror"
)
type LegacyIndexFileOptions struct {
*FileOptions
FrontMatter *FrontMatterOptions
}
type LegacyIndexFileCheck struct {
FileCheck
Options *LegacyIndexFileOptions
}
func NewLegacyIndexFileCheck(opts *LegacyIndexFileOptions) *LegacyIndexFileCheck {
check := &LegacyIndexFileCheck{
Options: opts,
}
if check.Options == nil {
check.Options = &LegacyIndexFileOptions{}
}
if check.Options.FileOptions == nil {
check.Options.FileOptions = &FileOptions{}
}
if check.Options.FrontMatter == nil {
check.Options.FrontMatter = &FrontMatterOptions{}
}
check.Options.FrontMatter.NoSidebarCurrent = true
check.Options.FrontMatter.NoSubcategory = true
check.Options.FrontMatter.RequireDescription = true
check.Options.FrontMatter.RequireLayout = true
check.Options.FrontMatter.RequirePageTitle = true
return check
}
func (check *LegacyIndexFileCheck) Run(path string) error {
fullpath := check.Options.FullPath(path)
log.Printf("[DEBUG] Checking file: %s", fullpath)
if err := LegacyFileExtensionCheck(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)
}
return nil
}
func (check *LegacyIndexFileCheck) RunAll(files []string) error {
var result *multierror.Error
for _, file := range files {
if err := check.Run(file); err != nil {
result = multierror.Append(result, err)
}
}
return result.ErrorOrNil()
}