1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-08-31 21:08:14 +02:00

Adding internal/config/builder.go

This commit is contained in:
Henrik Hautakoski 2024-02-18 10:43:58 +01:00
parent 85da219349
commit 0d1bec6a62
2 changed files with 253 additions and 0 deletions

102
internal/config/builder.go Normal file
View file

@ -0,0 +1,102 @@
package config
import (
"errors"
"io"
"os"
"github.com/mitchellh/mapstructure"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
// This is a simple module that encapsulate the creation
// of a config object and can override values from cli flags.
type Builder struct {
in io.Reader
flags *pflag.FlagSet
binds map[string]string
}
func NewBuilder() *Builder {
return &Builder{
binds: map[string]string{},
}
}
// Set the config file to read
func (b *Builder) SetConfigFile(filename string) *Builder {
file, _ := os.Open(filename)
return b.SetSource(file)
}
// Set the source to read
func (b *Builder) SetSource(in io.Reader) *Builder {
b.in = in
return b
}
// Set all flags that the builder should use.
func (b *Builder) SetFlags(flags *pflag.FlagSet) *Builder {
b.flags = flags
return b
}
// Add a flag to the builder.
func (b *Builder) AddFlag(flag *pflag.Flag) *Builder {
b.flags.AddFlag(flag)
return b
}
// Build the config object from file, cli-flags
func (b *Builder) Build() (*Config, error) {
if b.in == nil {
return nil, errors.New("Config not set")
}
conf := New()
v := viper.New()
v.SetConfigType("yaml")
if b.flags != nil {
// bind flags in viper.
for key, flagname := range b.binds {
flag := b.flags.Lookup(flagname)
if flag == nil {
continue
}
if err := v.BindPFlag(key, flag); err != nil {
return nil, err
}
}
}
// Read config and unmarshal
if err := v.ReadConfig(b.in); err != nil {
return nil, err
}
decoders := mapstructure.ComposeDecodeHookFunc(
mapstructure.TextUnmarshallerHookFunc(),
mapstructure.StringToTimeDurationHookFunc(),
mapstructure.StringToSliceHookFunc(","),
decodeShorthandShipConfig,
)
err := v.Unmarshal(&conf, viper.DecodeHook(decoders))
if err != nil {
return nil, err
}
// Call custom handler.
if b.flags != nil {
if err := conf.ReadCliFlags(b.flags); err != nil {
return nil, err
}
}
return &conf, nil
}