-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
76 lines (62 loc) · 1.75 KB
/
common.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
package template
import (
"github.com/bmatcuk/doublestar/v4"
"github.com/pkg/errors"
"github.com/hckops/hckctl/pkg/schema"
"github.com/hckops/hckctl/pkg/util"
)
func readRawTemplate(path string) (*RawTemplate, error) {
data, err := util.ReadFile(path)
if err != nil {
return nil, errors.Wrapf(err, "template not found %s", path)
}
kind, err := schema.ValidateAll(data)
if err != nil {
return nil, errors.Wrapf(err, "invalid schema %s", data)
}
return &RawTemplate{kind, data}, nil
}
func readTemplates(wildcard string) ([]*TemplateValidated, error) {
// https://github.com/golang/go/issues/11862
paths, err := doublestar.FilepathGlob(wildcard,
doublestar.WithFailOnPatternNotExist(), doublestar.WithFilesOnly(), doublestar.WithNoFollow())
if err != nil {
return nil, errors.Wrap(err, "invalid wildcard")
}
// validate all matching templates
var results []*TemplateValidated
for _, path := range paths {
if value, err := readRawTemplate(path); err != nil {
results = append(results, (&RawTemplate{}).toValidated(path, false))
} else {
results = append(results, value.toValidated(path, true))
}
}
return results, nil
}
func readTemplate[T TemplateType](path string) (*TemplateValue[T], error) {
raw, err := readRawTemplate(path)
if err != nil {
return nil, err
}
value, err := decodeFromYaml[T](raw.Data)
if err != nil {
return nil, err
}
return &TemplateValue[T]{
Kind: raw.Kind,
Data: value,
}, nil
}
func readTemplateInfo[T TemplateType](sourceType SourceType, path string, revision string) (*TemplateInfo[T], error) {
value, err := readTemplate[T](path)
if err != nil {
return nil, err
}
return &TemplateInfo[T]{
Value: value,
SourceType: sourceType,
Path: path,
Revision: revision,
}, nil
}