From eda823bec526b08a2d3751c9bc4d29e8564306cf Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Fri, 30 Dec 2022 15:35:51 +0000 Subject: [PATCH] added file notifier type --- config.tmpl | 3 +++ main.go | 6 ++++++ notifiers/file.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 notifiers/file.go diff --git a/config.tmpl b/config.tmpl index b9520d6..2619019 100644 --- a/config.tmpl +++ b/config.tmpl @@ -32,6 +32,9 @@ notifiers: user: "" password: "" to: "to@email.com" + File: + type: "file" + path: /config/notifications.log database: dsn: "/config/watch.db" # for docker usage prune: "@every 1h" \ No newline at end of file diff --git a/main.go b/main.go index f23da46..57e7986 100644 --- a/main.go +++ b/main.go @@ -211,6 +211,12 @@ func (web *Web) initNotifiers() { success = notifier.Open(notifierPath) break } + case "file": + { + notifier = ¬ifiers.FileNotifier{} + success = notifier.Open(notifierPath) + break + } default: { log.Println("Did not recognize notifier type:", notifierType) diff --git a/notifiers/file.go b/notifiers/file.go new file mode 100644 index 0000000..1639e18 --- /dev/null +++ b/notifiers/file.go @@ -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 +}