mirror of
https://github.com/eosswedenorg/thalos
synced 2026-07-04 12:03:41 +02:00
Adding internal/config/builder.go
This commit is contained in:
parent
85da219349
commit
0d1bec6a62
2 changed files with 253 additions and 0 deletions
102
internal/config/builder.go
Normal file
102
internal/config/builder.go
Normal 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
|
||||||
|
}
|
||||||
151
internal/config/builder_test.go
Normal file
151
internal/config/builder_test.go
Normal file
|
|
@ -0,0 +1,151 @@
|
||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/eosswedenorg/thalos/internal/log"
|
||||||
|
"github.com/spf13/pflag"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
shipclient "github.com/eosswedenorg-go/antelope-ship-client"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBuilder(t *testing.T) {
|
||||||
|
expected := Config{
|
||||||
|
Name: "ship-reader-1",
|
||||||
|
Api: "http://127.0.0.1:8080",
|
||||||
|
MessageCodec: "mojibake",
|
||||||
|
Log: log.Config{
|
||||||
|
Filename: "some_file.log",
|
||||||
|
Directory: "/path/to/whatever",
|
||||||
|
MaxFileSize: 200,
|
||||||
|
MaxTime: 30 * time.Minute,
|
||||||
|
},
|
||||||
|
Ship: ShipConfig{
|
||||||
|
Url: "127.0.0.1:8089",
|
||||||
|
StartBlockNum: 23671836,
|
||||||
|
EndBlockNum: 23872222,
|
||||||
|
IrreversibleOnly: true,
|
||||||
|
MaxMessagesInFlight: 1337,
|
||||||
|
},
|
||||||
|
Telegram: TelegramConfig{
|
||||||
|
Id: "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw",
|
||||||
|
Channel: -123456789,
|
||||||
|
},
|
||||||
|
Redis: RedisConfig{
|
||||||
|
Addr: "localhost:6379",
|
||||||
|
User: "myuser",
|
||||||
|
Password: "passwd",
|
||||||
|
DB: 4,
|
||||||
|
Prefix: "some::ship",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
builder := NewBuilder()
|
||||||
|
builder.SetSource(bytes.NewBuffer([]byte(`
|
||||||
|
name: "ship-reader-1"
|
||||||
|
api: "http://127.0.0.1:8080"
|
||||||
|
message_codec: "mojibake"
|
||||||
|
log:
|
||||||
|
filename: some_file.log
|
||||||
|
directory: /path/to/whatever
|
||||||
|
maxtime: 30m
|
||||||
|
maxfilesize: 200b
|
||||||
|
ship:
|
||||||
|
url: "127.0.0.1:8089"
|
||||||
|
irreversible_only: true
|
||||||
|
max_messages_in_flight: 1337
|
||||||
|
start_block_num: 23671836
|
||||||
|
end_block_num: 23872222
|
||||||
|
telegram:
|
||||||
|
id: "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw"
|
||||||
|
channel: -123456789
|
||||||
|
redis:
|
||||||
|
addr: "localhost:6379"
|
||||||
|
user: "myuser"
|
||||||
|
password: "passwd"
|
||||||
|
db: 4
|
||||||
|
prefix: "some::ship"
|
||||||
|
`)))
|
||||||
|
|
||||||
|
cfg, err := builder.Build()
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, &expected, cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuilder_NilSource(t *testing.T) {
|
||||||
|
cfg, err := NewBuilder().Build()
|
||||||
|
require.Nil(t, cfg)
|
||||||
|
require.EqualError(t, err, "Config not set")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuilder_WithShorthandShipUrl(t *testing.T) {
|
||||||
|
expected := Config{
|
||||||
|
Name: "ship-reader-1",
|
||||||
|
Api: "http://127.0.0.1:8080",
|
||||||
|
MessageCodec: "json",
|
||||||
|
Log: log.Config{
|
||||||
|
MaxFileSize: 10 * 1000 * 1000, // 10 mb
|
||||||
|
MaxTime: time.Hour * 24,
|
||||||
|
},
|
||||||
|
Ship: ShipConfig{
|
||||||
|
Url: "127.0.0.1:8089",
|
||||||
|
StartBlockNum: shipclient.NULL_BLOCK_NUMBER,
|
||||||
|
EndBlockNum: shipclient.NULL_BLOCK_NUMBER,
|
||||||
|
MaxMessagesInFlight: 10,
|
||||||
|
IrreversibleOnly: false,
|
||||||
|
},
|
||||||
|
Telegram: TelegramConfig{
|
||||||
|
Id: "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw",
|
||||||
|
Channel: -123456789,
|
||||||
|
},
|
||||||
|
Redis: RedisConfig{
|
||||||
|
Addr: "localhost:6379",
|
||||||
|
Password: "passwd",
|
||||||
|
DB: 4,
|
||||||
|
Prefix: "some::ship",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
builder := NewBuilder()
|
||||||
|
builder.SetSource(bytes.NewBuffer([]byte(`
|
||||||
|
name: "ship-reader-1"
|
||||||
|
api: "http://127.0.0.1:8080"
|
||||||
|
ship: "127.0.0.1:8089"
|
||||||
|
telegram:
|
||||||
|
id: "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw"
|
||||||
|
channel: -123456789
|
||||||
|
redis:
|
||||||
|
addr: "localhost:6379"
|
||||||
|
password: "passwd"
|
||||||
|
db: 4
|
||||||
|
prefix: "some::ship"
|
||||||
|
`)))
|
||||||
|
|
||||||
|
cfg, err := builder.Build()
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, &expected, cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuilder_Flags(t *testing.T) {
|
||||||
|
flags := pflag.FlagSet{}
|
||||||
|
flags.StringP("log", "l", "", "")
|
||||||
|
|
||||||
|
require.NoError(t, flags.Set("log", "/path/to/logs"))
|
||||||
|
|
||||||
|
cfg, err := NewBuilder().
|
||||||
|
SetSource(bytes.NewReader([]byte(``))).
|
||||||
|
SetFlags(&flags).
|
||||||
|
Build()
|
||||||
|
|
||||||
|
expected := New()
|
||||||
|
expected.Log.Filename = "logs"
|
||||||
|
expected.Log.Directory = "/path/to"
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, &expected, cfg)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue