@@ -9,9 +9,19 @@ import (
9
9
"github.com/Masterminds/sprig/v3"
10
10
"github.com/pkg/errors"
11
11
"github.com/stretchr/objx"
12
- yaml "gopkg.in/yaml.v2 "
12
+ yaml "gopkg.in/yaml.v3 "
13
13
)
14
14
15
+ func yamlForceStringQuotationRecursivePatch (n * yaml.Node ) {
16
+ if n .Tag == "!!str" {
17
+ n .Style = yaml .DoubleQuotedStyle
18
+ }
19
+ for _ , cNode := range n .Content {
20
+ // for all child nodes
21
+ yamlForceStringQuotationRecursivePatch (cNode )
22
+ }
23
+ }
24
+
15
25
// Manifest represents a Kubernetes API object. The fields `apiVersion` and
16
26
// `kind` are required, `metadata.name` should be present as well
17
27
type Manifest map [string ]interface {}
@@ -31,8 +41,17 @@ func NewFromObj(raw objx.Map) (Manifest, error) {
31
41
}
32
42
33
43
// String returns the Manifest in yaml representation
34
- func (m Manifest ) String () string {
35
- y , err := yaml .Marshal (m )
44
+ func (m Manifest ) String (forceQuotedStrings bool ) string {
45
+
46
+ yamlNode := & yaml.Node {}
47
+ if err := yamlNode .Encode (m ); err != nil {
48
+ panic (errors .Wrap (err , "converting manifest to yaml.Node" ))
49
+ }
50
+ if forceQuotedStrings {
51
+ yamlForceStringQuotationRecursivePatch (yamlNode )
52
+ }
53
+
54
+ y , err := yaml .Marshal (yamlNode )
36
55
if err != nil {
37
56
// this should never go wrong in normal operations
38
57
panic (errors .Wrap (err , "formatting manifest" ))
@@ -262,12 +281,19 @@ type List []Manifest
262
281
263
282
// String returns the List as a yaml stream. In case of an error, it is
264
283
// returned as a string instead.
265
- func (m List ) String () string {
284
+ func (m List ) String (forceQuotedStrings bool ) string {
266
285
buf := bytes.Buffer {}
267
286
enc := yaml .NewEncoder (& buf )
268
287
269
288
for _ , d := range m {
270
- if err := enc .Encode (d ); err != nil {
289
+ yamlNode := & yaml.Node {}
290
+ if err := yamlNode .Encode (d ); err != nil {
291
+ panic (errors .Wrap (err , "converting manifest to yaml.Node" ))
292
+ }
293
+ if forceQuotedStrings {
294
+ yamlForceStringQuotationRecursivePatch (yamlNode )
295
+ }
296
+ if err := enc .Encode (yamlNode ); err != nil {
271
297
// This should never happen in normal operations
272
298
panic (errors .Wrap (err , "formatting manifests" ))
273
299
}
0 commit comments