Refactor config loading, message formatting

This commit is contained in:
Timothée Floure 2020-05-03 10:03:36 +02:00
parent 560f0db15b
commit fd35273ed4

16
main.go
View file

@ -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)
}