config: added multiple provider

This commit is contained in:
Tigor Hutasuhut 2024-04-06 21:23:55 +07:00
parent 95f5923779
commit b2e2d53352
4 changed files with 119 additions and 31 deletions

98
config/builder.go Normal file
View file

@ -0,0 +1,98 @@
package config
import (
"os"
"strings"
"github.com/knadh/koanf/parsers/json"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/confmap"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/providers/rawbytes"
"github.com/knadh/koanf/v2"
)
type ConfigBuilder struct {
koanf *koanf.Koanf
err error
}
func NewConfigBuilder() *ConfigBuilder {
return &ConfigBuilder{koanf: koanf.New(".")}
}
func (builder *ConfigBuilder) Build() *Config {
return &Config{Koanf: builder.koanf}
}
func (builder *ConfigBuilder) BuildHandle() (*Config, error) {
return &Config{Koanf: builder.koanf}, builder.err
}
func (builder *ConfigBuilder) LoadDefault() *ConfigBuilder {
provider := confmap.Provider(map[string]any{
"log.enable": true,
"log.source": true,
"log.format": "pretty",
"log.level": "info",
"log.output": "stderr",
}, ".")
_ = builder.koanf.Load(provider, nil)
return builder
}
func (builder *ConfigBuilder) LoadJSON(b []byte) *ConfigBuilder {
provider := rawbytes.Provider(b)
err := builder.koanf.Load(provider, json.Parser())
if err != nil {
builder.err = err
}
return builder
}
func (builder *ConfigBuilder) LoadJSONFile(path string) *ConfigBuilder {
if _, err := os.Stat(path); err != nil {
return builder
}
provider := file.Provider(path)
err := builder.koanf.Load(provider, json.Parser())
if err != nil {
builder.err = err
}
return builder
}
func (builder *ConfigBuilder) LoadYaml(b []byte) *ConfigBuilder {
provider := rawbytes.Provider(b)
err := builder.koanf.Load(provider, yaml.Parser())
if err != nil {
builder.err = err
}
return builder
}
func (builder *ConfigBuilder) LoadYamlFile(path string) *ConfigBuilder {
if _, err := os.Stat(path); err != nil {
return builder
}
provider := file.Provider(path)
err := builder.koanf.Load(provider, yaml.Parser())
if err != nil {
builder.err = err
}
return builder
}
func (builder *ConfigBuilder) LoadEnv() *ConfigBuilder {
provider := env.Provider("REDMAGE_", ".", func(s string) string {
s = strings.TrimPrefix(s, "REDMAGE_")
s = strings.ToLower(s)
s = strings.ReplaceAll(s, "_", ".")
return s
})
_ = builder.koanf.Load(provider, nil)
return builder
}

View file

@ -1,7 +1,6 @@
package config
import (
"github.com/knadh/koanf/providers/confmap"
"github.com/knadh/koanf/v2"
)
@ -12,33 +11,3 @@ type Config struct {
func EmptyConfig() *Config {
return NewConfigBuilder().Build()
}
type ConfigBuilder struct {
koanf *koanf.Koanf
err error
}
func NewConfigBuilder() *ConfigBuilder {
return &ConfigBuilder{koanf: koanf.New(".")}
}
func (builder *ConfigBuilder) Build() *Config {
return &Config{Koanf: builder.koanf}
}
func (builder *ConfigBuilder) BuildHandle() (*Config, error) {
return &Config{Koanf: builder.koanf}, builder.err
}
func (builder *ConfigBuilder) LoadDefault() *ConfigBuilder {
provider := confmap.Provider(map[string]any{
"log.enable": true,
"log.source": true,
"log.format": "pretty",
"log.level": "info",
"log.output": "stderr",
}, ".")
_ = builder.koanf.Load(provider, nil)
return builder
}

7
go.mod
View file

@ -13,11 +13,18 @@ require (
)
require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/knadh/koanf/parsers/json v0.1.0 // indirect
github.com/knadh/koanf/parsers/yaml v0.1.0 // indirect
github.com/knadh/koanf/providers/env v0.1.0 // indirect
github.com/knadh/koanf/providers/file v0.1.0 // indirect
github.com/knadh/koanf/providers/rawbytes v0.1.0 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

14
go.sum
View file

@ -1,6 +1,8 @@
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s=
github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 h1:TQcrn6Wq+sKGkpyPvppOz99zsMBaUOKXq6HSv655U1c=
@ -9,8 +11,18 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/knadh/koanf/maps v0.1.1 h1:G5TjmUh2D7G2YWf5SQQqSiHRJEjaicvU0KpypqB3NIs=
github.com/knadh/koanf/maps v0.1.1/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI=
github.com/knadh/koanf/parsers/json v0.1.0 h1:dzSZl5pf5bBcW0Acnu20Djleto19T0CfHcvZ14NJ6fU=
github.com/knadh/koanf/parsers/json v0.1.0/go.mod h1:ll2/MlXcZ2BfXD6YJcjVFzhG9P0TdJ207aIBKQhV2hY=
github.com/knadh/koanf/parsers/yaml v0.1.0 h1:ZZ8/iGfRLvKSaMEECEBPM1HQslrZADk8fP1XFUxVI5w=
github.com/knadh/koanf/parsers/yaml v0.1.0/go.mod h1:cvbUDC7AL23pImuQP0oRw/hPuccrNBS2bps8asS0CwY=
github.com/knadh/koanf/providers/confmap v0.1.0 h1:gOkxhHkemwG4LezxxN8DMOFopOPghxRVp7JbIvdvqzU=
github.com/knadh/koanf/providers/confmap v0.1.0/go.mod h1:2uLhxQzJnyHKfxG927awZC7+fyHFdQkd697K4MdLnIU=
github.com/knadh/koanf/providers/env v0.1.0 h1:LqKteXqfOWyx5Ab9VfGHmjY9BvRXi+clwyZozgVRiKg=
github.com/knadh/koanf/providers/env v0.1.0/go.mod h1:RE8K9GbACJkeEnkl8L/Qcj8p4ZyPXZIQ191HJi44ZaQ=
github.com/knadh/koanf/providers/file v0.1.0 h1:fs6U7nrV58d3CFAFh8VTde8TM262ObYf3ODrc//Lp+c=
github.com/knadh/koanf/providers/file v0.1.0/go.mod h1:rjJ/nHQl64iYCtAW2QQnF0eSmDEX/YZ/eNFj5yR6BvA=
github.com/knadh/koanf/providers/rawbytes v0.1.0 h1:dpzgu2KO6uf6oCb4aP05KDmKmAmI51k5pe8RYKQ0qME=
github.com/knadh/koanf/providers/rawbytes v0.1.0/go.mod h1:mMTB1/IcJ/yE++A2iEZbY1MLygX7vttU+C+S/YmPu9c=
github.com/knadh/koanf/v2 v2.1.1 h1:/R8eXqasSTsmDCsAyYj+81Wteg8AqrV9CP6gvsTsOmM=
github.com/knadh/koanf/v2 v2.1.1/go.mod h1:4mnTRbZCK+ALuBXHZMjDfG9y714L7TykVnZkXbMU3Es=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
@ -28,8 +40,10 @@ github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyh
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=