@@ -12,6 +12,7 @@ import (
12
12
"github.com/spf13/cobra"
13
13
"gopkg.in/yaml.v3"
14
14
15
+ "github.com/pulumi/esc/syntax/encoding"
15
16
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
16
17
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
17
18
)
@@ -107,18 +108,18 @@ func newEnvSetCmd(env *envCommand) *cobra.Command {
107
108
}
108
109
109
110
if path [0 ] == "imports" {
110
- _ , err = yamlNode { & docNode }.set (nil , path , yamlValue )
111
+ _ , err = encoding. YAMLSyntax { Node : & docNode }.Set (nil , path , yamlValue )
111
112
} else {
112
- valuesNode , ok := yamlNode { & docNode }.get (resource.PropertyPath {"values" })
113
+ valuesNode , ok := encoding. YAMLSyntax { Node : & docNode }.Get (resource.PropertyPath {"values" })
113
114
if ! ok {
114
- valuesNode , err = yamlNode { & docNode }.set (nil , resource.PropertyPath {"values" }, yaml.Node {
115
+ valuesNode , err = encoding. YAMLSyntax { Node : & docNode }.Set (nil , resource.PropertyPath {"values" }, yaml.Node {
115
116
Kind : yaml .MappingNode ,
116
117
})
117
118
if err != nil {
118
119
return fmt .Errorf ("internal error: %w" , err )
119
120
}
120
121
}
121
- _ , err = yamlNode { valuesNode }.set (nil , path , yamlValue )
122
+ _ , err = encoding. YAMLSyntax { Node : valuesNode }.Set (nil , path , yamlValue )
122
123
}
123
124
if err != nil {
124
125
return err
@@ -187,154 +188,3 @@ func looksLikeSecret(path resource.PropertyPath, n yaml.Node) bool {
187
188
return info .Entropy >= entropyThreshold ||
188
189
(info .Entropy >= (entropyThreshold / 2 ) && entropyPerChar >= entropyPerCharThreshold )
189
190
}
190
-
191
- type yamlNode struct {
192
- * yaml.Node
193
- }
194
-
195
- func (n yamlNode ) get (path resource.PropertyPath ) (_ * yaml.Node , ok bool ) {
196
- if n .Kind == yaml .DocumentNode {
197
- return yamlNode {n .Content [0 ]}.get (path )
198
- }
199
-
200
- if len (path ) == 0 {
201
- return n .Node , true
202
- }
203
-
204
- switch n .Kind {
205
- case yaml .SequenceNode :
206
- index , ok := path [0 ].(int )
207
- if ! ok || index < 0 || index >= len (n .Content ) {
208
- return nil , false
209
- }
210
- return yamlNode {n .Content [index ]}.get (path [1 :])
211
- case yaml .MappingNode :
212
- key , ok := path [0 ].(string )
213
- if ! ok {
214
- return nil , false
215
- }
216
- for i := 0 ; i < len (n .Content ); i += 2 {
217
- keyNode , valueNode := n .Content [i ], n .Content [i + 1 ]
218
- if keyNode .Value == key {
219
- return yamlNode {valueNode }.get (path [1 :])
220
- }
221
- }
222
- return nil , false
223
- default :
224
- return nil , false
225
- }
226
- }
227
-
228
- func (n yamlNode ) set (prefix , path resource.PropertyPath , new yaml.Node ) (* yaml.Node , error ) {
229
- if n .Kind == yaml .DocumentNode {
230
- return yamlNode {n .Content [0 ]}.set (prefix , path , new )
231
- }
232
-
233
- if len (path ) == 0 {
234
- n .Content = new .Content
235
- n .Kind = new .Kind
236
- n .Tag = new .Tag
237
- n .Value = new .Value
238
- return n .Node , nil
239
- }
240
-
241
- prefix = append (prefix , path [0 ])
242
- switch n .Kind {
243
- case 0 :
244
- switch accessor := path [0 ].(type ) {
245
- case int :
246
- n .Kind , n .Tag = yaml .SequenceNode , "!!seq"
247
- case string :
248
- n .Kind , n .Tag = yaml .MappingNode , "!!map"
249
- default :
250
- contract .Failf ("unexpected accessor kind %T" , accessor )
251
- return nil , nil
252
- }
253
- return n .set (prefix [:len (prefix )- 1 ], path , new )
254
- case yaml .SequenceNode :
255
- index , ok := path [0 ].(int )
256
- if ! ok {
257
- return nil , fmt .Errorf ("%v: key for an array must be an int" , prefix )
258
- }
259
- if index < 0 || index > len (n .Content ) {
260
- return nil , fmt .Errorf ("%v: array index out of range" , prefix )
261
- }
262
- if index == len (n .Content ) {
263
- n .Content = append (n .Content , & yaml.Node {})
264
- }
265
- elem := n .Content [index ]
266
- return yamlNode {elem }.set (prefix , path [1 :], new )
267
- case yaml .MappingNode :
268
- key , ok := path [0 ].(string )
269
- if ! ok {
270
- return nil , fmt .Errorf ("%v: key for a map must be a string" , prefix )
271
- }
272
-
273
- var valueNode * yaml.Node
274
- for i := 0 ; i < len (n .Content ); i += 2 {
275
- keyNode , value := n .Content [i ], n .Content [i + 1 ]
276
- if keyNode .Value == key {
277
- valueNode = value
278
- break
279
- }
280
- }
281
- if valueNode == nil {
282
- n .Content = append (n .Content , & yaml.Node {
283
- Kind : yaml .ScalarNode ,
284
- Value : key ,
285
- Tag : "!!str" ,
286
- })
287
- n .Content = append (n .Content , & yaml.Node {})
288
- valueNode = n .Content [len (n .Content )- 1 ]
289
- }
290
- return yamlNode {valueNode }.set (prefix , path [1 :], new )
291
- default :
292
- return nil , fmt .Errorf ("%v: expected an array or an object" , prefix )
293
- }
294
- }
295
-
296
- func (n yamlNode ) delete (prefix , path resource.PropertyPath ) error {
297
- if n .Kind == yaml .DocumentNode {
298
- return yamlNode {n .Content [0 ]}.delete (prefix , path )
299
- }
300
-
301
- prefix = append (prefix , path [0 ])
302
- switch n .Kind {
303
- case yaml .SequenceNode :
304
- index , ok := path [0 ].(int )
305
- if ! ok {
306
- return fmt .Errorf ("%v: key for an array must be an int" , prefix )
307
- }
308
- if index < 0 || index >= len (n .Content ) {
309
- return fmt .Errorf ("%v: array index out of range" , prefix )
310
- }
311
- if len (path ) == 1 {
312
- n .Content = append (n .Content [:index ], n .Content [index + 1 :]... )
313
- return nil
314
- }
315
- elem := n .Content [index ]
316
- return yamlNode {elem }.delete (prefix , path [1 :])
317
- case yaml .MappingNode :
318
- key , ok := path [0 ].(string )
319
- if ! ok {
320
- return fmt .Errorf ("%v: key for a map must be a string" , prefix )
321
- }
322
-
323
- i := 0
324
- for ; i < len (n .Content ); i += 2 {
325
- if n .Content [i ].Value == key {
326
- break
327
- }
328
- }
329
- if len (path ) == 1 {
330
- if i != len (n .Content ) {
331
- n .Content = append (n .Content [:i ], n .Content [i + 2 :]... )
332
- }
333
- return nil
334
- }
335
- valueNode := n .Content [i + 1 ]
336
- return yamlNode {valueNode }.delete (prefix , path [1 :])
337
- default :
338
- return fmt .Errorf ("%v: expected an array or an object" , prefix )
339
- }
340
- }
0 commit comments