This repository was archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathtemplateInfo.go
162 lines (150 loc) · 3.86 KB
/
templateInfo.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
package models
import (
"bytes"
"fmt"
"text/template"
"github.com/Masterminds/sprig/v3"
)
func DrpSafeFuncMap() template.FuncMap {
gfm := sprig.GenericFuncMap()
// Dangerous
delete(gfm, "env")
delete(gfm, "expandenv")
// Misleading
delete(gfm, "ago")
delete(gfm, "now")
return template.FuncMap(gfm)
}
// TemplateInfo holds information on the templates in the boot
// environment that will be expanded into files.
//
// swagger:model
type TemplateInfo struct {
// Name of the template
//
// required: true
Name string
// A text/template that specifies how to create
// the final path the template should be
// written to.
//
// required: true
Path string
// Link optionally references another file to put at
// the path location.
Link string
// ID of the template that should be expanded. Either
// this or Contents should be set
//
// required: false
ID string
// Contents that should be used when this template needs
// to be expanded. Either this or ID should be set.
//
// required: false
Contents string
// Meta for the TemplateInfo. This can be used by the job running
// system and the bootenvs to handle OS, arch, and firmware differences.
//
// required: false
Meta map[string]string
PathTmpl *template.Template `json:"-"`
LinkTmpl *template.Template `json:"-"`
}
func (ti *TemplateInfo) Id() string {
if ti.ID == "" {
return ti.Name
}
return ti.ID
}
func (ti *TemplateInfo) SanityCheck(idx int, e ErrorAdder, missingPathOK bool) {
if ti.Name == "" {
e.Errorf("Template[%d] is missing a Name", idx)
}
if !missingPathOK {
if ti.Path == "" {
e.Errorf("Template[%d] is missing a Path", idx)
} else if _, err := template.New(ti.Name).Funcs(DrpSafeFuncMap()).Parse(ti.Path); err != nil {
e.Errorf("Template[%d] Path is not a valid text/template: %v", idx, err)
}
}
if ti.Contents == "" && ti.ID == "" && ti.Link == "" {
e.Errorf("Template[%d] must have either an ID, a Link, or Contents set", idx)
}
if ti.Contents != "" && ti.ID != "" {
e.Errorf("Template[%d] has both an ID and Contents", idx)
}
if ti.Meta == nil {
ti.Meta = map[string]string{}
}
}
func (ti *TemplateInfo) PathTemplate() *template.Template {
return ti.PathTmpl
}
func (ti *TemplateInfo) LinkTemplate() *template.Template {
return ti.LinkTmpl
}
func MergeTemplates(root *template.Template, tmpls []TemplateInfo, e ErrorAdder) *template.Template {
var res *template.Template
var err error
if root == nil {
res = template.New("").Funcs(DrpSafeFuncMap())
} else {
res, err = root.Clone()
}
if err != nil {
e.Errorf("Error cloning root: %v", err)
return nil
}
buf := &bytes.Buffer{}
for i := range tmpls {
ti := &tmpls[i]
if ti.Name == "" {
e.Errorf("Templates[%d] has no Name", i)
continue
}
if ti.Path != "" {
pathTmpl, err := template.New(ti.Name).Funcs(DrpSafeFuncMap()).Parse(ti.Path)
if err != nil {
e.Errorf("Error compiling path template %s (%s): %v",
ti.Name,
ti.Path,
err)
continue
} else {
ti.PathTmpl = pathTmpl.Option("missingkey=error")
}
}
if ti.ID != "" {
if res.Lookup(ti.ID) == nil {
e.Errorf("Templates[%d]: No common template for %s", i, ti.ID)
}
continue
}
if ti.Link != "" {
if ti.Path == "" {
e.Errorf("Templates[%d]: Path is empty but link is specified %s", i, ti.Name)
continue
}
linkTmpl, err := template.New(ti.Name).Funcs(DrpSafeFuncMap()).Parse(ti.Link)
if err != nil {
e.Errorf("Error compiling link template %s (%s): %v",
ti.Name,
ti.Link,
err)
} else {
ti.LinkTmpl = linkTmpl.Option("missingkey=error")
}
continue
}
if ti.Contents == "" {
e.Errorf("Templates[%d] has both an empty ID, Link, and contents", i)
}
fmt.Fprintf(buf, `{{define "%s"}}%s{{end}}\n`, ti.Name, ti.Contents)
}
_, err = res.Parse(buf.String())
if err != nil {
e.Errorf("Error parsing inline templates: %v", err)
}
return res
}