-
Notifications
You must be signed in to change notification settings - Fork 90
/
yaml.go
35 lines (27 loc) · 958 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
package kotsutil
import (
"bytes"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/util"
yaml "github.com/replicatedhq/yaml/v3"
)
// FixUpYAML is a general purpose function that will ensure that YAML is compatible with KOTS
// This ensures that lines aren't wrapped at 80 chars which breaks template functions
func FixUpYAML(inputContent []byte) ([]byte, error) {
docs := bytes.Split(inputContent, []byte("\n---\n"))
fixedUpDocs := make([][]byte, 0)
for _, doc := range docs {
yamlObj := map[string]interface{}{}
err := yaml.Unmarshal(doc, &yamlObj)
if err != nil {
return nil, errors.Wrap(err, "failed to unmarshal yaml")
}
fixedUpDoc, err := util.MarshalIndent(2, yamlObj)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal yaml")
}
fixedUpDocs = append(fixedUpDocs, fixedUpDoc)
}
// MarshalIndent add a line break at the end of each file
return bytes.Join(fixedUpDocs, []byte("---\n")), nil
}