fix value merging & json unmarshalling

This commit is contained in:
Diego Fernando Carrión 2023-10-13 13:07:10 +02:00
parent b474823853
commit 5f76bca8a3
Signed by: CRThaze
GPG Key ID: 0F647F8A6A3AF588
3 changed files with 17 additions and 11 deletions

1
go.mod
View File

@ -4,6 +4,7 @@ go 1.18
require ( require (
github.com/Masterminds/sprig/v3 v3.2.3 github.com/Masterminds/sprig/v3 v3.2.3
github.com/peterbourgon/mergemap v0.0.1
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
) )

2
go.sum
View File

@ -17,6 +17,8 @@ github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMK
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY= github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/peterbourgon/mergemap v0.0.1 h1:5/brtSACv34REV0xoYjPQ8JXZnx3nurGt6WInLRwqX4=
github.com/peterbourgon/mergemap v0.0.1/go.mod h1:jQyRpOpE/KbvPc0VKXjAqctYglwUO5W6zAcGcFfbvlo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ= github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=

25
main.go
View File

@ -7,12 +7,13 @@ import (
"html/template" "html/template"
"io" "io"
"io/fs" "io/fs"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
"github.com/peterbourgon/mergemap"
sprig "github.com/Masterminds/sprig/v3" sprig "github.com/Masterminds/sprig/v3"
yaml "gopkg.in/yaml.v3" yaml "gopkg.in/yaml.v3"
) )
@ -97,28 +98,30 @@ func expandFiles(files []string) (expFiles []string) {
func mergeValues() (result map[string]interface{}) { func mergeValues() (result map[string]interface{}) {
result = map[string]interface{}{} result = map[string]interface{}{}
for _, vf := range flags.valuesFiles { for _, vf := range flags.valuesFiles {
vfContent, err := ioutil.ReadFile(vf) vfContent, err := os.ReadFile(vf)
checkErr(err) checkErr(err)
if strings.HasSuffix(vf, ".json") { if strings.HasSuffix(vf, ".json") {
err = json.Unmarshal(vfContent, result) err = json.Unmarshal(vfContent, &result)
checkErr(err) checkErr(err)
} else { } else {
err = yaml.Unmarshal(vfContent, result) err = yaml.Unmarshal(vfContent, result)
checkErr(err) checkErr(err)
} }
} }
for i, v := range flags.values { for _, v := range flags.values {
subMap := result valueMap := map[string]interface{}{}
for _, node := range v.address { subMap := valueMap
// fmt.Fprintf(os.Stderr, "113: %+v\n", subMap)
for i, node := range v.address {
if i == len(v.address)-1 { if i == len(v.address)-1 {
subMap[node] = v.v subMap[node] = v.v
} else { break
if _, ok := subMap[node]; !ok {
subMap[node] = map[string]interface{}{}
}
subMap = subMap[node].(map[string]interface{})
} }
subMap[node] = map[string]interface{}{}
// fmt.Fprintf(os.Stderr, "120: %+v\n", subMap)
subMap = subMap[node].(map[string]interface{})
} }
mergemap.Merge(result, valueMap)
} }
return return
} }