-
Notifications
You must be signed in to change notification settings - Fork 88
/
splitter.go
65 lines (50 loc) · 1.4 KB
/
splitter.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package util
import (
"bytes"
"fmt"
"strings"
"github.com/pkg/errors"
yaml "github.com/replicatedhq/yaml/v3"
)
type OverlySimpleGVK struct {
APIVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Metadata OverlySimpleMetadata `yaml:"metadata"`
}
type OverlySimpleMetadata struct {
Name string `yaml:"name"`
Namespace string `yaml:"namespace"`
}
func SplitYAML(input []byte) (map[string][]byte, error) {
outputFiles := map[string][]byte{}
docs := ConvertToSingleDocs(input)
for _, doc := range docs {
if bytes.HasPrefix(doc, []byte("---\n")) {
doc = doc[4:]
}
if len(doc) == 0 {
continue
}
filename, err := generateName(outputFiles, doc, 0)
if err != nil {
return nil, errors.Wrap(err, "failed to generate name")
}
outputFiles[filename] = doc
}
return outputFiles, nil
}
func generateName(outputFiles map[string][]byte, content []byte, suffix int) (string, error) {
o := OverlySimpleGVK{}
if err := yaml.Unmarshal(content, &o); err != nil {
return "", errors.Wrap(err, "failed to unmarshal yaml")
}
filename := fmt.Sprintf("%s-%s", o.Metadata.Name, strings.ToLower(o.Kind))
if suffix > 0 {
filename = fmt.Sprintf("%s-%d", filename, suffix)
}
filename = fmt.Sprintf("%s.yaml", filename)
if _, exists := outputFiles[filename]; exists {
return generateName(outputFiles, content, suffix+1)
}
return filename, nil
}