Support for multiple yaml documents in stdin/file

Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
pull/12029/head
Jan-Otto Kröpke 1 year ago
parent 2398830f18
commit c7d257b382
No known key found for this signature in database

@ -17,13 +17,14 @@ limitations under the License.
package values
import (
"bytes"
"io"
"net/url"
"os"
"strings"
"github.com/pkg/errors"
"sigs.k8s.io/yaml"
"k8s.io/apimachinery/pkg/util/yaml"
"helm.sh/helm/v3/pkg/getter"
"helm.sh/helm/v3/pkg/strvals"
@ -46,18 +47,26 @@ func (opts *Options) MergeValues(p getter.Providers) (map[string]interface{}, er
// User specified a values files via -f/--values
for _, filePath := range opts.ValueFiles {
currentMap := map[string]interface{}{}
bytes, err := readFile(filePath, p)
data, err := readFile(filePath, p)
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(bytes, &currentMap); err != nil {
return nil, errors.Wrapf(err, "failed to parse %s", filePath)
decoder := yaml.NewYAMLOrJSONDecoder(bytes.NewReader(data), 4096)
for {
currentMap := map[string]interface{}{}
err := decoder.Decode(&currentMap)
if err == io.EOF {
break
} else if err != nil {
return nil, errors.Wrapf(err, "failed to parse %s", filePath)
}
// Merge with the previous map
base = mergeMaps(base, currentMap)
}
// Merge with the previous map
base = mergeMaps(base, currentMap)
}
// User specified a value via --set-json

@ -17,6 +17,7 @@ limitations under the License.
package values
import (
"os"
"reflect"
"testing"
@ -86,3 +87,31 @@ func TestReadFile(t *testing.T) {
t.Errorf("Expected error when has special strings")
}
}
func TestMergeYaml(t *testing.T) {
var p getter.Providers
valuesFile, err := os.CreateTemp("", "values.yaml")
if err != nil {
t.Error(err)
}
_, err = valuesFile.WriteString("---\nkey1: value1\n---\nkey2: value2")
if err != nil {
t.Error(err)
}
options := Options{ValueFiles: []string{valuesFile.Name()}}
testMap, err := options.MergeValues(p)
if err != nil {
t.Error(err)
}
expectedMap := map[string]interface{}{
"key1": "value1",
"key2": "value2",
}
equal := reflect.DeepEqual(testMap, expectedMap)
if !equal {
t.Errorf("Expected a map with different keys to merge properly with another map. Expected: %v, got %v", expectedMap, testMap)
}
}

Loading…
Cancel
Save