Created
September 20, 2017 18:36
-
-
Save arehmandev/c8eca212fb56c2a149bff3c0c7ab3afa to your computer and use it in GitHub Desktop.
Changing value in yaml file in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Charts: | |
name: foo | |
repo: foo.com | |
tags: realtag |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Charts: | |
name: "foo" | |
repo: "foo.com" | |
tags: asdfasdas |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was my exact usecase 🙌