Skip to content

Commit

Permalink
refactor(integration): Clean up integration init
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Nov 16, 2024
1 parent 6cd6a81 commit 2d8781e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 47 deletions.
4 changes: 2 additions & 2 deletions internal/domain/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Expand Down Expand Up @@ -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)
})
}
Expand Down
2 changes: 2 additions & 0 deletions internal/integration/gotify/gotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
46 changes: 1 addition & 45 deletions internal/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
52 changes: 52 additions & 0 deletions internal/integration/integrations.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
2 changes: 2 additions & 0 deletions internal/integration/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2d8781e

Please sign in to comment.