1
0
Fork 0

Ability to use config file only

This commit is contained in:
Michał Rudowicz 2025-01-02 19:14:21 +01:00
parent 7e9c341270
commit cdcef69618
1 changed files with 17 additions and 14 deletions

View File

@ -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.") 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.") 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.") 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") writeMemoryProfile := flag.Bool("write-memory-profile", false, "Whether application should dump its memory profile every 24 hours. Default: false")
flag.Parse() flag.Parse()
if len(*satelApiAddr) == 0 || len(*satelApiPort) == 0 || len(*chatIdRaw) == 0 { if (len(*satelApiAddr) == 0 || len(*satelApiPort) == 0) && (len(config.SatelAddr) == 0) {
logger.Fatal("Use --satel-addr=ADDR, --satel-port=PORT and --tg-chat-id=CHAT_ID command line flags to continue.") 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 var chatIds []int64
chatIdsStrings := strings.Split(*chatIdRaw, ",")
for _, chatIdStr := range chatIdsStrings { for _, chatIdStr := range chatIdsStrings {
if len(chatIdStr) == 0 {
continue
}
chatId, err := strconv.ParseInt(chatIdStr, 10, 64) chatId, err := strconv.ParseInt(chatIdStr, 10, 64)
if err != nil { if err != nil {
logger.Fatalf("Tried to use a non-int value for one of tg_chatIds: %s. That's bad.", chatIdStr) 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 config.WriteMemoryProfile = *writeMemoryProfile
} }
satelAddr := fmt.Sprintf("%s:%s", *satelApiAddr, *satelApiPort) config.ChatIds = append(config.ChatIds, chatIds...)
config.AllowedTypes = append(config.AllowedTypes, allowedTypes...)
config.SatelAddr = satelAddr config.AllowedIndexes = append(config.AllowedIndexes, allowedIndexes...)
config.ChatIds = chatIds
config.AllowedTypes = allowedTypes
config.AllowedIndexes = allowedIndexes
config.PoolInterval = OwnDuration{*satelPoolInterval}
} }
func MakeConfig(logger *log.Logger) AppConfig { func MakeConfig(logger *log.Logger) AppConfig {
config := loadConfigFromFile(ConfigFilePath, logger) config := loadConfigFromFile(ConfigFilePath, logger)
config.ArmCallbackUrls = []string{}
config.DisarmCallbackUrls = []string{}
config.AlarmCallbackUrls = []string{}
config.WriteMemoryProfile = false config.WriteMemoryProfile = false
if len(os.Getenv("NOTIFY_URL_ARM")) != 0 { if len(os.Getenv("NOTIFY_URL_ARM")) != 0 {