feat(encoding): integrate ini 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
40 | 40 | "github.com/spf13/cast" |
41 | 41 | "github.com/spf13/pflag" |
42 | 42 | "github.com/subosito/gotenv" |
43 | "gopkg.in/ini.v1" | |
44 | 43 | |
45 | 44 | "github.com/spf13/viper/internal/encoding" |
46 | 45 | "github.com/spf13/viper/internal/encoding/hcl" |
46 | "github.com/spf13/viper/internal/encoding/ini" | |
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" |
345 | 345 | decoderRegistry.RegisterDecoder("tfvars", codec) |
346 | 346 | } |
347 | 347 | |
348 | { | |
349 | codec := ini.Codec{ | |
350 | KeyDelimiter: v.keyDelim, | |
351 | LoadOptions: v.iniLoadOptions, | |
352 | } | |
353 | ||
354 | encoderRegistry.RegisterEncoder("ini", codec) | |
355 | decoderRegistry.RegisterDecoder("ini", codec) | |
356 | } | |
357 | ||
348 | 358 | v.encoderRegistry = encoderRegistry |
349 | 359 | v.decoderRegistry = decoderRegistry |
350 | 360 | } |
1645 | 1655 | buf.ReadFrom(in) |
1646 | 1656 | |
1647 | 1657 | switch format := strings.ToLower(v.getConfigType()); format { |
1648 | case "yaml", "yml", "json", "toml", "hcl", "tfvars": | |
1658 | case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini": | |
1649 | 1659 | err := v.decoderRegistry.Decode(format, buf.Bytes(), c) |
1650 | 1660 | if err != nil { |
1651 | 1661 | return ConfigParseError{err} |
1675 | 1685 | // set innermost value |
1676 | 1686 | deepestMap[lastKey] = value |
1677 | 1687 | } |
1678 | ||
1679 | case "ini": | |
1680 | cfg := ini.Empty(v.iniLoadOptions) | |
1681 | err := cfg.Append(buf.Bytes()) | |
1682 | if err != nil { | |
1683 | return ConfigParseError{err} | |
1684 | } | |
1685 | sections := cfg.Sections() | |
1686 | for i := 0; i < len(sections); i++ { | |
1687 | section := sections[i] | |
1688 | keys := section.Keys() | |
1689 | for j := 0; j < len(keys); j++ { | |
1690 | key := keys[j] | |
1691 | value := cfg.Section(section.Name()).Key(key.Name()).String() | |
1692 | c[section.Name()+"."+key.Name()] = value | |
1693 | } | |
1694 | } | |
1695 | 1688 | } |
1696 | 1689 | |
1697 | 1690 | insensitiviseMap(c) |
1702 | 1695 | func (v *Viper) marshalWriter(f afero.File, configType string) error { |
1703 | 1696 | c := v.AllSettings() |
1704 | 1697 | switch configType { |
1705 | case "yaml", "yml", "json", "toml", "hcl", "tfvars": | |
1698 | case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini": | |
1706 | 1699 | b, err := v.encoderRegistry.Encode(configType, c) |
1707 | 1700 | if err != nil { |
1708 | 1701 | return ConfigMarshalError{err} |
1740 | 1733 | if _, err := f.WriteString(s); err != nil { |
1741 | 1734 | return ConfigMarshalError{err} |
1742 | 1735 | } |
1743 | ||
1744 | case "ini": | |
1745 | keys := v.AllKeys() | |
1746 | cfg := ini.Empty() | |
1747 | ini.PrettyFormat = false | |
1748 | for i := 0; i < len(keys); i++ { | |
1749 | key := keys[i] | |
1750 | lastSep := strings.LastIndex(key, ".") | |
1751 | sectionName := key[:(lastSep)] | |
1752 | keyName := key[(lastSep + 1):] | |
1753 | if sectionName == "default" { | |
1754 | sectionName = "" | |
1755 | } | |
1756 | cfg.Section(sectionName).Key(keyName).SetValue(v.GetString(key)) | |
1757 | } | |
1758 | cfg.WriteTo(f) | |
1759 | 1736 | } |
1760 | 1737 | return nil |
1761 | 1738 | } |