From bcf42571e54a84405be7d12e93f8990f2b9ee5da Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Wed, 28 Dec 2022 12:15:09 +0000 Subject: [PATCH] refactor of notifiers, it's now a list in the config --- main.go | 58 +++++++++++++++++++++++++++++-------------- notifiers/discord.go | 22 ++++++++++------ notifiers/email.go | 33 ++++++++++++++---------- notifiers/notifier.go | 2 +- notifiers/shoutrrr.go | 10 +++++--- notifiers/telegram.go | 27 +++++++++++++++----- todo.md | 3 ++- 7 files changed, 103 insertions(+), 52 deletions(-) diff --git a/main.go b/main.go index 0582dfc..cfff320 100644 --- a/main.go +++ b/main.go @@ -162,28 +162,48 @@ func (web *Web) initCronJobs() { func (web *Web) initNotifiers() { web.notifiers = make(map[string]notifiers.Notifier, 5) - if viper.IsSet("notifiers.telegram") { - telegramBot := notifiers.TelegramNotifier{} - if telegramBot.Open() { - web.notifiers["Telegram"] = &telegramBot - } + if !viper.IsSet("notifiers") { + log.Panicln("No notifiers set!") + return } - if viper.IsSet("notifiers.discord") { - discordBot := notifiers.DiscordNotifier{} - if discordBot.Open() { - web.notifiers["Discord"] = &discordBot + notifiersMap := viper.GetStringMap("notifiers") + for name := range notifiersMap { + notifierPath := fmt.Sprintf("notifiers.%s", name) + notifierMap := viper.GetStringMapString(notifierPath) + + notifierType, exists := notifierMap["type"] + if !exists { + log.Printf("No 'type' for '%s' notifier!", name) + continue } - } - if viper.IsSet("notifiers.email") { - emailBot := notifiers.EmailNotifier{} - if emailBot.Open() { - web.notifiers["Email"] = &emailBot + success := false + var notifier notifiers.Notifier + switch notifierType { + case "telegram": + { + notifier = ¬ifiers.TelegramNotifier{} + success = notifier.Open(notifierPath) + break + } + case "discord": + { + notifier = ¬ifiers.DiscordNotifier{} + success = notifier.Open(notifierPath) + break + } + case "email": + { + notifier = ¬ifiers.EmailNotifier{} + success = notifier.Open(notifierPath) + } + case "shoutrrr": + { + notifier = ¬ifiers.ShoutrrrNotifier{} + success = notifier.Open(notifierPath) + } } - } - if viper.IsSet("notifiers.shoutrrr") { - shoutrrrBot := notifiers.ShoutrrrNotifier{} - if shoutrrrBot.Open() { - web.notifiers["Shoutrrr"] = &shoutrrrBot + if success { + web.notifiers[name] = notifier } } } diff --git a/notifiers/discord.go b/notifiers/discord.go index 5e620da..77e3794 100644 --- a/notifiers/discord.go +++ b/notifiers/discord.go @@ -1,6 +1,7 @@ package notifiers import ( + "fmt" "log" "github.com/bwmarrin/discordgo" @@ -18,8 +19,10 @@ type DiscordNotifier struct { Debug bool } -func (discord *DiscordNotifier) Open() bool { - if !viper.IsSet("notifiers.discord.userID") && !viper.IsSet("notifiers.discord.server") { +func (discord *DiscordNotifier) Open(configPath string) bool { + userIDPath := fmt.Sprintf("%s.userID", configPath) + serverPath := fmt.Sprintf("%s.server", configPath) + if !viper.IsSet(userIDPath) && !viper.IsSet(serverPath) { log.Println("Net either 'serverID' or 'userID' for Discord") return false } @@ -28,8 +31,8 @@ func (discord *DiscordNotifier) Open() bool { log.Println("Could not start Discord notifier:\n", err) return false } - if viper.IsSet("notifiers.discord.userID") { - discord.UserID = viper.GetString("notifiers.discord.userID") + if viper.IsSet(userIDPath) { + discord.UserID = viper.GetString(userIDPath) channel, err := bot.UserChannelCreate(discord.UserID) if err != nil { log.Println("Could not connect to user channel:", discord.UserID, err) @@ -38,9 +41,11 @@ func (discord *DiscordNotifier) Open() bool { discord.UserChannel = channel log.Println("Authorized discord bot for:", channel.Recipients) } - if viper.IsSet("notifiers.discord.server") { - discord.ServerID = viper.GetString("notifiers.discord.server.ID") - discord.ChannelID = viper.GetString("notifiers.discord.server.channel") + if viper.IsSet(serverPath) { + serverIDPath := fmt.Sprintf("%s.server.ID", configPath) + serverChannelPath := fmt.Sprintf("%s.server.channel", configPath) + discord.ServerID = viper.GetString(serverIDPath) + discord.ChannelID = viper.GetString(serverChannelPath) channels, err := bot.GuildChannels(discord.ServerID) if err != nil { log.Println("Could not connect to server channel:", discord.ServerID, err) @@ -61,7 +66,8 @@ func (discord *DiscordNotifier) Open() bool { } log.Println("Authorized discord bot for:", discord.ServerChannel.Name) } - discord.Debug = viper.GetBool("notifiers.discord.debug") + debugPath := fmt.Sprintf("%s.debug", configPath) + discord.Debug = viper.GetBool(debugPath) if discord.Debug { bot.LogLevel = discordgo.LogDebug } else { diff --git a/notifiers/email.go b/notifiers/email.go index f153c82..a551115 100644 --- a/notifiers/email.go +++ b/notifiers/email.go @@ -1,6 +1,7 @@ package notifiers import ( + "fmt" "log" "github.com/spf13/viper" @@ -17,39 +18,45 @@ type EmailNotifier struct { Debug bool } -func (email *EmailNotifier) Open() bool { - if viper.IsSet("notifiers.email.server") { - email.Server = viper.GetString("notifiers.email.server") +func (email *EmailNotifier) Open(configPath string) bool { + serverPath := fmt.Sprintf("%s.server", configPath) + if viper.IsSet(serverPath) { + email.Server = viper.GetString(serverPath) } else { log.Println("Need 'server' var for email notifier") return false } - if viper.IsSet("notifiers.email.port") { - email.Port = viper.GetInt("notifiers.email.port") + portPath := fmt.Sprintf("%s.port", configPath) + if viper.IsSet(portPath) { + email.Port = viper.GetInt(portPath) } else { log.Println("Need 'port' var for email notifier") return false } - if viper.IsSet("notifiers.email.user") { - email.User = viper.GetString("notifiers.email.user") + userPath := fmt.Sprintf("%s.user", configPath) + if viper.IsSet(userPath) { + email.User = viper.GetString(userPath) } else { log.Println("Need 'user' var for email notifier") return false } - if viper.IsSet("notifiers.email.from") { - email.From = viper.GetString("notifiers.email.from") + fromPath := fmt.Sprintf("%s.from", configPath) + if viper.IsSet(fromPath) { + email.From = viper.GetString(fromPath) } else { log.Println("Need 'from' var for email notifier") return false } - if viper.IsSet("notifiers.email.to") { - email.To = viper.GetString("notifiers.email.to") + toPath := fmt.Sprintf("%s.to", configPath) + if viper.IsSet(toPath) { + email.To = viper.GetString(toPath) } else { log.Println("Need 'to' var for email notifier") return false } - if viper.IsSet("notifiers.email.password") { - email.Password = viper.GetString("notifiers.email.password") + passwordPath := fmt.Sprintf("%s.password", configPath) + if viper.IsSet(passwordPath) { + email.Password = viper.GetString(passwordPath) } else { log.Println("Need 'password' var for email notifier") return false diff --git a/notifiers/notifier.go b/notifiers/notifier.go index b68fbc9..5399063 100644 --- a/notifiers/notifier.go +++ b/notifiers/notifier.go @@ -1,7 +1,7 @@ package notifiers type Notifier interface { - Open() bool + Open(configPath string) bool Message(message string) bool Close() bool } diff --git a/notifiers/shoutrrr.go b/notifiers/shoutrrr.go index 735f2d6..36217ff 100644 --- a/notifiers/shoutrrr.go +++ b/notifiers/shoutrrr.go @@ -1,6 +1,7 @@ package notifiers import ( + "fmt" "log" "github.com/containrrr/shoutrrr" @@ -12,13 +13,14 @@ type ShoutrrrNotifier struct { URLs []string } -func (shoutr *ShoutrrrNotifier) Open() bool { - log.Println("Shoutrrr version:", shoutrrr.Version()) - if !viper.IsSet("notifiers.shoutrrr.urls") { +func (shoutr *ShoutrrrNotifier) Open(configPath string) bool { + urlsPath := fmt.Sprintf("%s.urls", configPath) + if !viper.IsSet(urlsPath) { log.Println("Need 'urls' for Shoutrrr") return false } - shoutr.URLs = viper.GetStringSlice("notifiers.shoutrrr.urls") + shoutr.URLs = viper.GetStringSlice(urlsPath) + log.Println("Shoutrrr version:", shoutrrr.Version()) return true } diff --git a/notifiers/telegram.go b/notifiers/telegram.go index c1003de..ddf02ba 100644 --- a/notifiers/telegram.go +++ b/notifiers/telegram.go @@ -1,6 +1,7 @@ package notifiers import ( + "fmt" "log" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" @@ -8,17 +9,31 @@ import ( ) type TelegramNotifier struct { - Bot *tgbotapi.BotAPI - Token string - Debug bool + Bot *tgbotapi.BotAPI + Token string + ChatID int64 + Debug bool } -func (telegram *TelegramNotifier) Open() bool { - bot, err := tgbotapi.NewBotAPI(viper.GetString("notifiers.telegram.token")) +func (telegram *TelegramNotifier) Open(configPath string) bool { + tokenPath := fmt.Sprintf("%s.token", configPath) + if !viper.IsSet(tokenPath) { + log.Println("Telegram needs 'token' value set") + return false + } + telegram.Token = viper.GetString(tokenPath) + bot, err := tgbotapi.NewBotAPI(telegram.Token) if err != nil { log.Println("Could not start Telegram notifier:\n", err) return false } + + chatIDPath := fmt.Sprintf("%s.chat", configPath) + if !viper.IsSet(chatIDPath) { + log.Panicln("Telegram needs 'chat' ID value") + return false + } + telegram.ChatID = viper.GetInt64(chatIDPath) telegram.Bot = bot bot.Debug = viper.GetBool("notifiers.telegram.debug") log.Printf("Authorized telegram bot: %s", bot.Self.UserName) @@ -26,7 +41,7 @@ func (telegram *TelegramNotifier) Open() bool { } func (telegram *TelegramNotifier) Message(message string) bool { - msg := tgbotapi.NewMessage(viper.GetInt64("notifiers.telegram.chat"), message) + msg := tgbotapi.NewMessage(telegram.ChatID, message) _, err := telegram.Bot.Send(msg) if err != nil { log.Println("Could not send Telegram message:\n", err) diff --git a/todo.md b/todo.md index efab87a..8510997 100644 --- a/todo.md +++ b/todo.md @@ -4,4 +4,5 @@ - comments - run/fix staticcheck - add browserless support ? -- refactor notifiers to be a list, with 'type' value? \ No newline at end of file +- ~~refactor notifiers to be a list, with 'type' value?~~ + - readme \ No newline at end of file