File tree 4 files changed +22
-1
lines changed
4 files changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -45,7 +45,7 @@ require (
45
45
k8s.io/klog/v2 v2.100.1
46
46
k8s.io/kubectl v0.28.2
47
47
oras.land/oras-go v1.2.4
48
- sigs.k8s.io/yaml v1.3 .0
48
+ sigs.k8s.io/yaml v1.4 .0
49
49
)
50
50
51
51
require (
Original file line number Diff line number Diff line change @@ -555,3 +555,5 @@ sigs.k8s.io/structured-merge-diff/v4 v4.2.3 h1:PRbqxJClWWYMNV1dhaG4NsibJbArud9kF
555
555
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 /go.mod h1:qjx8mGObPmV2aSZepjQjbmb2ihdVs8cGKBraizNC69E =
556
556
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo =
557
557
sigs.k8s.io/yaml v1.3.0 /go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8 =
558
+ sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E =
559
+ sigs.k8s.io/yaml v1.4.0 /go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY =
Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ import (
25
25
"github.com/BurntSushi/toml"
26
26
"github.com/Masterminds/sprig/v3"
27
27
"sigs.k8s.io/yaml"
28
+ goYaml "sigs.k8s.io/yaml/goyaml.v3"
28
29
)
29
30
30
31
// funcMap returns a mapping of all of the functions that Engine has.
@@ -49,6 +50,7 @@ func funcMap() template.FuncMap {
49
50
extra := template.FuncMap {
50
51
"toToml" : toTOML ,
51
52
"toYaml" : toYAML ,
53
+ "toYamlPretty" : toYAMLPretty ,
52
54
"fromYaml" : fromYAML ,
53
55
"fromYamlArray" : fromYAMLArray ,
54
56
"toJson" : toJSON ,
@@ -88,6 +90,19 @@ func toYAML(v interface{}) string {
88
90
return strings .TrimSuffix (string (data ), "\n " )
89
91
}
90
92
93
+ func toYAMLPretty (v interface {}) string {
94
+ var data bytes.Buffer
95
+ encoder := goYaml .NewEncoder (& data )
96
+ encoder .SetIndent (2 )
97
+ err := encoder .Encode (v )
98
+
99
+ if err != nil {
100
+ // Swallow errors inside of a template.
101
+ return ""
102
+ }
103
+ return strings .TrimSuffix (data .String (), "\n " )
104
+ }
105
+
91
106
// fromYAML converts a YAML document into a map[string]interface{}.
92
107
//
93
108
// This is not a general-purpose YAML parser, and will not parse all valid
Original file line number Diff line number Diff line change @@ -33,6 +33,10 @@ func TestFuncs(t *testing.T) {
33
33
tpl : `{{ toYaml . }}` ,
34
34
expect : `foo: bar` ,
35
35
vars : map [string ]interface {}{"foo" : "bar" },
36
+ }, {
37
+ tpl : `{{ toYamlPretty . }}` ,
38
+ expect : "baz:\n - 1\n - 2\n - 3" ,
39
+ vars : map [string ]interface {}{"baz" : []int {1 , 2 , 3 }},
36
40
}, {
37
41
tpl : `{{ toToml . }}` ,
38
42
expect : "foo = \" bar\" \n " ,
You can’t perform that action at this time.
0 commit comments