-
Notifications
You must be signed in to change notification settings - Fork 88
/
config_context.go
110 lines (89 loc) · 2.65 KB
/
config_context.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
package template
import (
"encoding/base64"
"fmt"
"text/template"
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
)
func (b *Builder) NewConfigContext(configGroups []kotsv1beta1.ConfigGroup, templateContext map[string]interface{}) (*ConfigCtx, error) {
configCtx := &ConfigCtx{
ItemValues: templateContext,
}
for _, configGroup := range configGroups {
for _, configItem := range configGroup.Items {
// if the pending value is different from the built, then use the pending every time
// We have to ignore errors here because we only have the static context loaded
// for rendering. some items have templates that need the config context,
// so we can ignore these.
builtDefault, _ := b.String(configItem.Default)
builtValue, _ := b.String(configItem.Value)
var built string
if builtValue != "" {
built = builtValue
} else {
built = builtDefault
}
if v, ok := templateContext[configItem.Name]; ok {
built = fmt.Sprintf("%s", v)
}
configCtx.ItemValues[configItem.Name] = built
}
}
return configCtx, nil
}
// ConfigCtx is the context for builder functions before the application has started.
type ConfigCtx struct {
ItemValues map[string]interface{}
}
// FuncMap represents the available functions in the ConfigCtx.
func (ctx ConfigCtx) FuncMap() template.FuncMap {
return template.FuncMap{
"ConfigOption": ctx.configOption,
"ConfigOptionIndex": ctx.configOptionIndex,
"ConfigOptionData": ctx.configOptionData,
"ConfigOptionEquals": ctx.configOptionEquals,
"ConfigOptionNotEquals": ctx.configOptionNotEquals,
}
}
func (ctx ConfigCtx) configOption(name string) string {
v, err := ctx.getConfigOptionValue(name)
if err != nil {
return ""
}
return v
}
func (ctx ConfigCtx) configOptionIndex(name string) string {
return ""
}
func (ctx ConfigCtx) configOptionData(name string) string {
v, err := ctx.getConfigOptionValue(name)
if err != nil {
return ""
}
decoded, err := base64.StdEncoding.DecodeString(v)
if err != nil {
return ""
}
return string(decoded)
}
func (ctx ConfigCtx) configOptionEquals(name string, value string) bool {
val, err := ctx.getConfigOptionValue(name)
if err != nil {
return false
}
return value == val
}
func (ctx ConfigCtx) configOptionNotEquals(name string, value string) bool {
val, err := ctx.getConfigOptionValue(name)
if err != nil {
return false
}
return value != val
}
func (ctx ConfigCtx) getConfigOptionValue(itemName string) (string, error) {
if val, ok := ctx.ItemValues[itemName]; ok {
return fmt.Sprintf("%s", val), nil
}
return "", errors.New("unable to find config item")
}