From cdcef69618c5ad75f52f9af7a0857cd323eb017e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Rudowicz?= Date: Thu, 2 Jan 2025 19:14:21 +0100 Subject: [PATCH] Ability to use config file only --- config.go | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/config.go b/config.go index 56c6270..f8fb846 100644 --- a/config.go +++ b/config.go @@ -102,16 +102,26 @@ func getCmdLineParams(config *AppConfig, logger *log.Logger) { chatIdRaw := flag.String("tg-chat-id", "", "Telegram Chat ID where to send updates. Use \",\" to specify multiple IDs.") allowedTypesRaw := flag.String("allowed-types", "", "Satel change types that are allowed. All other types will be discarded. By default all are allowed. Use \",\" to specify multiple types.") allowedIndexesRaw := flag.String("allowed-indexes", "", "Satel indexes (zones?) that are allowed. All other indexes will be discarded. By default all are allowed. Use \",\" to specify multiple indexes.") - satelPoolInterval := flag.Duration("pool-interval", 5*time.Second, "How often should the SATEL device be pooled for changes? Default: 5 seconds.") writeMemoryProfile := flag.Bool("write-memory-profile", false, "Whether application should dump its memory profile every 24 hours. Default: false") flag.Parse() - if len(*satelApiAddr) == 0 || len(*satelApiPort) == 0 || len(*chatIdRaw) == 0 { - logger.Fatal("Use --satel-addr=ADDR, --satel-port=PORT and --tg-chat-id=CHAT_ID command line flags to continue.") + if (len(*satelApiAddr) == 0 || len(*satelApiPort) == 0) && (len(config.SatelAddr) == 0) { + logger.Fatal("Satel address not provided. Use --satel-addr=ADDR and --satel-port=PORT or satel-addr field in the configuration file to solve this.") + } + if len(*satelApiAddr) != 0 && len(*satelApiPort) != 0 { + if len(config.SatelAddr) != 0 { + logger.Print("Overriding satel-addr from config with values from command line. ", config.SatelAddr, " will be replaced with ", *satelApiAddr, ":", *satelApiPort) + } + satelAddr := fmt.Sprintf("%s:%s", *satelApiAddr, *satelApiPort) + + config.SatelAddr = satelAddr } - chatIdsStrings := strings.Split(*chatIdRaw, ",") var chatIds []int64 + chatIdsStrings := strings.Split(*chatIdRaw, ",") for _, chatIdStr := range chatIdsStrings { + if len(chatIdStr) == 0 { + continue + } chatId, err := strconv.ParseInt(chatIdStr, 10, 64) if err != nil { logger.Fatalf("Tried to use a non-int value for one of tg_chatIds: %s. That's bad.", chatIdStr) @@ -146,20 +156,13 @@ func getCmdLineParams(config *AppConfig, logger *log.Logger) { config.WriteMemoryProfile = *writeMemoryProfile } - satelAddr := fmt.Sprintf("%s:%s", *satelApiAddr, *satelApiPort) - - config.SatelAddr = satelAddr - config.ChatIds = chatIds - config.AllowedTypes = allowedTypes - config.AllowedIndexes = allowedIndexes - config.PoolInterval = OwnDuration{*satelPoolInterval} + config.ChatIds = append(config.ChatIds, chatIds...) + config.AllowedTypes = append(config.AllowedTypes, allowedTypes...) + config.AllowedIndexes = append(config.AllowedIndexes, allowedIndexes...) } func MakeConfig(logger *log.Logger) AppConfig { config := loadConfigFromFile(ConfigFilePath, logger) - config.ArmCallbackUrls = []string{} - config.DisarmCallbackUrls = []string{} - config.AlarmCallbackUrls = []string{} config.WriteMemoryProfile = false if len(os.Getenv("NOTIFY_URL_ARM")) != 0 {