added file notifier type

This commit is contained in:
BroodjeAap 2022-12-30 15:35:51 +00:00
parent 5f1dd5a91a
commit eda823bec5
3 changed files with 59 additions and 0 deletions

View file

@ -32,6 +32,9 @@ notifiers:
user: "<user>"
password: "<password>"
to: "to@email.com"
File:
type: "file"
path: /config/notifications.log
database:
dsn: "/config/watch.db" # for docker usage
prune: "@every 1h"

View file

@ -211,6 +211,12 @@ func (web *Web) initNotifiers() {
success = notifier.Open(notifierPath)
break
}
case "file":
{
notifier = &notifiers.FileNotifier{}
success = notifier.Open(notifierPath)
break
}
default:
{
log.Println("Did not recognize notifier type:", notifierType)

50
notifiers/file.go Normal file
View file

@ -0,0 +1,50 @@
package notifiers
import (
"fmt"
"log"
"os"
"github.com/spf13/viper"
)
type FileNotifier struct {
Path string
}
func (file *FileNotifier) Open(configPath string) bool {
pathPath := fmt.Sprintf("%s.path", configPath)
if !viper.IsSet(pathPath) {
log.Println("Need 'path' for FileNotifier")
return false
}
file.Path = viper.GetString(pathPath)
f, err := os.OpenFile(file.Path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Println("Could not open:", file.Path)
return false
}
f.Close()
log.Println("File notifier path: ", file.Path)
return true
}
func (file *FileNotifier) Message(message string) bool {
f, err := os.OpenFile(file.Path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Println("Could not open:", file.Path)
return false
}
defer f.Close()
_, err = f.WriteString(message + "\n")
if err != nil {
log.Println("Could not write notification to:", file.Path)
return false
}
return true
}
func (file *FileNotifier) Close() bool {
return true
}