mirror of
https://github.com/eosswedenorg/thalos
synced 2026-06-16 04:24:56 +02:00
internal/config: add additional flags for config fields.
This commit is contained in:
parent
beb5b6cf04
commit
2db0a64bd4
3 changed files with 107 additions and 9 deletions
|
|
@ -22,8 +22,31 @@ type Builder struct {
|
|||
func NewBuilder() *Builder {
|
||||
return &Builder{
|
||||
binds: map[string]string{
|
||||
"ship.start_block_num": "start-block",
|
||||
"ship.end_block_num": "end-block",
|
||||
"api": "url",
|
||||
"message_codec": "codec",
|
||||
|
||||
// Redis
|
||||
"redis.addr": "redis-addr",
|
||||
"redis.user": "redis-user",
|
||||
"redis.password": "redis-password",
|
||||
"redis.db": "redis-db",
|
||||
"redis.prefix": "redis-prefix",
|
||||
|
||||
// Telegram
|
||||
"telegram.id": "telegram-id",
|
||||
"telegram.channel": "telegram-channel",
|
||||
|
||||
// Log
|
||||
"log.maxfilesize": "log-max-filesize",
|
||||
"log.maxtime": "log-max-time",
|
||||
|
||||
// Ship
|
||||
"ship.url": "ship-url",
|
||||
"ship.start_block_num": "start-block",
|
||||
"ship.end_block_num": "end-block",
|
||||
"ship.irreversible_only": "irreversible-only",
|
||||
"ship.max_messages_in_flight": "max-msg-in-flight",
|
||||
"ship.chain": "chain",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -133,16 +133,59 @@ redis:
|
|||
func TestBuilder_Flags(t *testing.T) {
|
||||
flags := GetFlags()
|
||||
|
||||
require.NoError(t, flags.Set("log", "/path/to/logs"))
|
||||
require.NoError(t, flags.Set("url", "https://myapi"))
|
||||
require.NoError(t, flags.Set("codec", "binary"))
|
||||
require.NoError(t, flags.Set("redis-addr", "154.223.38.15:6380"))
|
||||
require.NoError(t, flags.Set("redis-user", "myuser"))
|
||||
require.NoError(t, flags.Set("redis-password", "secret123"))
|
||||
require.NoError(t, flags.Set("redis-db", "3"))
|
||||
require.NoError(t, flags.Set("redis-prefix", "custom-prefix"))
|
||||
require.NoError(t, flags.Set("telegram-id", "72983126312982618"))
|
||||
require.NoError(t, flags.Set("telegram-channel", "-293492332"))
|
||||
require.NoError(t, flags.Set("log", "/path/to/logs/mylog"))
|
||||
require.NoError(t, flags.Set("log-max-filesize", "25mb"))
|
||||
require.NoError(t, flags.Set("log-max-time", "10m"))
|
||||
require.NoError(t, flags.Set("ship-url", "ws://myship.com:7823"))
|
||||
require.NoError(t, flags.Set("start-block", "7327833"))
|
||||
require.NoError(t, flags.Set("end-block", "329408392"))
|
||||
require.NoError(t, flags.Set("irreversible-only", "true"))
|
||||
require.NoError(t, flags.Set("max-msg-in-flight", "98"))
|
||||
require.NoError(t, flags.Set("chain", "wax"))
|
||||
|
||||
cfg, err := NewBuilder().
|
||||
SetSource(bytes.NewReader([]byte(``))).
|
||||
SetFlags(flags).
|
||||
Build()
|
||||
|
||||
expected := New()
|
||||
expected.Log.Filename = "logs"
|
||||
expected.Log.Directory = "/path/to"
|
||||
expected := Config{
|
||||
Api: "https://myapi",
|
||||
MessageCodec: "binary",
|
||||
Log: log.Config{
|
||||
Filename: "mylog",
|
||||
Directory: "/path/to/logs",
|
||||
MaxFileSize: 25 * 1000 * 1000, // 25 mb
|
||||
MaxTime: time.Minute * 10,
|
||||
},
|
||||
Ship: ShipConfig{
|
||||
Url: "ws://myship.com:7823",
|
||||
StartBlockNum: 7327833,
|
||||
EndBlockNum: 329408392,
|
||||
MaxMessagesInFlight: 98,
|
||||
IrreversibleOnly: true,
|
||||
Chain: "wax",
|
||||
},
|
||||
Telegram: TelegramConfig{
|
||||
Id: "72983126312982618",
|
||||
Channel: -293492332,
|
||||
},
|
||||
Redis: RedisConfig{
|
||||
Addr: "154.223.38.15:6380",
|
||||
User: "myuser",
|
||||
Password: "secret123",
|
||||
DB: 3,
|
||||
Prefix: "custom-prefix",
|
||||
},
|
||||
}
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, &expected, cfg)
|
||||
|
|
|
|||
|
|
@ -2,20 +2,52 @@ package config
|
|||
|
||||
import (
|
||||
"path"
|
||||
"time"
|
||||
|
||||
shipclient "github.com/eosswedenorg-go/antelope-ship-client"
|
||||
"github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
func GetFlags() *pflag.FlagSet {
|
||||
flags := pflag.FlagSet{}
|
||||
|
||||
// Cli only flags
|
||||
flags.StringP("config", "c", "./config.yml", "Config file to read")
|
||||
flags.StringP("level", "L", "info", "Log level to use")
|
||||
flags.StringP("log", "l", "", "Path to log file (default: print to stdout/stderr)")
|
||||
flags.StringP("pid", "p", "", "Where to write process id")
|
||||
flags.BoolP("no-state-cache", "n", false, "Force the application to take start block from config/api")
|
||||
|
||||
flags.Int("start-block", 0, "Start to stream from this block (default: config value, cache, head from api)")
|
||||
flags.Int("end-block", 0, "Stop streaming when this block is reached")
|
||||
// Generic
|
||||
flags.StringP("url", "u", "", "Url to antelope api")
|
||||
flags.String("codec", "json", "Codec used to send messages")
|
||||
|
||||
// Redis
|
||||
flags.String("redis-addr", "127.0.0.1:6379", "host:port to redis server")
|
||||
flags.String("redis-user", "", "Redis username")
|
||||
flags.String("redis-password", "", "Redis password")
|
||||
flags.Int("redis-db", 0, "Redis database")
|
||||
flags.String("redis-prefix", "ship", "Redis channel prefix")
|
||||
|
||||
// Telegram
|
||||
flags.String("telegram-id", "", "Id of telegram bot")
|
||||
flags.Int64("telegram-channel", 0, "Telegram channel to send notifications to")
|
||||
|
||||
// Log
|
||||
flags.StringP("log", "l", "", "Path to log file (default: print to stdout/stderr)")
|
||||
flags.String("log-max-filesize", "10mb", "Max filesize for logfile to rotate")
|
||||
flags.Duration("log-max-time", time.Hour*24, "Max time for logfile to rotate")
|
||||
|
||||
// Ship
|
||||
flags.String("ship-url", "ws://127.0.0.1:8080", "Url to ship node")
|
||||
flags.Uint32("start-block", shipclient.NULL_BLOCK_NUMBER, "Start to stream from this block")
|
||||
flags.Uint32("end-block", shipclient.NULL_BLOCK_NUMBER, "Stop streaming when this block is reached")
|
||||
|
||||
flags.Lookup("start-block").DefValue = "config value, cache, head from api"
|
||||
flags.Lookup("end-block").DefValue = "none"
|
||||
|
||||
flags.Bool("irreversible-only", false, "Only stream irreversible blocks from ship")
|
||||
flags.Int("max-msg-in-flight", 10, "Maximum messages that can be sent from SHIP without acknowledgement")
|
||||
flags.String("chain", "", "ChainID used in channel namespace, can be any string (default from api)")
|
||||
|
||||
return &flags
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue