Go library to deal with YAML documents embedded within k8s manifests (like Spring boot's application.yaml).
example.yaml
---
kind: ConfigMap
metadata:
name: cm1
namespace: default
apiVersion: v1
data:
application.yaml: |
xyz: 456
abc:
def:
leaf1: 123
leaf2: Hello
code
package main
import (
ydom "github.com/rkosegi/yaml-toolkit/dom"
yk8s "github.com/rkosegi/yaml-toolkit/k8s"
)
func main() {
d, err := yk8s.YamlDoc("example.yaml", "application.yaml")
if err != nil {
panic(err)
}
print (d.Document().Child("xyz").(ydom.Leaf).Value()) // 456
d.Document().AddValue("another-key", ydom.LeafNode("789")) // add new child node
err = d.Save()
if err != nil {
panic(err)
}
}
Left | Right |
---|---|
left.yaml | right.yaml |
---
root:
sub1:
leaf1: abc
leaf2: 123
list:
- 1
- 2 |
---
root:
sub1:
leaf1: def
leaf3: 789
list:
- 3 |
package main
import (
"fmt"
yta "github.com/rkosegi/yaml-toolkit/analytics"
ydiff "github.com/rkosegi/yaml-toolkit/diff"
)
func main() {
dp := yta.DefaultFileDecoderProvider(".yaml")
ds := yta.NewDocumentSet()
err := ds.AddDocumentFromFile("left.yaml", dp, yta.WithTags("left"))
if err != nil {
panic(err)
}
err = ds.AddDocumentFromFile("right.yaml", dp, yta.WithTags("right"))
if err != nil {
panic(err)
}
changes := ydiff.Diff(
ds.TaggedSubset("right").Merged(),
ds.TaggedSubset("left").Merged(),
)
fmt.Printf("All changes: %d\n", len(*changes))
for _, change := range *changes {
fmt.Printf("%s: %s => %v\n", change.Type, change.Path, change.Value)
}
}
output:
All changes: 5
Delete: root.list => <nil>
Add: root.list[0] => 3
Change: root.sub1.leaf1 => abc
Delete: root.sub1.leaf2 => <nil>
Add: root.sub1.leaf3 => 789