diff --git a/internal/domain/domain_test.go b/internal/domain/domain_test.go index 78ab25d..69e1375 100644 --- a/internal/domain/domain_test.go +++ b/internal/domain/domain_test.go @@ -94,7 +94,7 @@ func TestDomain_NotifyThreshold(t *testing.T) { TimeLeft: tt.fields.TimeLeft, TriggeredThreshold: tt.fields.TriggeredThreshold, } - tt.wantErr(t, d.NotifyThreshold(context.Background(), integration.Integrations{"telegram": tg})) + tt.wantErr(t, d.NotifyThreshold(context.Background(), integration.Integrations{tg})) assert.Equal(t, tt.wantNotify, gotNotify) }) } @@ -165,7 +165,7 @@ func TestDomain_NotifyStatusChange(t *testing.T) { TimeLeft: tt.fields.TimeLeft, TriggeredThreshold: tt.fields.TriggeredThreshold, } - tt.wantErr(t, d.NotifyStatusChange(context.Background(), integration.Integrations{"telegram": tg})) + tt.wantErr(t, d.NotifyStatusChange(context.Background(), integration.Integrations{tg})) assert.Equal(t, tt.wantNotify, gotNotify) }) } diff --git a/internal/integration/gotify/gotify.go b/internal/integration/gotify/gotify.go index aabd9cf..76678cb 100644 --- a/internal/integration/gotify/gotify.go +++ b/internal/integration/gotify/gotify.go @@ -20,6 +20,8 @@ type Gotify struct { token string } +func (g *Gotify) Name() string { return "Gotify" } + func (g *Gotify) Setup(ctx context.Context, conf *config.Config) error { if conf.GotifyURL == "" { return fmt.Errorf("gotify %w: token", util.ErrNotConfigured) diff --git a/internal/integration/integration.go b/internal/integration/integration.go index 5101d65..ddee054 100644 --- a/internal/integration/integration.go +++ b/internal/integration/integration.go @@ -2,56 +2,12 @@ package integration import ( "context" - "errors" - "log/slog" "gabe565.com/domain-watch/internal/config" - "gabe565.com/domain-watch/internal/integration/gotify" - "gabe565.com/domain-watch/internal/integration/telegram" - "gabe565.com/domain-watch/internal/util" ) type Integration interface { + Name() string Setup(ctx context.Context, conf *config.Config) error Send(ctx context.Context, text string) error } - -type Integrations map[string]Integration - -func Default() Integrations { - return map[string]Integration{ - "telegram": &telegram.Telegram{}, - "gotify": &gotify.Gotify{}, - } -} - -func Setup(ctx context.Context, conf *config.Config) (Integrations, error) { - var configured uint8 - - integrations := Default() - - for _, integration := range integrations { - err := integration.Setup(ctx, conf) - if err != nil { - if errors.Is(err, util.ErrNotConfigured) { - continue - } - return nil, err - } - configured++ - } - - if configured == 0 { - slog.Warn("No integrations were configured") - } - - return integrations, nil -} - -func (i Integrations) Send(ctx context.Context, message string) { - for name, integration := range i { - if err := integration.Send(ctx, message); err != nil { - slog.Error("Failed to send message", "integration", name, "error", err) - } - } -} diff --git a/internal/integration/integrations.go b/internal/integration/integrations.go new file mode 100644 index 0000000..2f5b473 --- /dev/null +++ b/internal/integration/integrations.go @@ -0,0 +1,52 @@ +package integration + +import ( + "context" + "errors" + "log/slog" + "slices" + + "gabe565.com/domain-watch/internal/config" + "gabe565.com/domain-watch/internal/integration/gotify" + "gabe565.com/domain-watch/internal/integration/telegram" + "gabe565.com/domain-watch/internal/util" +) + +type Integrations []Integration + +func All() Integrations { + return Integrations{ + &telegram.Telegram{}, + &gotify.Gotify{}, + } +} + +func Setup(ctx context.Context, conf *config.Config) (Integrations, error) { + all := All() + integrations := make(Integrations, 0, len(all)) + for _, integration := range all { + err := integration.Setup(ctx, conf) + if err != nil { + if errors.Is(err, util.ErrNotConfigured) { + continue + } + return nil, err + } + integrations = append(integrations, integration) + } + + if len(integrations) == 0 { + slog.Warn("No integrations were configured") + } + + integrations = slices.Clip(integrations) + return integrations, nil +} + +func (i Integrations) Send(ctx context.Context, message string) { + for _, integration := range i { + if err := integration.Send(ctx, message); err != nil { + slog.Error("Failed to send message", "integration", integration.Name(), "error", err) + } + } +} diff --git a/internal/integration/telegram/telegram.go b/internal/integration/telegram/telegram.go index ef9d21d..98eb41a 100644 --- a/internal/integration/telegram/telegram.go +++ b/internal/integration/telegram/telegram.go @@ -15,6 +15,8 @@ type Telegram struct { Bot *bot.Bot } +func (t *Telegram) Name() string { return "Telegram" } + func (t *Telegram) Setup(ctx context.Context, conf *config.Config) error { if t.ChatID = conf.TelegramChat; t.ChatID == 0 { return fmt.Errorf("telegram %w: chat ID", util.ErrNotConfigured)