-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathprovider_file.go
More file actions
70 lines (52 loc) · 1.58 KB
/
provider_file.go
File metadata and controls
70 lines (52 loc) · 1.58 KB
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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package check
import (
"fmt"
"io/fs"
"log"
"path/filepath"
)
type ProviderFileOptions struct {
*FileOptions
FrontMatter *FrontMatterOptions
ValidExtensions []string
}
type ProviderFileCheck struct {
Options *ProviderFileOptions
ProviderFs fs.FS
}
func NewProviderFileCheck(providerFs fs.FS, opts *ProviderFileOptions) *ProviderFileCheck {
check := &ProviderFileCheck{
Options: opts,
ProviderFs: providerFs,
}
if check.Options == nil {
check.Options = &ProviderFileOptions{}
}
if check.Options.FileOptions == nil {
check.Options.FileOptions = &FileOptions{}
}
if check.Options.FrontMatter == nil {
check.Options.FrontMatter = &FrontMatterOptions{}
}
return check
}
func (check *ProviderFileCheck) Run(path string) error {
fullpath := check.Options.FullPath(path)
log.Printf("[DEBUG] Checking file: %s", fullpath)
if err := FileExtensionCheck(path, check.Options.ValidExtensions); err != nil {
return fmt.Errorf("%s: error checking file extension: %w", filepath.FromSlash(path), err)
}
if err := FileSizeCheck(check.ProviderFs, path); err != nil {
return fmt.Errorf("%s: error checking file size: %w", filepath.FromSlash(path), err)
}
content, err := fs.ReadFile(check.ProviderFs, path)
if err != nil {
return fmt.Errorf("%s: error reading file: %w", filepath.FromSlash(path), err)
}
if err := NewFrontMatterCheck(check.Options.FrontMatter).Run(content); err != nil {
return fmt.Errorf("%s: error checking file frontmatter: %w", filepath.FromSlash(path), err)
}
return nil
}