Skip to content

Instantly share code, notes, and snippets.

@arehmandev
Created September 20, 2017 18:36
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arehmandev/c8eca212fb56c2a149bff3c0c7ab3afa to your computer and use it in GitHub Desktop.
Save arehmandev/c8eca212fb56c2a149bff3c0c7ab3afa to your computer and use it in GitHub Desktop.
Changing value in yaml file in go
Charts:
name: foo
repo: foo.com
tags: realtag
Charts:
name: "foo"
repo: "foo.com"
tags: asdfasdas
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"github.com/spf13/viper"
yaml "gopkg.in/yaml.v2"
)
type repo struct {
Charts struct {
Name string `yaml:"name"`
Repo string `yaml:"repo"`
Tags string `yaml:"tags"`
} `yaml:"Charts"`
}
func main() {
viper.SetConfigName("config")
viper.AddConfigPath(currentdir())
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
log.Fatal(err)
}
C := repo{}
err = viper.Unmarshal(&C)
if err != nil {
log.Fatalf("unable to decode into struct, %v", err)
}
fmt.Println(C)
// Change value in map and marshal back into yaml
C.Charts.Tags = "realtag"
fmt.Println(C)
d, err := yaml.Marshal(&C)
if err != nil {
log.Fatalf("error: %v", err)
}
fmt.Println(string(d))
// write to file
f, err := os.Create("/tmp/dat2")
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile("changed.yaml", d, 0644)
if err != nil {
log.Fatal(err)
}
f.Close()
}
func currentdir() (cwd string) {
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
return cwd
}
@jayd3e
Copy link

jayd3e commented Apr 15, 2024

This was my exact usecase 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment