Codebase list golang-github-spf13-viper / 4309360
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
1 changed file(s) with 13 addition(s) and 36 deletion(s). Raw diff Collapse all Expand all
4040 "github.com/spf13/cast"
4141 "github.com/spf13/pflag"
4242 "github.com/subosito/gotenv"
43 "gopkg.in/ini.v1"
4443
4544 "github.com/spf13/viper/internal/encoding"
4645 "github.com/spf13/viper/internal/encoding/hcl"
46 "github.com/spf13/viper/internal/encoding/ini"
4747 "github.com/spf13/viper/internal/encoding/json"
4848 "github.com/spf13/viper/internal/encoding/toml"
4949 "github.com/spf13/viper/internal/encoding/yaml"
345345 decoderRegistry.RegisterDecoder("tfvars", codec)
346346 }
347347
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
348358 v.encoderRegistry = encoderRegistry
349359 v.decoderRegistry = decoderRegistry
350360 }
16451655 buf.ReadFrom(in)
16461656
16471657 switch format := strings.ToLower(v.getConfigType()); format {
1648 case "yaml", "yml", "json", "toml", "hcl", "tfvars":
1658 case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini":
16491659 err := v.decoderRegistry.Decode(format, buf.Bytes(), c)
16501660 if err != nil {
16511661 return ConfigParseError{err}
16751685 // set innermost value
16761686 deepestMap[lastKey] = value
16771687 }
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 }
16951688 }
16961689
16971690 insensitiviseMap(c)
17021695 func (v *Viper) marshalWriter(f afero.File, configType string) error {
17031696 c := v.AllSettings()
17041697 switch configType {
1705 case "yaml", "yml", "json", "toml", "hcl", "tfvars":
1698 case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini":
17061699 b, err := v.encoderRegistry.Encode(configType, c)
17071700 if err != nil {
17081701 return ConfigMarshalError{err}
17401733 if _, err := f.WriteString(s); err != nil {
17411734 return ConfigMarshalError{err}
17421735 }
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)
17591736 }
17601737 return nil
17611738 }