1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-06-16 04:24:56 +02:00

app/config/config.go: rework api.

This commit is contained in:
Henrik Hautakoski 2024-02-11 14:29:01 +01:00
parent 655dd730d7
commit e5e15a7645
3 changed files with 25 additions and 25 deletions

View file

@ -46,8 +46,8 @@ type Config struct {
Telegram TelegramConfig `yaml:"telegram"`
}
func Parse(data []byte) (*Config, error) {
cfg := Config{
func New() Config {
return Config{
MessageCodec: "json",
Log: log.Config{
MaxFileSize: 10 * 1000 * 1000, // 10 mb
@ -60,15 +60,10 @@ func Parse(data []byte) (*Config, error) {
IrreversibleOnly: false,
},
Redis: RedisConfig{
Addr: "localhost:6379",
Password: "",
DB: 0,
Prefix: "ship",
Addr: "localhost:6379",
Prefix: "ship",
},
}
err := yaml.Unmarshal(data, &cfg)
return &cfg, err
}
func (ship *ShipConfig) UnmarshalYAML(value *yaml.Node) error {
@ -87,11 +82,15 @@ func (ship *ShipConfig) UnmarshalYAML(value *yaml.Node) error {
return err
}
func Load(filename string) (*Config, error) {
func (cfg *Config) ReadYAML(data []byte) error {
return yaml.Unmarshal(data, cfg)
}
func (cfg *Config) ReadFile(filename string) error {
bytes, err := os.ReadFile(filename)
if err != nil {
return nil, err
return err
}
return Parse(bytes)
return cfg.ReadYAML(bytes)
}

View file

@ -10,7 +10,7 @@ import (
shipclient "github.com/eosswedenorg-go/antelope-ship-client"
)
func TestParse_Default(t *testing.T) {
func TestNew(t *testing.T) {
expected := Config{
MessageCodec: "json",
@ -34,12 +34,10 @@ func TestParse_Default(t *testing.T) {
},
}
cfg, err := Parse([]byte(``))
require.NoError(t, err)
require.Equal(t, cfg, &expected)
require.Equal(t, New(), expected)
}
func TestParse(t *testing.T) {
func TestReadYAML(t *testing.T) {
expected := Config{
Name: "ship-reader-1",
Api: "http://127.0.0.1:8080",
@ -70,7 +68,8 @@ func TestParse(t *testing.T) {
},
}
cfg, err := Parse([]byte(`
cfg := Config{}
err := cfg.ReadYAML([]byte(`
name: "ship-reader-1"
api: "http://127.0.0.1:8080"
message_codec: "mojibake"
@ -97,10 +96,10 @@ redis:
`))
require.NoError(t, err)
require.Equal(t, cfg, &expected)
require.Equal(t, cfg, expected)
}
func TestParseShorthandShipUrl(t *testing.T) {
func TestReadYAMLShorthandShipUrl(t *testing.T) {
expected := Config{
Name: "ship-reader-1",
Api: "http://127.0.0.1:8080",
@ -128,7 +127,9 @@ func TestParseShorthandShipUrl(t *testing.T) {
},
}
cfg, err := Parse([]byte(`
cfg := New()
err := cfg.ReadYAML([]byte(`
name: "ship-reader-1"
api: "http://127.0.0.1:8080"
ship: "127.0.0.1:8089"
@ -143,5 +144,5 @@ redis:
`))
require.NoError(t, err)
require.Equal(t, cfg, &expected)
require.Equal(t, cfg, expected)
}

View file

@ -37,7 +37,7 @@ import (
// Global variables
// ---------------------------
var conf *config.Config
var conf config.Config
var shClient *shipclient.Stream
@ -241,8 +241,8 @@ func serverCmd(ctx *cli.Context) error {
}
// Parse config
conf, err = config.Load(ctx.Path("config"))
if err != nil {
conf = config.New()
if err = conf.ReadFile(ctx.Path("config")); err != nil {
return err
}