/
options.go
71 lines (59 loc) · 1.57 KB
/
options.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
package values
import (
"io/ioutil"
"github.com/shikingram/adctl/pkg/chartutil"
"github.com/pkg/errors"
"sigs.k8s.io/yaml"
)
type Options struct {
ValueFiles []string
StringValues []string
Values []string
FileValues []string
}
// MergeValues merges values from files specified via -f/--values and directly
// via --set marshaling them to YAML
func (opts *Options) MergeValues() (chartutil.Values, error) {
base := chartutil.Values{}
// // local default values files
// defaultYaml, err := ioutil.ReadFile("values.yaml")
// if err != nil {
// return nil, err
// }
// if err := yaml.Unmarshal(defaultYaml, &base); err != nil {
// return nil, errors.Wrapf(err, "failed to parse default yaml")
// }
// User specified a values files via -f/--values
for _, filePath := range opts.ValueFiles {
currentMap := chartutil.Values{}
bytes, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(bytes, ¤tMap); err != nil {
return nil, errors.Wrapf(err, "failed to parse %s", filePath)
}
// Merge with the previous map
base = mergeMaps(base, currentMap)
}
// TODO: User specified a value via --set
return base, nil
}
func mergeMaps(a, b map[string]interface{}) map[string]interface{} {
out := make(map[string]interface{}, len(a))
for k, v := range a {
out[k] = v
}
for k, v := range b {
if v, ok := v.(map[string]interface{}); ok {
if bv, ok := out[k]; ok {
if bv, ok := bv.(map[string]interface{}); ok {
out[k] = mergeMaps(bv, v)
continue
}
}
}
out[k] = v
}
return out
}