-
Notifications
You must be signed in to change notification settings - Fork 5
/
yaml.go
106 lines (88 loc) · 2.64 KB
/
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
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) bwplotka/mimic Authors
// Licensed under the Apache License 2.0.
package encoding
import (
"bytes"
"errors"
"fmt"
"io"
"strings"
ghodssyaml "github.com/ghodss/yaml"
yaml2 "gopkg.in/yaml.v3"
yaml3 "gopkg.in/yaml.v3"
)
// yamlEncoder implements the Encoder interface.
type yamlEncoder struct {
io.Reader
}
// EncodeComment returns byte slice that represents a YAML comment.
// We split `lines` by '\n' and encode as a single/multi line comment.
func (yamlEncoder) EncodeComment(lines string) []byte {
commentLines := strings.Split(lines, "\n")
finalString := ""
for _, comment := range commentLines {
if comment == "" {
continue
}
if finalString == "" {
finalString = "# " + strings.TrimLeft(comment, " ")
} else {
finalString = finalString + "\n" + "# " + strings.TrimLeft(comment, " ")
}
}
if finalString == "" {
return []byte{}
}
finalString = finalString + "\n"
return []byte(finalString)
}
// GhodssYAML returns reader that encodes anything to YAML using github.com/ghodss/yaml.
// It works by first marshalling to JSON, so no `yaml` directive will work (it accepts `json` though).
//
// Recommended for:
// * Kubernetes
func GhodssYAML(in ...interface{}) yamlEncoder {
return yaml(ghodssyaml.Marshal, in...)
}
// YAML returns reader that encodes anything to YAML using gopkg.in/yaml.v3.
// NOTE: Indentations are currently "weird": https://github.com/go-yaml/yaml/issues/661
func YAML(in ...interface{}) yamlEncoder {
return yaml(yaml3.Marshal, in...)
}
// YAML2 returns reader that encodes anything to YAML using gopkg.in/yaml.v2.
// NOTE: Indentations are currently "weird": https://github.com/go-yaml/yaml/issues/661
// Recommended for:
// * Prometheus, Alertmanager configuration
func YAML2(in ...interface{}) yamlEncoder {
return yaml(yaml2.Marshal, in...)
}
type MarshalFunc func(o interface{}) ([]byte, error)
func yaml(marshalFn MarshalFunc, in ...interface{}) yamlEncoder {
var concatDelim = []byte("---\n")
if len(in) == 0 {
return yamlEncoder{Reader: errReader{err: errors.New("nothing to output")}}
}
var res [][]byte
for _, entry := range in {
var entryBytes []byte
// Do not marshal strings - they should be appended directly
if extraString, ok := entry.(string); ok {
entryBytes = []byte(extraString)
} else {
b, err := marshalFn(entry)
if err != nil {
return yamlEncoder{Reader: errReader{err: fmt.Errorf("unable to marshal to YAML: %v: %w", in, err)}}
}
entryBytes = b
}
res = append(res, entryBytes)
}
if len(res) == 1 {
return yamlEncoder{
Reader: bytes.NewBuffer(res[0]),
}
}
return yamlEncoder{
Reader: bytes.NewBuffer(bytes.Join(res, concatDelim)),
}
}