added file notifier type
This commit is contained in:
parent
5f1dd5a91a
commit
eda823bec5
3 changed files with 59 additions and 0 deletions
|
@ -32,6 +32,9 @@ notifiers:
|
||||||
user: "<user>"
|
user: "<user>"
|
||||||
password: "<password>"
|
password: "<password>"
|
||||||
to: "to@email.com"
|
to: "to@email.com"
|
||||||
|
File:
|
||||||
|
type: "file"
|
||||||
|
path: /config/notifications.log
|
||||||
database:
|
database:
|
||||||
dsn: "/config/watch.db" # for docker usage
|
dsn: "/config/watch.db" # for docker usage
|
||||||
prune: "@every 1h"
|
prune: "@every 1h"
|
6
main.go
6
main.go
|
@ -211,6 +211,12 @@ func (web *Web) initNotifiers() {
|
||||||
success = notifier.Open(notifierPath)
|
success = notifier.Open(notifierPath)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
case "file":
|
||||||
|
{
|
||||||
|
notifier = ¬ifiers.FileNotifier{}
|
||||||
|
success = notifier.Open(notifierPath)
|
||||||
|
break
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
log.Println("Did not recognize notifier type:", notifierType)
|
log.Println("Did not recognize notifier type:", notifierType)
|
||||||
|
|
50
notifiers/file.go
Normal file
50
notifiers/file.go
Normal 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
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue