34 | 34 |
"time"
|
35 | 35 |
|
36 | 36 |
"github.com/fsnotify/fsnotify"
|
37 | |
"github.com/magiconair/properties"
|
38 | 37 |
"github.com/mitchellh/mapstructure"
|
39 | 38 |
"github.com/spf13/afero"
|
40 | 39 |
"github.com/spf13/cast"
|
|
44 | 43 |
"github.com/spf13/viper/internal/encoding"
|
45 | 44 |
"github.com/spf13/viper/internal/encoding/hcl"
|
46 | 45 |
"github.com/spf13/viper/internal/encoding/ini"
|
|
46 |
"github.com/spf13/viper/internal/encoding/javaproperties"
|
47 | 47 |
"github.com/spf13/viper/internal/encoding/json"
|
48 | 48 |
"github.com/spf13/viper/internal/encoding/toml"
|
49 | 49 |
"github.com/spf13/viper/internal/encoding/yaml"
|
|
214 | 214 |
aliases map[string]string
|
215 | 215 |
typeByDefValue bool
|
216 | 216 |
|
217 | |
// Store read properties on the object so that we can write back in order with comments.
|
218 | |
// This will only be used if the configuration read is a properties file.
|
219 | |
properties *properties.Properties
|
220 | |
|
221 | 217 |
onConfigChange func(fsnotify.Event)
|
222 | 218 |
|
223 | 219 |
logger Logger
|
|
353 | 349 |
|
354 | 350 |
encoderRegistry.RegisterEncoder("ini", codec)
|
355 | 351 |
decoderRegistry.RegisterDecoder("ini", codec)
|
|
352 |
}
|
|
353 |
|
|
354 |
{
|
|
355 |
codec := &javaproperties.Codec{
|
|
356 |
KeyDelimiter: v.keyDelim,
|
|
357 |
}
|
|
358 |
|
|
359 |
encoderRegistry.RegisterEncoder("properties", codec)
|
|
360 |
decoderRegistry.RegisterDecoder("properties", codec)
|
|
361 |
|
|
362 |
encoderRegistry.RegisterEncoder("props", codec)
|
|
363 |
decoderRegistry.RegisterDecoder("props", codec)
|
|
364 |
|
|
365 |
encoderRegistry.RegisterEncoder("prop", codec)
|
|
366 |
decoderRegistry.RegisterDecoder("prop", codec)
|
356 | 367 |
}
|
357 | 368 |
|
358 | 369 |
v.encoderRegistry = encoderRegistry
|
|
1655 | 1666 |
buf.ReadFrom(in)
|
1656 | 1667 |
|
1657 | 1668 |
switch format := strings.ToLower(v.getConfigType()); format {
|
1658 | |
case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini":
|
|
1669 |
case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini", "properties", "props", "prop":
|
1659 | 1670 |
err := v.decoderRegistry.Decode(format, buf.Bytes(), c)
|
1660 | 1671 |
if err != nil {
|
1661 | 1672 |
return ConfigParseError{err}
|
|
1669 | 1680 |
for k, v := range env {
|
1670 | 1681 |
c[k] = v
|
1671 | 1682 |
}
|
1672 | |
|
1673 | |
case "properties", "props", "prop":
|
1674 | |
v.properties = properties.NewProperties()
|
1675 | |
var err error
|
1676 | |
if v.properties, err = properties.Load(buf.Bytes(), properties.UTF8); err != nil {
|
1677 | |
return ConfigParseError{err}
|
1678 | |
}
|
1679 | |
for _, key := range v.properties.Keys() {
|
1680 | |
value, _ := v.properties.Get(key)
|
1681 | |
// recursively build nested maps
|
1682 | |
path := strings.Split(key, ".")
|
1683 | |
lastKey := strings.ToLower(path[len(path)-1])
|
1684 | |
deepestMap := deepSearch(c, path[0:len(path)-1])
|
1685 | |
// set innermost value
|
1686 | |
deepestMap[lastKey] = value
|
1687 | |
}
|
1688 | 1683 |
}
|
1689 | 1684 |
|
1690 | 1685 |
insensitiviseMap(c)
|
|
1695 | 1690 |
func (v *Viper) marshalWriter(f afero.File, configType string) error {
|
1696 | 1691 |
c := v.AllSettings()
|
1697 | 1692 |
switch configType {
|
1698 | |
case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini":
|
|
1693 |
case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini", "prop", "props", "properties":
|
1699 | 1694 |
b, err := v.encoderRegistry.Encode(configType, c)
|
1700 | 1695 |
if err != nil {
|
1701 | 1696 |
return ConfigMarshalError{err}
|
1702 | 1697 |
}
|
1703 | 1698 |
|
1704 | 1699 |
_, err = f.WriteString(string(b))
|
1705 | |
if err != nil {
|
1706 | |
return ConfigMarshalError{err}
|
1707 | |
}
|
1708 | |
|
1709 | |
case "prop", "props", "properties":
|
1710 | |
if v.properties == nil {
|
1711 | |
v.properties = properties.NewProperties()
|
1712 | |
}
|
1713 | |
p := v.properties
|
1714 | |
for _, key := range v.AllKeys() {
|
1715 | |
_, _, err := p.Set(key, v.GetString(key))
|
1716 | |
if err != nil {
|
1717 | |
return ConfigMarshalError{err}
|
1718 | |
}
|
1719 | |
}
|
1720 | |
_, err := p.WriteComment(f, "#", properties.UTF8)
|
1721 | 1700 |
if err != nil {
|
1722 | 1701 |
return ConfigMarshalError{err}
|
1723 | 1702 |
}
|