@@ -35,7 +35,7 @@ func UpdateYamlValue(yamlBytes []byte, path []string, value string) ([]byte, err
35
35
}
36
36
37
37
// Convert the updated YAML node back to YAML bytes.
38
- updatedYAMLBytes , err := yamlMarshal (body )
38
+ updatedYAMLBytes , err := YamlMarshal (body )
39
39
if err != nil {
40
40
return nil , fmt .Errorf ("failed to convert YAML node to bytes: %w" , err )
41
41
}
@@ -100,39 +100,25 @@ func lookupKey(node *yaml.Node, key string) (*yaml.Node, *yaml.Node) {
100
100
return nil , nil
101
101
}
102
102
103
- // Walks a yaml document to the specified path, and then applies the transformation to that node.
103
+ // Walks a yaml document from the root node to the specified path, and then applies the transformation to that node.
104
104
//
105
105
// The transform must return true if it made changes to the node.
106
106
// If the requested path is not defined in the document, no changes are made to the document.
107
107
//
108
- // If no changes are made, the original document is returned.
109
- // If changes are made, a newly marshalled document is returned. (This may result in different indentation for all nodes)
110
- func TransformNode (yamlBytes []byte , path []string , transform func (node * yaml.Node ) (bool , error )) ([]byte , error ) {
111
- // Parse the YAML file.
112
- var node yaml.Node
113
- err := yaml .Unmarshal (yamlBytes , & node )
114
- if err != nil {
115
- return nil , fmt .Errorf ("failed to parse YAML: %w" , err )
116
- }
117
-
108
+ // Returns true if the transform function made changes to any node
109
+ func TransformNode (rootNode * yaml.Node , path []string , transform func (node * yaml.Node ) (bool , error )) (bool , error ) {
118
110
// Empty document: nothing to do.
119
- if len (node .Content ) == 0 {
120
- return yamlBytes , nil
111
+ if len (rootNode .Content ) == 0 {
112
+ return false , nil
121
113
}
122
114
123
- body := node .Content [0 ]
115
+ body := rootNode .Content [0 ]
124
116
125
117
if didTransform , err := transformNode (body , path , transform ); err != nil || ! didTransform {
126
- return yamlBytes , err
118
+ return false , err
127
119
}
128
120
129
- // Convert the updated YAML node back to YAML bytes.
130
- updatedYAMLBytes , err := yamlMarshal (body )
131
- if err != nil {
132
- return nil , fmt .Errorf ("failed to convert YAML node to bytes: %w" , err )
133
- }
134
-
135
- return updatedYAMLBytes , nil
121
+ return true , nil
136
122
}
137
123
138
124
// A recursive function to walk down the tree. See TransformNode for more details.
@@ -149,34 +135,22 @@ func transformNode(node *yaml.Node, path []string, transform func(node *yaml.Nod
149
135
return transformNode (valueNode , path [1 :], transform )
150
136
}
151
137
152
- // takes a yaml document in bytes , a path to a key, and a new name for the key.
138
+ // Takes the root node of a yaml document, a path to a key, and a new name for the key.
153
139
// Will rename the key to the new name if it exists, and do nothing otherwise.
154
- func RenameYamlKey (yamlBytes []byte , path []string , newKey string ) ([]byte , error ) {
155
- // Parse the YAML file.
156
- var node yaml.Node
157
- err := yaml .Unmarshal (yamlBytes , & node )
158
- if err != nil {
159
- return nil , fmt .Errorf ("failed to parse YAML: %w for bytes %s" , err , string (yamlBytes ))
160
- }
161
-
140
+ // Returns true if it found the old path and renamed the key
141
+ func RenameYamlKey (rootNode * yaml.Node , path []string , newKey string ) (bool , error ) {
162
142
// Empty document: nothing to do.
163
- if len (node .Content ) == 0 {
164
- return yamlBytes , nil
143
+ if len (rootNode .Content ) == 0 {
144
+ return false , nil
165
145
}
166
146
167
- body := node .Content [0 ]
147
+ body := rootNode .Content [0 ]
168
148
169
149
if didRename , err := renameYamlKey (body , path , newKey ); err != nil || ! didRename {
170
- return yamlBytes , err
171
- }
172
-
173
- // Convert the updated YAML node back to YAML bytes.
174
- updatedYAMLBytes , err := yamlMarshal (body )
175
- if err != nil {
176
- return nil , fmt .Errorf ("failed to convert YAML node to bytes: %w" , err )
150
+ return false , err
177
151
}
178
152
179
- return updatedYAMLBytes , nil
153
+ return true , nil
180
154
}
181
155
182
156
// Recursive function to rename the YAML key.
@@ -206,34 +180,21 @@ func renameYamlKey(node *yaml.Node, path []string, newKey string) (bool, error)
206
180
207
181
// Traverses a yaml document, calling the callback function for each node. The
208
182
// callback is allowed to modify the node in place, in which case it should
209
- // return true. The function returns the original yaml document if none of the
210
- // callbacks returned true, and the modified document otherwise.
211
- func Walk (yamlBytes []byte , callback func (node * yaml.Node , path string ) bool ) ([]byte , error ) {
212
- // Parse the YAML file.
213
- var node yaml.Node
214
- err := yaml .Unmarshal (yamlBytes , & node )
215
- if err != nil {
216
- return nil , fmt .Errorf ("failed to parse YAML: %w" , err )
217
- }
218
-
183
+ // return true. The function returns true if the callback modified the document at all,
184
+ // and false otherwise.
185
+ func Walk (rootNode * yaml.Node , callback func (node * yaml.Node , path string ) bool ) (bool , error ) {
219
186
// Empty document: nothing to do.
220
- if len (node .Content ) == 0 {
221
- return yamlBytes , nil
187
+ if len (rootNode .Content ) == 0 {
188
+ return false , nil
222
189
}
223
190
224
- body := node .Content [0 ]
191
+ body := rootNode .Content [0 ]
225
192
226
193
if didChange , err := walk (body , "" , callback ); err != nil || ! didChange {
227
- return yamlBytes , err
194
+ return false , err
228
195
}
229
196
230
- // Convert the updated YAML node back to YAML bytes.
231
- updatedYAMLBytes , err := yamlMarshal (body )
232
- if err != nil {
233
- return nil , fmt .Errorf ("failed to convert YAML node to bytes: %w" , err )
234
- }
235
-
236
- return updatedYAMLBytes , nil
197
+ return true , nil
237
198
}
238
199
239
200
func walk (node * yaml.Node , path string , callback func (* yaml.Node , string ) bool ) (bool , error ) {
@@ -275,7 +236,7 @@ func walk(node *yaml.Node, path string, callback func(*yaml.Node, string) bool)
275
236
return didChange , nil
276
237
}
277
238
278
- func yamlMarshal (node * yaml.Node ) ([]byte , error ) {
239
+ func YamlMarshal (node * yaml.Node ) ([]byte , error ) {
279
240
var buffer bytes.Buffer
280
241
encoder := yaml .NewEncoder (& buffer )
281
242
encoder .SetIndent (2 )
0 commit comments