Closed ivanduka closed 3 years ago
@ivanduka
Use github.com/goccy/go-yaml/parser
to get *ast.File
and write new value to it .
Full example the following:
https://play.golang.org/p/utYhox4g6FE
package main
import (
"fmt"
"strings"
"github.com/goccy/go-yaml"
"github.com/goccy/go-yaml/parser"
)
func main() {
yml := `
http:
services:
websiteService:
loadBalancer:
servers:
- url: http://localhost:1081
`
urlPath, err := yaml.PathString("$.http.services.websiteService.loadBalancer.servers[0].url")
if err != nil {
panic(err)
}
file, err := parser.ParseBytes([]byte(yml), 0)
if err != nil {
panic(err)
}
if err := urlPath.ReplaceWithReader(file, strings.NewReader(`https://example.com`)); err != nil {
panic(err)
}
fmt.Println(file)
}
Thanks!
Hi, I am trying to change a value in YAML file, but cannot figure out how to do it with YAMLPath.
The original attempt without YAMLPath was:
dynamic.yml:
Now with YAMLPath it looks way more organized when reading values, but I cannot figure out how to actually modify values and save it. Is it even possible?