-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.go
47 lines (39 loc) · 1.07 KB
/
template.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
package registry
import (
"fmt"
"regexp"
polarisv1alpha1 "github.com/RicochetStudios/polaris/apis/v1alpha1"
)
const (
// templateRegex is a regular expression to validate templates.
templateRegex string = `^{{ (?P<tpl>(\.\w+)*) }}$`
)
// TemplateValue takes a value and resolves its template if it is a template.
// If the value is not a template, it returns the value as is.
func TemplateValue(v string, s Schema, i polarisv1alpha1.ServerSpec) string {
// Template the env var if needed.
re, err := regexp.Compile(templateRegex)
// If the regex is invalid, return an empty string.
if err != nil {
return ""
}
if re.MatchString(v) {
// Get the template to target.
matches := re.FindStringSubmatch(v)
tplIndex := re.SubexpIndex("tpl")
tpl := matches[tplIndex]
// Resolve the templates.
switch tpl {
case ".name":
return i.Name
case ".modLoader":
return i.Game.ModLoader
case ".players":
return fmt.Sprint(s.Sizes[i.Size].Players)
case ".version":
return i.Game.Version
}
}
// If it is not a template, return an empty string.
return v
}