From fd35273ed4d59c0f8556b722565546e72883e58b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Sun, 3 May 2020 10:03:36 +0200 Subject: [PATCH] Refactor config loading, message formatting --- main.go | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index 618a1c0..a3c9af0 100644 --- a/main.go +++ b/main.go @@ -2,18 +2,18 @@ package main import ( "os" - "flag" "fmt" "log" - "io/ioutil" + "flag" "net/http" "encoding/json" - "github.com/prometheus/alertmanager/template" "github.com/BurntSushi/toml" "github.com/matrix-org/gomatrix" + "github.com/prometheus/alertmanager/template" ) var logger *log.Logger +var config Configuration type Configuration struct { Homeserver string @@ -27,7 +27,7 @@ type Configuration struct { } func generateMatrixMessageBody(alert template.Alert) string { - return alert.Status + " // " + alert.Annotations["summary"] + return fmt.Sprintf("**%v** %v.", alert.Status, alert.Annotations["summary"]) } func getMatrixClient(homeserver string, user string, token string, targetRoomID string) *gomatrix.Client { @@ -99,13 +99,7 @@ func main() { flag.Parse() logger.Printf("Reading configuration from %v.", *configPath) - raw, err := ioutil.ReadFile(*configPath) - if err != nil { - logger.Fatalf("Could not read configuration file (%v): %v", *configPath, err) - } - - var config Configuration - md, err := toml.Decode(string(raw), &config) + md, err := toml.DecodeFile(*configPath, &config) if err != nil { logger.Fatalf("Could not parse configuration file (%v): %v", *configPath, err) }