/
config_merge.go
71 lines (62 loc) · 1.86 KB
/
config_merge.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 ddevapp
import (
"fmt"
"github.com/drud/ddev/pkg/util"
"github.com/imdario/mergo"
"strings"
)
// mergeAdditionalConfigIntoApp takes the provided yaml `config.*.yaml` and merges
// it into "app"
func (app *DdevApp) mergeAdditionalConfigIntoApp(configPath string) error {
newConfig := DdevApp{}
err := newConfig.LoadConfigYamlFile(configPath)
if err != nil {
return err
}
// If override_config is set in the config.*.yaml, then just load it on top of app
// Otherwise (the normal default case) merge.
if newConfig.OverrideConfig {
err = app.LoadConfigYamlFile(configPath)
if err != nil {
return err
}
} else {
err = mergo.Merge(app, newConfig, mergo.WithAppendSlice, mergo.WithOverride)
if err != nil {
return err
}
}
// We don't need this set; it's only a flag to determine behavior above
app.OverrideConfig = false
// Make sure we don't have absolutely identical items in our resultant arrays
for _, arr := range []*[]string{&app.WebImageExtraPackages, &app.DBImageExtraPackages, &app.AdditionalHostnames, &app.AdditionalFQDNs, &app.OmitContainers} {
*arr = util.SliceToUniqueSlice(arr)
}
for _, arr := range []*[]string{&app.WebEnvironment} {
*arr = EnvToUniqueEnv(arr)
}
return nil
}
// EnvToUniqueEnv() makes sure that only the last occurrence of an env (NAME=val)
// slice is actually retained.
func EnvToUniqueEnv(inSlice *[]string) []string {
mapStore := map[string]string{}
newSlice := []string{}
for _, s := range *inSlice {
// config.yaml vars look like ENV1=val1 and ENV2=val2
// Split them and then make sure the last one wins
k, v, found := strings.Cut(s, "=")
// If we didn't find the "=" delimiter, it wasn't an env
if !found {
continue
}
mapStore[k] = v
}
for k, v := range mapStore {
newSlice = append(newSlice, fmt.Sprintf("%s=%v", k, v))
}
if len(newSlice) == 0 {
return nil
}
return newSlice
}