1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 07:57:07 +00:00
kyverno/pkg/utils/yaml/utils.go
Charles-Edouard Brétéché bc6a228f7d
refactor: separate yaml utils package (#3520)
Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>

Co-authored-by: Vyankatesh Kudtarkar <vyankateshkd@gmail.com>
2022-04-01 09:56:16 +00:00

28 lines
742 B
Go

package yaml
import (
"bufio"
"bytes"
"fmt"
"io"
"k8s.io/apimachinery/pkg/util/yaml"
)
// SplitDocuments reads the YAML bytes per-document, unmarshals the TypeMeta information from each document
// and returns a map between the GroupVersionKind of the document and the document bytes
func SplitDocuments(yamlBytes []byte) (documents [][]byte, error error) {
buf := bytes.NewBuffer(yamlBytes)
reader := yaml.NewYAMLReader(bufio.NewReader(buf))
for {
// Read one YAML document at a time, until io.EOF is returned
b, err := reader.Read()
if err == io.EOF || len(b) == 0 {
break
} else if err != nil {
return documents, fmt.Errorf("unable to read yaml")
}
documents = append(documents, b)
}
return documents, nil
}