From 2c3286206ef4507830fc88a4cd87eca51110a2b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Rudowicz?= Date: Sat, 23 Mar 2024 10:40:20 +0100 Subject: [PATCH] Introduction of AppConfig --- config.go | 15 +++++++++++++++ main.go | 14 +++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 config.go diff --git a/config.go b/config.go new file mode 100644 index 0000000..2a07197 --- /dev/null +++ b/config.go @@ -0,0 +1,15 @@ +package main + +import ( + "time" + + "git.sr.ht/~michalr/go-satel" +) + +type AppConfig struct { + satelAddr string + chatIds []int64 + allowedTypes []satel.ChangeType + allowedIndexes []int + poolInterval time.Duration +} diff --git a/main.go b/main.go index 80a6213..07943eb 100644 --- a/main.go +++ b/main.go @@ -34,7 +34,7 @@ func (self RealSleeper) Sleep(ch chan<- interface{}) { }() } -func getCmdLineParams(logger *log.Logger) (string, []int64, []satel.ChangeType, []int, time.Duration) { +func getCmdLineParams(logger *log.Logger) AppConfig { satelApiAddr := flag.String("satel-addr", "", "Address that should be used to connect to the SATEL device") satelApiPort := flag.String("satel-port", "7094", "Port that should be used to connect to the SATEL device") chatIdRaw := flag.String("tg-chat-id", "", "Telegram Chat ID where to send updates. Use \",\" to specify multiple IDs.") @@ -81,7 +81,7 @@ func getCmdLineParams(logger *log.Logger) (string, []int64, []satel.ChangeType, } satelAddr := fmt.Sprintf("%s:%s", *satelApiAddr, *satelApiPort) - return satelAddr, chatIds, allowedTypes, allowedIndexes, *satelPoolInterval + return AppConfig{satelAddr, chatIds, allowedTypes, allowedIndexes, *satelPoolInterval} } func makeSatel(satelAddr string, poolInterval time.Duration) *satel.Satel { @@ -110,10 +110,10 @@ func main() { ) stopRequested.Store(false) - satelAddr, chatIds, allowedTypes, allowedIndexes, poolInterval := getCmdLineParams(logger) + cfg := getCmdLineParams(logger) - s := makeSatel(satelAddr, poolInterval) - logger.Printf("Connected to Satel: %s", satelAddr) + s := makeSatel(cfg.satelAddr, cfg.poolInterval) + logger.Printf("Connected to Satel: %s", cfg.satelAddr) bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN")) if err != nil { @@ -121,7 +121,7 @@ func main() { } logger.Print("Created Telegram Bot API client") - tgSender := TgSender{bot, s, log.New(os.Stderr, "TgFormatter", log.Lmicroseconds), chatIds} + tgSender := TgSender{bot, s, log.New(os.Stderr, "TgFormatter", log.Lmicroseconds), cfg.chatIds} tpl := template.Must(template.New("TelegramMessage").Parse(TelegramMessageTemplate)) @@ -138,7 +138,7 @@ func main() { for stopRequested.Load() == false { for e := range FilterByTypeOrIndex( FilterByLastSeen(s.Events, &wg, &dataStore, log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)), - &wg, allowedTypes, allowedIndexes) { + &wg, cfg.allowedTypes, cfg.allowedIndexes) { logger.Print("Received change from SATEL: ", e) tgEvents <- GenericMessage{e.BasicEvents} }