mirror of
https://github.com/spf13/viper
synced 2025-05-06 20:27:17 +00:00
Added method to write into TOML file.
This commit is contained in:
parent
0967fc9ace
commit
985fae64ee
1 changed files with 29 additions and 1 deletions
28
viper.go
28
viper.go
|
@ -20,6 +20,7 @@
|
|||
package viper
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
|
@ -31,6 +32,7 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"github.com/spf13/afero"
|
||||
|
@ -1067,6 +1069,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
|
||||
}
|
||||
|
||||
// SetDefault sets the default value for this key.
|
||||
// SetDefault is case-insensitive for a key.
|
||||
// Default only used when no value is provided by the user via flag, config or ENV.
|
||||
|
|
Loading…
Add table
Reference in a new issue