@@ -106,118 +106,6 @@ func findPattern(s, pattern string) int {
106
106
return bytes .Index ([]byte (s ), []byte (pattern ))
107
107
}
108
108
109
- func copyEntryPoints (sourceFile , destFile string ) error {
110
- // Read source file
111
- sourceData , err := os .ReadFile (sourceFile )
112
- if err != nil {
113
- return fmt .Errorf ("error reading source file: %w" , err )
114
- }
115
-
116
- // Read destination file
117
- destData , err := os .ReadFile (destFile )
118
- if err != nil {
119
- return fmt .Errorf ("error reading destination file: %w" , err )
120
- }
121
-
122
- // Parse source YAML
123
- var sourceYAML map [string ]interface {}
124
- if err := yaml .Unmarshal (sourceData , & sourceYAML ); err != nil {
125
- return fmt .Errorf ("error parsing source YAML: %w" , err )
126
- }
127
-
128
- // Parse destination YAML
129
- var destYAML map [string ]interface {}
130
- if err := yaml .Unmarshal (destData , & destYAML ); err != nil {
131
- return fmt .Errorf ("error parsing destination YAML: %w" , err )
132
- }
133
-
134
- // Get entryPoints section from source
135
- entryPoints , ok := sourceYAML ["entryPoints" ]
136
- if ! ok {
137
- return fmt .Errorf ("entryPoints section not found in source file" )
138
- }
139
-
140
- // Update entryPoints in destination
141
- destYAML ["entryPoints" ] = entryPoints
142
-
143
- // Marshal updated destination YAML
144
- // updatedData, err := yaml.Marshal(destYAML)
145
- updatedData , err := MarshalYAMLWithIndent (destYAML , 2 )
146
- if err != nil {
147
- return fmt .Errorf ("error marshaling updated YAML: %w" , err )
148
- }
149
-
150
- // Write updated YAML back to destination file
151
- if err := os .WriteFile (destFile , updatedData , 0644 ); err != nil {
152
- return fmt .Errorf ("error writing to destination file: %w" , err )
153
- }
154
-
155
- return nil
156
- }
157
-
158
- func copyWebsecureEntryPoint (sourceFile , destFile string ) error {
159
- // Read source file
160
- sourceData , err := os .ReadFile (sourceFile )
161
- if err != nil {
162
- return fmt .Errorf ("error reading source file: %w" , err )
163
- }
164
-
165
- // Read destination file
166
- destData , err := os .ReadFile (destFile )
167
- if err != nil {
168
- return fmt .Errorf ("error reading destination file: %w" , err )
169
- }
170
-
171
- // Parse source YAML
172
- var sourceYAML map [string ]interface {}
173
- if err := yaml .Unmarshal (sourceData , & sourceYAML ); err != nil {
174
- return fmt .Errorf ("error parsing source YAML: %w" , err )
175
- }
176
-
177
- // Parse destination YAML
178
- var destYAML map [string ]interface {}
179
- if err := yaml .Unmarshal (destData , & destYAML ); err != nil {
180
- return fmt .Errorf ("error parsing destination YAML: %w" , err )
181
- }
182
-
183
- // Get entryPoints section from source
184
- entryPoints , ok := sourceYAML ["entryPoints" ].(map [string ]interface {})
185
- if ! ok {
186
- return fmt .Errorf ("entryPoints section not found in source file or has invalid format" )
187
- }
188
-
189
- // Get websecure configuration
190
- websecure , ok := entryPoints ["websecure" ]
191
- if ! ok {
192
- return fmt .Errorf ("websecure entrypoint not found in source file" )
193
- }
194
-
195
- // Get or create entryPoints section in destination
196
- destEntryPoints , ok := destYAML ["entryPoints" ].(map [string ]interface {})
197
- if ! ok {
198
- // If entryPoints section doesn't exist, create it
199
- destEntryPoints = make (map [string ]interface {})
200
- destYAML ["entryPoints" ] = destEntryPoints
201
- }
202
-
203
- // Update websecure in destination
204
- destEntryPoints ["websecure" ] = websecure
205
-
206
- // Marshal updated destination YAML
207
- // updatedData, err := yaml.Marshal(destYAML)
208
- updatedData , err := MarshalYAMLWithIndent (destYAML , 2 )
209
- if err != nil {
210
- return fmt .Errorf ("error marshaling updated YAML: %w" , err )
211
- }
212
-
213
- // Write updated YAML back to destination file
214
- if err := os .WriteFile (destFile , updatedData , 0644 ); err != nil {
215
- return fmt .Errorf ("error writing to destination file: %w" , err )
216
- }
217
-
218
- return nil
219
- }
220
-
221
109
func copyDockerService (sourceFile , destFile , serviceName string ) error {
222
110
// Read source file
223
111
sourceData , err := os .ReadFile (sourceFile )
@@ -391,3 +279,75 @@ func CheckAndAddTraefikLogVolume(composePath string) error {
391
279
fmt .Println ("Added traefik log volume and created logs directory" )
392
280
return nil
393
281
}
282
+
283
+ // MergeYAML merges two YAML files, where the contents of the second file
284
+ // are merged into the first file. In case of conflicts, values from the
285
+ // second file take precedence.
286
+ func MergeYAML (baseFile , overlayFile string ) error {
287
+ // Read the base YAML file
288
+ baseContent , err := os .ReadFile (baseFile )
289
+ if err != nil {
290
+ return fmt .Errorf ("error reading base file: %v" , err )
291
+ }
292
+
293
+ // Read the overlay YAML file
294
+ overlayContent , err := os .ReadFile (overlayFile )
295
+ if err != nil {
296
+ return fmt .Errorf ("error reading overlay file: %v" , err )
297
+ }
298
+
299
+ // Parse base YAML into a map
300
+ var baseMap map [string ]interface {}
301
+ if err := yaml .Unmarshal (baseContent , & baseMap ); err != nil {
302
+ return fmt .Errorf ("error parsing base YAML: %v" , err )
303
+ }
304
+
305
+ // Parse overlay YAML into a map
306
+ var overlayMap map [string ]interface {}
307
+ if err := yaml .Unmarshal (overlayContent , & overlayMap ); err != nil {
308
+ return fmt .Errorf ("error parsing overlay YAML: %v" , err )
309
+ }
310
+
311
+ // Merge the overlay into the base
312
+ merged := mergeMap (baseMap , overlayMap )
313
+
314
+ // Marshal the merged result back to YAML
315
+ mergedContent , err := MarshalYAMLWithIndent (merged , 2 )
316
+ if err != nil {
317
+ return fmt .Errorf ("error marshaling merged YAML: %v" , err )
318
+ }
319
+
320
+ // Write the merged content back to the base file
321
+ if err := os .WriteFile (baseFile , mergedContent , 0644 ); err != nil {
322
+ return fmt .Errorf ("error writing merged YAML: %v" , err )
323
+ }
324
+
325
+ return nil
326
+ }
327
+
328
+ // mergeMap recursively merges two maps
329
+ func mergeMap (base , overlay map [string ]interface {}) map [string ]interface {} {
330
+ result := make (map [string ]interface {})
331
+
332
+ // Copy all key-values from base map
333
+ for k , v := range base {
334
+ result [k ] = v
335
+ }
336
+
337
+ // Merge overlay values
338
+ for k , v := range overlay {
339
+ // If both maps have the same key and both values are maps, merge recursively
340
+ if baseVal , ok := base [k ]; ok {
341
+ if baseMap , isBaseMap := baseVal .(map [string ]interface {}); isBaseMap {
342
+ if overlayMap , isOverlayMap := v .(map [string ]interface {}); isOverlayMap {
343
+ result [k ] = mergeMap (baseMap , overlayMap )
344
+ continue
345
+ }
346
+ }
347
+ }
348
+ // Otherwise, overlay value takes precedence
349
+ result [k ] = v
350
+ }
351
+
352
+ return result
353
+ }
0 commit comments