From e5e15a7645e813351dbc205463fcbb1434dbee11 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sun, 11 Feb 2024 14:29:01 +0100 Subject: [PATCH] app/config/config.go: rework api. --- app/config/config.go | 23 +++++++++++------------ app/config/config_test.go | 21 +++++++++++---------- cmd/thalos/server.go | 6 +++--- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/app/config/config.go b/app/config/config.go index e3050a2..3cc3ebc 100644 --- a/app/config/config.go +++ b/app/config/config.go @@ -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) } diff --git a/app/config/config_test.go b/app/config/config_test.go index d9aad83..f3737a7 100644 --- a/app/config/config_test.go +++ b/app/config/config_test.go @@ -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) } diff --git a/cmd/thalos/server.go b/cmd/thalos/server.go index 12211a7..eb6d6d9 100644 --- a/cmd/thalos/server.go +++ b/cmd/thalos/server.go @@ -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 }