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 ( import (
"os" "os"
"flag"
"fmt" "fmt"
"log" "log"
"io/ioutil" "flag"
"net/http" "net/http"
"encoding/json" "encoding/json"
"github.com/prometheus/alertmanager/template"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/matrix-org/gomatrix" "github.com/matrix-org/gomatrix"
"github.com/prometheus/alertmanager/template"
) )
var logger *log.Logger var logger *log.Logger
var config Configuration
type Configuration struct { type Configuration struct {
Homeserver string Homeserver string
@ -27,7 +27,7 @@ type Configuration struct {
} }
func generateMatrixMessageBody(alert template.Alert) string { 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 { func getMatrixClient(homeserver string, user string, token string, targetRoomID string) *gomatrix.Client {
@ -99,13 +99,7 @@ func main() {
flag.Parse() flag.Parse()
logger.Printf("Reading configuration from %v.", *configPath) logger.Printf("Reading configuration from %v.", *configPath)
raw, err := ioutil.ReadFile(*configPath) md, err := toml.DecodeFile(*configPath, &config)
if err != nil {
logger.Fatalf("Could not read configuration file (%v): %v", *configPath, err)
}
var config Configuration
md, err := toml.Decode(string(raw), &config)
if err != nil { if err != nil {
logger.Fatalf("Could not parse configuration file (%v): %v", *configPath, err) logger.Fatalf("Could not parse configuration file (%v): %v", *configPath, err)
} }