6
6
"errors"
7
7
"fmt"
8
8
"io"
9
- "os"
10
9
"strings"
11
10
12
11
"github.com/google/yamlfmt"
@@ -37,7 +36,7 @@ func ValidateContent(content []byte) error {
37
36
return err
38
37
}
39
38
40
- // EvaluateExpression evaluates the yq expression, and returns the modified yaml.
39
+ // EvaluateExpression evaluates the yq expression and returns the modified yaml.
41
40
func EvaluateExpression (expression string , content []byte ) ([]byte , error ) {
42
41
if expression == "" {
43
42
return content , nil
@@ -56,21 +55,6 @@ func EvaluateExpression(expression string, content []byte) ([]byte, error) {
56
55
if err != nil {
57
56
return nil , err
58
57
}
59
- tmpYAMLFile , err := os .CreateTemp ("" , "lima-yq-*.yaml" )
60
- if err != nil {
61
- return nil , err
62
- }
63
- tmpYAMLPath := tmpYAMLFile .Name ()
64
- defer os .RemoveAll (tmpYAMLPath )
65
- _ , err = tmpYAMLFile .Write (contentModified )
66
- if err != nil {
67
- tmpYAMLFile .Close ()
68
- return nil , err
69
- }
70
- if err = tmpYAMLFile .Close (); err != nil {
71
- return nil , err
72
- }
73
-
74
58
memory := logging .NewMemoryBackend (0 )
75
59
backend := logging .AddModuleLevel (memory )
76
60
logging .SetBackend (backend )
@@ -80,13 +64,8 @@ func EvaluateExpression(expression string, content []byte) ([]byte, error) {
80
64
encoderPrefs .Indent = 2
81
65
encoderPrefs .ColorsEnabled = false
82
66
encoder := yqlib .NewYamlEncoder (encoderPrefs )
83
- out := new (bytes.Buffer )
84
- printer := yqlib .NewPrinter (encoder , yqlib .NewSinglePrinterWriter (out ))
85
67
decoder := yqlib .NewYamlDecoder (yqlib .ConfiguredYamlPreferences )
86
-
87
- streamEvaluator := yqlib .NewStreamEvaluator ()
88
- files := []string {tmpYAMLPath }
89
- err = streamEvaluator .EvaluateFiles (expression , files , printer , decoder )
68
+ out , err := yqlib .NewStringEvaluator ().EvaluateAll (expression , string (contentModified ), encoder , decoder )
90
69
if err != nil {
91
70
logger := logrus .StandardLogger ()
92
71
for node := memory .Head (); node != nil ; node = node .Next () {
@@ -110,8 +89,7 @@ func EvaluateExpression(expression string, content []byte) ([]byte, error) {
110
89
}
111
90
return nil , err
112
91
}
113
-
114
- return formatter .Format (out .Bytes ())
92
+ return formatter .Format ([]byte (out ))
115
93
}
116
94
117
95
func Join (yqExprs []string ) string {
0 commit comments