feat(encoding): integrate dotenv codec into Viper
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
Mark Sagi-Kazar authored 2 years ago
Márk Sági-Kazár committed 2 years ago
38 | 38 | "github.com/spf13/afero" |
39 | 39 | "github.com/spf13/cast" |
40 | 40 | "github.com/spf13/pflag" |
41 | "github.com/subosito/gotenv" | |
42 | 41 | |
43 | 42 | "github.com/spf13/viper/internal/encoding" |
43 | "github.com/spf13/viper/internal/encoding/dotenv" | |
44 | 44 | "github.com/spf13/viper/internal/encoding/hcl" |
45 | 45 | "github.com/spf13/viper/internal/encoding/ini" |
46 | 46 | "github.com/spf13/viper/internal/encoding/javaproperties" |
366 | 366 | decoderRegistry.RegisterDecoder("prop", codec) |
367 | 367 | } |
368 | 368 | |
369 | { | |
370 | codec := &dotenv.Codec{} | |
371 | ||
372 | encoderRegistry.RegisterEncoder("dotenv", codec) | |
373 | decoderRegistry.RegisterDecoder("dotenv", codec) | |
374 | ||
375 | encoderRegistry.RegisterEncoder("env", codec) | |
376 | decoderRegistry.RegisterDecoder("env", codec) | |
377 | } | |
378 | ||
369 | 379 | v.encoderRegistry = encoderRegistry |
370 | 380 | v.decoderRegistry = decoderRegistry |
371 | 381 | } |
1666 | 1676 | buf.ReadFrom(in) |
1667 | 1677 | |
1668 | 1678 | switch format := strings.ToLower(v.getConfigType()); format { |
1669 | case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini", "properties", "props", "prop": | |
1679 | case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini", "properties", "props", "prop", "dotenv", "env": | |
1670 | 1680 | err := v.decoderRegistry.Decode(format, buf.Bytes(), c) |
1671 | 1681 | if err != nil { |
1672 | 1682 | return ConfigParseError{err} |
1673 | } | |
1674 | ||
1675 | case "dotenv", "env": | |
1676 | env, err := gotenv.StrictParse(buf) | |
1677 | if err != nil { | |
1678 | return ConfigParseError{err} | |
1679 | } | |
1680 | for k, v := range env { | |
1681 | c[k] = v | |
1682 | 1683 | } |
1683 | 1684 | } |
1684 | 1685 | |
1690 | 1691 | func (v *Viper) marshalWriter(f afero.File, configType string) error { |
1691 | 1692 | c := v.AllSettings() |
1692 | 1693 | switch configType { |
1693 | case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini", "prop", "props", "properties": | |
1694 | case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini", "prop", "props", "properties", "dotenv", "env": | |
1694 | 1695 | b, err := v.encoderRegistry.Encode(configType, c) |
1695 | 1696 | if err != nil { |
1696 | 1697 | return ConfigMarshalError{err} |
1698 | 1699 | |
1699 | 1700 | _, err = f.WriteString(string(b)) |
1700 | 1701 | if err != nil { |
1701 | return ConfigMarshalError{err} | |
1702 | } | |
1703 | ||
1704 | case "dotenv", "env": | |
1705 | lines := []string{} | |
1706 | for _, key := range v.AllKeys() { | |
1707 | envName := strings.ToUpper(strings.Replace(key, ".", "_", -1)) | |
1708 | val := v.Get(key) | |
1709 | lines = append(lines, fmt.Sprintf("%v=%v", envName, val)) | |
1710 | } | |
1711 | s := strings.Join(lines, "\n") | |
1712 | if _, err := f.WriteString(s); err != nil { | |
1713 | 1702 | return ConfigMarshalError{err} |
1714 | 1703 | } |
1715 | 1704 | } |