generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 40
/
yaml.go
38 lines (33 loc) · 840 Bytes
/
yaml.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
package utils
import (
"bufio"
"bytes"
"io"
"strings"
utilyaml "k8s.io/apimachinery/pkg/util/yaml"
)
type Document = []byte
func SplitYamlDocuments(fileBytes Document) ([]Document, error) {
var documents [][]byte
reader := utilyaml.NewYAMLReader(bufio.NewReader(bytes.NewBuffer(fileBytes)))
for {
document, err := reader.Read()
if err == io.EOF || len(document) == 0 {
break
} else if err != nil {
return nil, err
}
documents = append(documents, []byte(document))
}
return documents, nil
}
// IsEmptyYamlDocument checks if a yaml document is empty (contains only comments)
func IsEmptyYamlDocument(document Document) bool {
for _, line := range strings.Split(string(document), "\n") {
line := strings.TrimSpace(line)
if line != "" && !strings.HasPrefix(line, "#") {
return false
}
}
return true
}