From fcd2cda803062da7404cf0c8c475c6db763d0ca2 Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Sun, 3 Nov 2024 19:49:10 -0500 Subject: [PATCH] Use mapstructure for notify config --- go.mod | 1 + go.sum | 2 ++ pkg/config/config.go | 7 +++++-- pkg/notify/notify.go | 26 +++++++++++++++++++------- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 5cb2a64..3fe31ca 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/go-chi/httprate v0.9.0 github.com/go-chi/jwtauth/v5 v5.3.1 github.com/go-chi/render v1.0.3 + github.com/go-viper/mapstructure/v2 v2.2.1 github.com/golang-migrate/migrate/v4 v4.17.1 github.com/google/uuid v1.6.0 github.com/gorilla/websocket v1.5.3 diff --git a/go.sum b/go.sum index d73a798..3ca4872 100644 --- a/go.sum +++ b/go.sum @@ -44,6 +44,8 @@ github.com/go-chi/jwtauth/v5 v5.3.1 h1:1ePWrjVctvp1tyBq5b/2ER8Th/+RbYc7x4qNsc5rh github.com/go-chi/jwtauth/v5 v5.3.1/go.mod h1:6Fl2RRmWXs3tJYE1IQGX81FsPoGqDwq9c15j52R5q80= github.com/go-chi/render v1.0.3 h1:AsXqd2a1/INaIfUSKq3G5uA8weYx20FOsM7uSoCyyt4= github.com/go-chi/render v1.0.3/go.mod h1:/gr3hVkmYR0YlEy3LxCuVRFzEu9Ruok+gFqbIofjao0= +github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss= +github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= diff --git a/pkg/config/config.go b/pkg/config/config.go index 2003723..757842b 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -70,8 +70,11 @@ type NotifyService struct { } func (n *NotifyService) GetS(k, defaultVal string) string { - if v, has := n.Config[k].(string); has { - return v + if v, has := n.Config[k]; has { + if v, isString := v.(string); isString { + return v + } + log.Error().Str("configKey", k).Str("provider", n.Provider).Str("default", defaultVal).Msg("notify config value is not a string! using default") } return defaultVal diff --git a/pkg/notify/notify.go b/pkg/notify/notify.go index 82bd034..cf9716e 100644 --- a/pkg/notify/notify.go +++ b/pkg/notify/notify.go @@ -7,6 +7,7 @@ import ( "dynatron.me/x/stillbox/pkg/config" + "github.com/go-viper/mapstructure/v2" "github.com/nikoksr/notify" "github.com/nikoksr/notify/service/http" ) @@ -19,9 +20,7 @@ type notifier struct { *notify.Notify } -func (n *notifier) buildSlackWebhookPayload(cfg config.NotifyService) func(string, string) any { - icon := cfg.GetS("icon", "🚨") - url := cfg.GetS("messageURL", "") +func (n *notifier) buildSlackWebhookPayload(cfg *slackWebhookConfig) func(string, string) any { type Attachment struct { Title string `json:"title"` @@ -42,27 +41,40 @@ func (n *notifier) buildSlackWebhookPayload(cfg config.NotifyService) func(strin { Title: subject, Text: message, - TitleLink: url, + TitleLink: cfg.MessageURL, Timestamp: time.Now().Unix(), }, }, - IconEmoji: icon, + IconEmoji: cfg.Icon, } return m } } +type slackWebhookConfig struct { + WebhookURL string `mapstructure:"webhookURL"` + Icon string `mapstructure:"icon"` + MessageURL string `mapstructure:"messageURL"` +} + func (n *notifier) addService(cfg config.NotifyService) error { switch cfg.Provider { case "slackwebhook": + swc := &slackWebhookConfig{ + Icon: "🚨", + } + err := mapstructure.Decode(cfg.Config, &swc) + if err != nil { + return err + } hs := http.New() hs.AddReceivers(&http.Webhook{ ContentType: "application/json", Header: make(stdhttp.Header), Method: stdhttp.MethodPost, - URL: cfg.GetS("webhookURL", ""), - BuildPayload: n.buildSlackWebhookPayload(cfg), + URL: swc.WebhookURL, + BuildPayload: n.buildSlackWebhookPayload(swc), }) n.UseServices(hs) default: