mirror of
https://github.com/spf13/viper
synced 2024-12-23 12:07:02 +00:00
Add support for toml
This commit is contained in:
parent
4fae23fe25
commit
c4b0efecc9
1 changed files with 15 additions and 9 deletions
24
util.go
24
util.go
|
@ -164,31 +164,37 @@ func userHomeDir() string {
|
||||||
return os.Getenv("HOME")
|
return os.Getenv("HOME")
|
||||||
}
|
}
|
||||||
|
|
||||||
func marshalConfigWriter(out afero.File, c map[string]interface{}, configType string) error {
|
func marshalConfigWriter(f afero.File, c map[string]interface{}, configType string) error {
|
||||||
switch configType {
|
switch configType {
|
||||||
case "json":
|
case "json":
|
||||||
b, err := json.MarshalIndent(v.AllSettings(), "", " ")
|
b, err := json.MarshalIndent(v.AllSettings(), "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ConfigMarshalError{err}
|
return ConfigMarshalError{err}
|
||||||
}
|
}
|
||||||
_, err = out.WriteString(string(b))
|
_, err = f.WriteString(string(b))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ConfigMarshalError{err}
|
return ConfigMarshalError{err}
|
||||||
}
|
}
|
||||||
|
|
||||||
// case "toml":
|
case "hcl":
|
||||||
// w := bufio.NewWriter(out)
|
// TODO: How to convert map to hcl???
|
||||||
// if err := toml.NewEncoder(w).Encode(v.AllSettings()); err != nil {
|
|
||||||
// return ConfigMarshalError{err}
|
case "prop", "props", "properties":
|
||||||
// }
|
|
||||||
// w.Flush()
|
case "toml":
|
||||||
|
t := toml.TreeFromMap(v.AllSettings())
|
||||||
|
s := t.String()
|
||||||
|
_, err := f.WriteString(s)
|
||||||
|
if err != nil {
|
||||||
|
return ConfigMarshalError{err}
|
||||||
|
}
|
||||||
|
|
||||||
case "yaml", "yml":
|
case "yaml", "yml":
|
||||||
b, err := yaml.Marshal(v.AllSettings())
|
b, err := yaml.Marshal(v.AllSettings())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ConfigMarshalError{err}
|
return ConfigMarshalError{err}
|
||||||
}
|
}
|
||||||
_, err = out.WriteString(string(b))
|
_, err = f.WriteString(string(b))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ConfigMarshalError{err}
|
return ConfigMarshalError{err}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue