From e4c852c99e0ca830e94c4ade99601a248119c923 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 24 Apr 2023 17:44:31 +0200 Subject: [PATCH] app/config/config.go: Move from json format to yaml. --- .gitignore | 2 +- app/config/config.go | 51 +++++++++++++++------------- app/config/config_test.go | 71 ++++++++++++++++++--------------------- cmd/main/main.go | 2 +- config.example.json | 21 ------------ config.example.yml | 20 +++++++++++ go.mod | 2 +- 7 files changed, 83 insertions(+), 86 deletions(-) delete mode 100644 config.example.json create mode 100644 config.example.yml diff --git a/.gitignore b/.gitignore index 2edaacd..1e5f400 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ build/ -config.json +config.yml diff --git a/app/config/config.go b/app/config/config.go index d72b56c..dc32584 100644 --- a/app/config/config.go +++ b/app/config/config.go @@ -1,42 +1,43 @@ package config import ( - "encoding/json" "io/ioutil" + "gopkg.in/yaml.v3" + shipclient "github.com/eosswedenorg-go/antelope-ship-client" ) type RedisConfig struct { - Addr string `json:"addr"` - Password string `json:"password"` - DB int `json:"db"` - CacheID string `json:"cache_id"` - Prefix string `json:"prefix"` + Addr string `yaml:"addr"` + Password string `yaml:"password"` + DB int `yaml:"db"` + CacheID string `yaml:"cache_id"` + Prefix string `yaml:"prefix"` } type TelegramConfig struct { - Id string `json:"id"` - Channel int64 `json:"channel"` + Id string `yaml:"id"` + Channel int64 `yaml:"channel"` } type ShipConfig struct { - Url string `json:"url"` - IrreversibleOnly bool `json:"irreversible_only"` - MaxMessagesInFlight uint32 `json:"max_messages_in_flight"` - StartBlockNum uint32 `json:"start_block_num"` - EndBlockNum uint32 `json:"end_block_num"` + Url string `yaml:"url"` + IrreversibleOnly bool `yaml:"irreversible_only"` + MaxMessagesInFlight uint32 `yaml:"max_messages_in_flight"` + StartBlockNum uint32 `yaml:"start_block_num"` + EndBlockNum uint32 `yaml:"end_block_num"` } type Config struct { - Name string `json:"name"` - Ship ShipConfig `json:"ship"` - Api string `json:"api"` + Name string `yaml:"name"` + Ship ShipConfig `yaml:"ship"` + Api string `yaml:"api"` - Redis RedisConfig `json:"redis"` - MessageCodec string `json:"message_codec"` + Redis RedisConfig `yaml:"redis"` + MessageCodec string `yaml:"message_codec"` - Telegram TelegramConfig `json:"telegram"` + Telegram TelegramConfig `yaml:"telegram"` } func Parse(data []byte) (*Config, error) { @@ -56,21 +57,23 @@ func Parse(data []byte) (*Config, error) { }, } - err := json.Unmarshal(data, &cfg) + err := yaml.Unmarshal(data, &cfg) return &cfg, err } -func (ship *ShipConfig) UnmarshalJSON(data []byte) error { +func (ship *ShipConfig) UnmarshalYAML(value *yaml.Node) error { var err error - if err = json.Unmarshal(data, &ship.Url); err != nil { - // + if value.Kind == yaml.ScalarNode { + ship.Url = value.Value + } else { type ShipConfigRaw ShipConfig raw := ShipConfigRaw(*ship) - if err = json.Unmarshal(data, &raw); err == nil { + if err = value.Decode(&raw); err == nil { *ship = ShipConfig(raw) } } + return err } diff --git a/app/config/config_test.go b/app/config/config_test.go index e87e979..9b61eb7 100644 --- a/app/config/config_test.go +++ b/app/config/config_test.go @@ -27,7 +27,7 @@ func TestParse_Default(t *testing.T) { }, } - cfg, err := Parse([]byte(`{}`)) + cfg, err := Parse([]byte(``)) require.NoError(t, err) require.Equal(t, cfg, &expected) } @@ -56,28 +56,25 @@ func TestParse(t *testing.T) { }, } - cfg, err := Parse([]byte(`{ - "name": "ship-reader-1", - "api": "http://127.0.0.1:8080", - "message_codec": "mojibake", - "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", - "password": "passwd", - "db": 4, - "prefix": "some::ship" - } - }`)) + cfg, err := Parse([]byte(` +name: "ship-reader-1" +api: "http://127.0.0.1:8080" +message_codec: "mojibake" +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" + password: "passwd" + db: 4 + prefix: "some::ship" +`)) require.NoError(t, err) require.Equal(t, cfg, &expected) @@ -107,21 +104,19 @@ func TestParseShorthandShipUrl(t *testing.T) { }, } - cfg, err := Parse([]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 := Parse([]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" +`)) require.NoError(t, err) require.Equal(t, cfg, &expected) diff --git a/cmd/main/main.go b/cmd/main/main.go index 52aeab2..223d71d 100644 --- a/cmd/main/main.go +++ b/cmd/main/main.go @@ -110,7 +110,7 @@ func main() { showHelp := getopt.BoolLong("help", 'h', "display this help text") showVersion := getopt.BoolLong("version", 'v', "display this help text") - configFile := getopt.StringLong("config", 'c', "./config.json", "Config file to read", "file") + configFile := getopt.StringLong("config", 'c', "./config.yml", "Config file to read", "file") pidFile := getopt.StringLong("pid", 'p', "", "Where to write process id", "file") getopt.Parse() diff --git a/config.example.json b/config.example.json deleted file mode 100644 index 47b383c..0000000 --- a/config.example.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "name": "ship-reader-1", - "api": "http://127.0.0.1:8080", - "ship": { - "url": "127.0.0.1:8089", - "irreversible_only": false, - "max_messages_in_flight": 10, - "start_block_num": 4294967295, - "end_block_num": 4294967295 - }, - "telegram": { - "id": "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw", - "channel": -123456789 - }, - "redis": { - "addr": "localhost:6379", - "password": "", - "db": 0, - "prefix": "ship" - } -} diff --git a/config.example.yml b/config.example.yml new file mode 100644 index 0000000..28574f1 --- /dev/null +++ b/config.example.yml @@ -0,0 +1,20 @@ +name: "ship-reader-1" +api: "http://127.0.0.1:8080" +message_codec: "json" + +ship: + url: "127.0.0.1:8089" + irreversible_only: false + max_messages_in_flight: 10 + start_block_num: 4294967295 + end_block_num: 4294967295 + +telegram: + id: "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw" + channel: -123456789 + +redis: + addr: "localhost:6379" + pasword: "" + db: 0 + prefix: "ship" diff --git a/go.mod b/go.mod index b8b25e2..825f566 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/pborman/getopt/v2 v2.1.0 github.com/sirupsen/logrus v1.9.0 github.com/stretchr/testify v1.8.2 + gopkg.in/yaml.v3 v3.0.1 ) require ( @@ -47,7 +48,6 @@ require ( golang.org/x/sys v0.7.0 // indirect golang.org/x/term v0.7.0 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect ) replace github.com/eosswedenorg/thalos/api => ./api