/
sauce.go
169 lines (140 loc) · 4.25 KB
/
sauce.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package recipe
import (
"fmt"
"path/filepath"
"strings"
"text/template"
"github.com/fatih/structs"
"github.com/gofrs/uuid"
)
// Sauce represents a rendered recipe
type Sauce struct {
// Version of the sauce API schema. Currently should have value "v1"
APIVersion string `yaml:"apiVersion"`
// The recipe which was used to render the sauce
Recipe Recipe `yaml:"recipe"`
// Values which was used to execute the recipe
Values VariableValues `yaml:"values,omitempty"`
// Files genereated from the recipe
Files map[string]File `yaml:"files"`
// Random unique ID whose value is determined on first render and stays the same
// on subsequent re-renders (upgrades) of the sauce. Can be used for example as a seed
// for template random functions to provide same result on each template
ID uuid.UUID `yaml:"id"`
// SubPath is used as a prefix when saving and loading the files rendered by the sauce.
// This is useful for example in monorepos where the sauce is rendered to a subdirectory of the project directory.
SubPath string `yaml:"subPath,omitempty"`
// CheckFrom defines the repository where updates should be checked for the recipe
CheckFrom string `yaml:"from,omitempty"`
}
type RecipeConflict struct {
Path string
Sha256Sum string
OtherSha256Sum string
}
const (
SaucesFileName = "sauces"
// The directory name which contains all Jalapeno related files
// in the project directory
SauceDirName = ".jalapeno"
)
func NewSauce() *Sauce {
return &Sauce{
APIVersion: "v1",
}
}
func (s *Sauce) Validate() error {
if s.APIVersion != "v1" {
return fmt.Errorf("unreconized sauce API version \"%s\"", s.APIVersion)
}
if s.ID.IsNil() {
return fmt.Errorf("sauce ID was empty")
}
if s.CheckFrom != "" && !strings.HasPrefix(s.CheckFrom, "oci://") {
return fmt.Errorf("currently recipe updates can only be checked from OCI repositories, got: %s", s.CheckFrom)
}
if err := ValidateSubpath(s.SubPath); err != nil {
return err
}
if err := s.Recipe.Validate(); err != nil {
return fmt.Errorf("sauce recipe was invalid: %w", err)
}
for _, variable := range s.Recipe.Variables {
if _, found := s.Values[variable.Name]; !(variable.Optional || variable.If != "") && !found {
return fmt.Errorf("sauce did not have value for required variable '%s'", variable.Name)
}
}
return nil
}
func ValidateSubpath(path string) error {
p := filepath.Clean(path)
switch {
case p == ".":
return nil
case filepath.IsAbs(p):
return fmt.Errorf("subPath must be a relative path, got: %s", path)
case strings.Contains(p, ".."):
return fmt.Errorf("subPath must point to a directory inside the project root, got: %s", path)
default:
return nil
}
}
// Check if the recipe conflicts with another recipe. Recipes conflict if they touch the same files.
func (s *Sauce) Conflicts(other *Sauce) []RecipeConflict {
if s.SubPath != other.SubPath {
return nil
}
var conflicts []RecipeConflict
for path, file := range s.Files {
if otherFile, exists := other.Files[path]; exists {
conflicts = append(
conflicts,
RecipeConflict{
Path: path,
Sha256Sum: file.Checksum,
OtherSha256Sum: otherFile.Checksum,
})
}
}
return conflicts
}
func (s *Sauce) CreateTemplateContext() (map[string]interface{}, error) {
if err := s.Validate(); err != nil {
return nil, err
}
mappedValues := make(VariableValues)
for name, value := range s.Values {
switch value := value.(type) {
// Map table to more convenient format
case TableValue:
mappedValues[name] = value.ToMapSlice()
default:
mappedValues[name] = value
}
}
return structs.Map(TemplateContext{
ID: s.ID.String(),
Recipe: struct{ APIVersion, Name, Version, Source string }{
s.Recipe.APIVersion,
s.Recipe.Name,
s.Recipe.Version,
s.Recipe.Source,
},
Variables: mappedValues,
}), nil
}
func (s *Sauce) RenderInitHelp() (string, error) {
context, err := s.CreateTemplateContext()
if err != nil {
return "", err
}
t, err := template.New("initHelp").Parse(s.Recipe.InitHelp)
if err != nil {
return "", fmt.Errorf("failed to parse initHelp template: %w", err)
}
var buf strings.Builder
if err := t.Execute(&buf, context); err != nil {
return "", fmt.Errorf("failed to render initHelp template: %w", err)
}
return buf.String(), nil
}