From 48e0b61d8cc0a63aecff14f774ff3faf696df021 Mon Sep 17 00:00:00 2001 From: g3rk6 Date: Tue, 8 Sep 2015 03:25:36 -0400 Subject: [PATCH 1/4] Added method to write into TOML file. --- viper.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/viper.go b/viper.go index 389a58e..270abcc 100644 --- a/viper.go +++ b/viper.go @@ -20,6 +20,7 @@ package viper import ( + "bufio" "bytes" "fmt" "io" @@ -31,6 +32,7 @@ import ( "strings" "time" + "github.com/BurntSushi/toml" "github.com/kr/pretty" "github.com/mitchellh/mapstructure" "github.com/spf13/cast" @@ -874,6 +876,32 @@ func (v *Viper) InConfig(key string) bool { return exists } +// Save configuration to file +func SaveConfig() error { return v.SaveConfig() } +func (v *Viper) SaveConfig() error { + + jww.INFO.Println("Attempting to write config into the file") + if !stringInSlice(v.getConfigType(), SupportedExts) { + return UnsupportedConfigError(v.getConfigType()) + } + + f, err := os.Create(v.getConfigFile()) + defer f.Close() + + if err != nil { + return err + } + + w := bufio.NewWriter(f) + + if err := toml.NewEncoder(w).Encode(v.AllSettings()); err != nil { + jww.FATAL.Println("Panic while writing into the file") + } + w.Flush() + + return nil +} + // Set the default value for this key. // Default only used when no value is provided by the user via flag, config or ENV. func SetDefault(key string, value interface{}) { v.SetDefault(key, value) } From d5c009456e5a320255bbfb753b12714be252a3b4 Mon Sep 17 00:00:00 2001 From: g3rk6 Date: Sun, 20 Sep 2015 02:24:06 -0400 Subject: [PATCH 2/4] Added functionality to export configuration based on config type. The feature supports JSON and TOML. --- viper.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/viper.go b/viper.go index 270abcc..7c542af 100644 --- a/viper.go +++ b/viper.go @@ -22,6 +22,7 @@ package viper import ( "bufio" "bytes" + "encoding/json" "fmt" "io" "io/ioutil" @@ -880,7 +881,7 @@ func (v *Viper) InConfig(key string) bool { func SaveConfig() error { return v.SaveConfig() } func (v *Viper) SaveConfig() error { - jww.INFO.Println("Attempting to write config into the file") + jww.INFO.Println("Attempting to write config into the file.") if !stringInSlice(v.getConfigType(), SupportedExts) { return UnsupportedConfigError(v.getConfigType()) } @@ -892,12 +893,23 @@ func (v *Viper) SaveConfig() error { return err } - w := bufio.NewWriter(f) + switch v.getConfigType() { + case "json": - if err := toml.NewEncoder(w).Encode(v.AllSettings()); err != nil { - jww.FATAL.Println("Panic while writing into the file") + b, err := json.MarshalIndent(v.AllSettings(), "", " ") + if err != nil { + jww.FATAL.Println("Panic while encoding into JSON format.") + } + f.WriteString(string(b)) + + case "toml": + + w := bufio.NewWriter(f) + if err := toml.NewEncoder(w).Encode(v.AllSettings()); err != nil { + jww.FATAL.Println("Panic while encoding into TOML format.") + } + w.Flush() } - w.Flush() return nil } @@ -930,6 +942,8 @@ func (v *Viper) ReadInConfig() error { return UnsupportedConfigError(v.getConfigType()) } + jww.DEBUG.Println("Reading file: ", v.getConfigFile()) + file, err := ioutil.ReadFile(v.getConfigFile()) if err != nil { return err From 11ae2b93920f8a790b92bc1fc39d64e827290f6a Mon Sep 17 00:00:00 2001 From: g3rk6 Date: Sun, 10 Jan 2016 21:59:10 -0500 Subject: [PATCH 3/4] Added method to write into YAML file. --- viper.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/viper.go b/viper.go index 7c542af..9a2ace3 100644 --- a/viper.go +++ b/viper.go @@ -40,6 +40,7 @@ import ( jww "github.com/spf13/jwalterweatherman" "github.com/spf13/pflag" "gopkg.in/fsnotify.v1" + "gopkg.in/yaml.v2" ) var v *Viper @@ -909,6 +910,14 @@ func (v *Viper) SaveConfig() error { jww.FATAL.Println("Panic while encoding into TOML format.") } w.Flush() + + case "yaml", "yml": + + b, err := yaml.Marshal(v.AllSettings()) + if err != nil { + jww.FATAL.Println("Panic while encoding into YAML format.") + } + f.WriteString(string(b)) } return nil From 2715727d1f8fa834985b76ec43e2262cf79978bb Mon Sep 17 00:00:00 2001 From: g3rk6 Date: Mon, 25 Jan 2016 22:56:20 -0500 Subject: [PATCH 4/4] Fixed the issue of incorrect defer and error checking order. The error checking must be first otherwise it will cause panic. --- viper.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/viper.go b/viper.go index 9a2ace3..4eefc53 100644 --- a/viper.go +++ b/viper.go @@ -888,11 +888,10 @@ func (v *Viper) SaveConfig() error { } f, err := os.Create(v.getConfigFile()) - defer f.Close() - if err != nil { return err } + defer f.Close() switch v.getConfigType() { case "json":