Fix patch YAML without new line on end of file
This commit is contained in:
@@ -109,6 +109,13 @@ func AddOrReplace(src []byte, key string, value any, nodeParent *yaml.Node) ([]b
|
||||
i0 := LineOffset(src, nodeKey.Line)
|
||||
i1 := LineOffset(src, LastChild(nodeValue).Line+1)
|
||||
|
||||
if i1 < 0 { // no new line on the end of file
|
||||
if value != nil {
|
||||
return append(src[:i0], put...), nil
|
||||
}
|
||||
return src[:i0], nil
|
||||
}
|
||||
|
||||
dst := make([]byte, 0, len(src)+len(put))
|
||||
dst = append(dst, src[:i0]...)
|
||||
if value != nil {
|
||||
@@ -121,6 +128,14 @@ func AddOrReplace(src []byte, key string, value any, nodeParent *yaml.Node) ([]b
|
||||
|
||||
i := LineOffset(src, LastChild(nodeParent).Line+1)
|
||||
|
||||
if i < 0 { // no new line on the end of file
|
||||
src = append(src, '\n')
|
||||
if value != nil {
|
||||
src = append(src, put...)
|
||||
}
|
||||
return src, nil
|
||||
}
|
||||
|
||||
dst := make([]byte, 0, len(src)+len(put))
|
||||
dst = append(dst, src[:i]...)
|
||||
if value != nil {
|
||||
|
||||
Reference in New Issue
Block a user