mirror of
https://github.com/spf13/viper
synced 2024-12-23 03:57:01 +00:00
Add shared write function and safe methods
This commit is contained in:
parent
6b4d3dc059
commit
aa295b144a
1 changed files with 62 additions and 20 deletions
82
viper.go
82
viper.go
|
@ -1141,31 +1141,73 @@ func (v *Viper) MergeConfig(in io.Reader) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteConfig writes the current configuration to a file.
|
func writeConfig(filename string, force bool) error { return v.writeConfig(filename, force) }
|
||||||
func WriteConfig() error { return v.WriteConfig() }
|
func (v *Viper) writeConfig(filename string, force bool) error {
|
||||||
func (v *Viper) WriteConfig() error {
|
jww.INFO.Println("Attempting to write configuration to file.")
|
||||||
|
ext := filepath.Ext(filename)
|
||||||
jww.INFO.Println("Attempting to write config into the file.")
|
if len(ext) <= 1 {
|
||||||
// filename, err := v.getConfigFile()
|
return fmt.Errorf("Filename: %s requires valid extension.", filename)
|
||||||
filename := "out.yaml"
|
}
|
||||||
// if err != nil {
|
configType := ext[1:]
|
||||||
// return err
|
if !stringInSlice(configType, SupportedExts) {
|
||||||
// }
|
return UnsupportedConfigError(configType)
|
||||||
if !stringInSlice(v.getConfigType(), SupportedExts) {
|
|
||||||
return UnsupportedConfigError(v.getConfigType())
|
|
||||||
}
|
}
|
||||||
if v.config == nil {
|
if v.config == nil {
|
||||||
v.config = make(map[string]interface{})
|
v.config = make(map[string]interface{})
|
||||||
}
|
}
|
||||||
|
var flags int
|
||||||
file, err := v.fs.OpenFile(filename, os.O_CREATE|os.O_WRONLY, os.FileMode(0644))
|
if force == true {
|
||||||
|
flags = os.O_CREATE | os.O_WRONLY
|
||||||
|
} else {
|
||||||
|
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
||||||
|
flags = os.O_WRONLY
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("File: %s exists. Use WriteConfig to overwrite.", filename)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f, err := v.fs.OpenFile(filename, flags, os.FileMode(0644))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
return v.marshalWriter(f, v.config, configType)
|
||||||
return v.marshalWriter(file, v.config)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteConfig writes the current configuration to a file.
|
||||||
|
func WriteConfig() error { return v.WriteConfig() }
|
||||||
|
func (v *Viper) WriteConfig() error {
|
||||||
|
filename, err := v.getConfigFile()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return v.writeConfig(filename, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SafeWriteConfig writes current configuration to file only if the file does not exist.
|
||||||
|
func SafeWriteConfig() error { return v.SafeWriteConfig() }
|
||||||
|
func (v *Viper) SafeWriteConfig() error {
|
||||||
|
filename, err := v.getConfigFile()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return v.writeConfig(filename, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteConfigAs writes current configuration to a given filename.
|
||||||
|
func WriteConfigAs(filename string) error { return v.WriteConfigAs(filename) }
|
||||||
|
func (v *Viper) WriteConfigAs(filename string) error {
|
||||||
|
return v.writeConfig(filename, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SafeWriteConfigAs writes current configuration to a given filename is it does not exist.
|
||||||
|
func SafeWriteConfigAs(filename string) error { return v.SafeWriteConfigAs(filename) }
|
||||||
|
func (v *Viper) SafeWriteConfigAs(filename string) error {
|
||||||
|
return v.writeConfig(filename, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteConfigKey writes given key and value to file.
|
||||||
|
|
||||||
|
// WriteConfigKeyAs writes given key and value to a given filename.
|
||||||
|
|
||||||
func keyExists(k string, m map[string]interface{}) string {
|
func keyExists(k string, m map[string]interface{}) string {
|
||||||
lk := strings.ToLower(k)
|
lk := strings.ToLower(k)
|
||||||
for mk := range m {
|
for mk := range m {
|
||||||
|
@ -1285,12 +1327,12 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Marshal a map into Writer.
|
// Marshal a map into Writer.
|
||||||
func marshalWriter(out afero.File, c map[string]interface{}) error {
|
func marshalWriter(out afero.File, c map[string]interface{}, configType string) error {
|
||||||
return v.marshalWriter(out, c)
|
return v.marshalWriter(out, c, configType)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Viper) marshalWriter(out afero.File, c map[string]interface{}) error {
|
func (v *Viper) marshalWriter(out afero.File, c map[string]interface{}, configType string) error {
|
||||||
return marshalConfigWriter(out, c, v.getConfigType())
|
return marshalConfigWriter(out, c, configType)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Viper) insensitiviseMaps() {
|
func (v *Viper) insensitiviseMaps() {
|
||||||
|
|
Loading…
Reference in a new issue