@@ -24,13 +24,13 @@ func CalculateOverlay(originalYAML, targetYAML, existingOverlay string) (string,
24
24
25
25
// we go from the original to a new version, then look at the extra overlays on top
26
26
// of that, then add that to the existing overlay
27
- var overlayDocument overlay.Overlay
28
- err = yaml .Unmarshal ([]byte (existingOverlay ), & overlayDocument )
27
+ var existingOverlayDocument overlay.Overlay
28
+ err = yaml .Unmarshal ([]byte (existingOverlay ), & existingOverlayDocument )
29
29
if err != nil {
30
30
return "" , fmt .Errorf ("failed to parse overlay schema: %w" , err )
31
31
}
32
32
// now modify the original using the existing overlay
33
- err = overlayDocument .ApplyTo (& orig )
33
+ err = existingOverlayDocument .ApplyTo (& orig )
34
34
if err != nil {
35
35
return "" , fmt .Errorf ("failed to apply existing overlay: %w" , err )
36
36
}
@@ -39,10 +39,15 @@ func CalculateOverlay(originalYAML, targetYAML, existingOverlay string) (string,
39
39
if err != nil {
40
40
return "" , fmt .Errorf ("failed to compare schemas: %w" , err )
41
41
}
42
- // Now we take those actions, add them onto the end of the existing overlay
43
- overlayDocument .Actions = append (overlayDocument .Actions , newOverlay .Actions ... )
42
+ // special case, is there only one action and it targets the same as the last overlayDocument.Actions item entry, we'll just replace it.
43
+ if len (newOverlay .Actions ) == 1 && len (existingOverlayDocument .Actions ) > 0 && newOverlay .Actions [0 ].Target == existingOverlayDocument .Actions [len (existingOverlayDocument .Actions )- 1 ].Target {
44
+ existingOverlayDocument .Actions [len (existingOverlayDocument .Actions )- 1 ] = newOverlay .Actions [0 ]
45
+ } else {
46
+ // Otherwise, we'll just append the new overlay to the existing overlay
47
+ existingOverlayDocument .Actions = append (existingOverlayDocument .Actions , newOverlay .Actions ... )
48
+ }
44
49
45
- out , err := yaml .Marshal (overlayDocument )
50
+ out , err := yaml .Marshal (existingOverlayDocument )
46
51
if err != nil {
47
52
return "" , fmt .Errorf ("failed to marshal schema: %w" , err )
48
53
}
0 commit comments