99 lines
2.2 KiB
Go
99 lines
2.2 KiB
Go
|
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
|
||
|
}
|