Skip to content

Commit

Permalink
Support for multiple yaml documents in stdin/file
Browse files Browse the repository at this point in the history
  • Loading branch information
jkroepke committed Apr 15, 2022
1 parent 49819b4 commit 2a887c7
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions pkg/cli/values/options.go
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package values

import (
"bytes"
"io/ioutil"
"net/url"
"os"
Expand Down Expand Up @@ -45,16 +46,18 @@ func (opts *Options) MergeValues(p getter.Providers) (map[string]interface{}, er
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)
for _, document := range splitYamlDocument(data) {
if err := yaml.Unmarshal(document, &currentMap); 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
Expand All @@ -74,11 +77,11 @@ func (opts *Options) MergeValues(p getter.Providers) (map[string]interface{}, er
// User specified a value via --set-file
for _, value := range opts.FileValues {
reader := func(rs []rune) (interface{}, error) {
bytes, err := readFile(string(rs), p)
data, err := readFile(string(rs), p)
if err != nil {
return nil, err
}
return string(bytes), err
return string(data), err
}
if err := strvals.ParseIntoFile(value, base, reader); err != nil {
return nil, errors.Wrap(err, "failed parsing --set-file data")
Expand Down Expand Up @@ -125,3 +128,7 @@ func readFile(filePath string, p getter.Providers) ([]byte, error) {
}
return data.Bytes(), err
}

func splitYamlDocument(data []byte) [][]byte {
return bytes.Split(data, []byte("\n---\n"))
}

0 comments on commit 2a887c7

Please sign in to comment.