mirror of
https://github.com/spf13/viper
synced 2024-12-23 03:57:01 +00:00
feat(encoding): integrate dotenv codec into Viper
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
parent
1d11247e33
commit
04d3a0cb02
1 changed files with 13 additions and 24 deletions
37
viper.go
37
viper.go
|
@ -39,9 +39,9 @@ import (
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/spf13/cast"
|
"github.com/spf13/cast"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
"github.com/subosito/gotenv"
|
|
||||||
|
|
||||||
"github.com/spf13/viper/internal/encoding"
|
"github.com/spf13/viper/internal/encoding"
|
||||||
|
"github.com/spf13/viper/internal/encoding/dotenv"
|
||||||
"github.com/spf13/viper/internal/encoding/hcl"
|
"github.com/spf13/viper/internal/encoding/hcl"
|
||||||
"github.com/spf13/viper/internal/encoding/ini"
|
"github.com/spf13/viper/internal/encoding/ini"
|
||||||
"github.com/spf13/viper/internal/encoding/javaproperties"
|
"github.com/spf13/viper/internal/encoding/javaproperties"
|
||||||
|
@ -367,6 +367,16 @@ func (v *Viper) resetEncoding() {
|
||||||
decoderRegistry.RegisterDecoder("prop", codec)
|
decoderRegistry.RegisterDecoder("prop", codec)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
codec := &dotenv.Codec{}
|
||||||
|
|
||||||
|
encoderRegistry.RegisterEncoder("dotenv", codec)
|
||||||
|
decoderRegistry.RegisterDecoder("dotenv", codec)
|
||||||
|
|
||||||
|
encoderRegistry.RegisterEncoder("env", codec)
|
||||||
|
decoderRegistry.RegisterDecoder("env", codec)
|
||||||
|
}
|
||||||
|
|
||||||
v.encoderRegistry = encoderRegistry
|
v.encoderRegistry = encoderRegistry
|
||||||
v.decoderRegistry = decoderRegistry
|
v.decoderRegistry = decoderRegistry
|
||||||
}
|
}
|
||||||
|
@ -1667,20 +1677,11 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error {
|
||||||
buf.ReadFrom(in)
|
buf.ReadFrom(in)
|
||||||
|
|
||||||
switch format := strings.ToLower(v.getConfigType()); format {
|
switch format := strings.ToLower(v.getConfigType()); format {
|
||||||
case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini", "properties", "props", "prop":
|
case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini", "properties", "props", "prop", "dotenv", "env":
|
||||||
err := v.decoderRegistry.Decode(format, buf.Bytes(), c)
|
err := v.decoderRegistry.Decode(format, buf.Bytes(), c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ConfigParseError{err}
|
return ConfigParseError{err}
|
||||||
}
|
}
|
||||||
|
|
||||||
case "dotenv", "env":
|
|
||||||
env, err := gotenv.StrictParse(buf)
|
|
||||||
if err != nil {
|
|
||||||
return ConfigParseError{err}
|
|
||||||
}
|
|
||||||
for k, v := range env {
|
|
||||||
c[k] = v
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
insensitiviseMap(c)
|
insensitiviseMap(c)
|
||||||
|
@ -1691,7 +1692,7 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error {
|
||||||
func (v *Viper) marshalWriter(f afero.File, configType string) error {
|
func (v *Viper) marshalWriter(f afero.File, configType string) error {
|
||||||
c := v.AllSettings()
|
c := v.AllSettings()
|
||||||
switch configType {
|
switch configType {
|
||||||
case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini", "prop", "props", "properties":
|
case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini", "prop", "props", "properties", "dotenv", "env":
|
||||||
b, err := v.encoderRegistry.Encode(configType, c)
|
b, err := v.encoderRegistry.Encode(configType, c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ConfigMarshalError{err}
|
return ConfigMarshalError{err}
|
||||||
|
@ -1701,18 +1702,6 @@ func (v *Viper) marshalWriter(f afero.File, configType string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ConfigMarshalError{err}
|
return ConfigMarshalError{err}
|
||||||
}
|
}
|
||||||
|
|
||||||
case "dotenv", "env":
|
|
||||||
lines := []string{}
|
|
||||||
for _, key := range v.AllKeys() {
|
|
||||||
envName := strings.ToUpper(strings.Replace(key, ".", "_", -1))
|
|
||||||
val := v.Get(key)
|
|
||||||
lines = append(lines, fmt.Sprintf("%v=%v", envName, val))
|
|
||||||
}
|
|
||||||
s := strings.Join(lines, "\n")
|
|
||||||
if _, err := f.WriteString(s); err != nil {
|
|
||||||
return ConfigMarshalError{err}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue