Added method to write into TOML file.

This commit is contained in:
g3rk6 2015-09-08 03:25:36 -04:00
parent 0c82789feb
commit 48e0b61d8c

View file

@ -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) }