smon/notification/factory.go
2024-07-04 19:21:02 +02:00

83 lines
1.5 KiB
Go

package notification
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
// Standard
"log/slog"
"slices"
"strings"
)
var (
allServices []Service
)
func ServiceFactory(t string, config []byte, prio int, ackURL string, logger *slog.Logger) (Service, error) {
switch t {
case "NTFY":
ntfy, err := NewNTFY(config, prio, ackURL)
if err != nil {
err = werr.Wrap(err).WithData(config)
return nil, err
}
ntfy.SetLogger(logger)
return ntfy, nil
case "PUSHOVER":
pushover, err := NewPushover(config, prio, ackURL)
if err != nil {
err = werr.Wrap(err).WithData(config)
return nil, err
}
pushover.SetLogger(logger)
return pushover, nil
case "SCRIPT":
script, err := NewScript(config, prio, ackURL)
if err != nil {
err = werr.Wrap(err).WithData(config)
return nil, err
}
script.SetLogger(logger)
return script, nil
}
return nil, werr.New("Unknown notification service, '%s'", t).WithCode("002-0000")
}
func NewInstance(typ string) Service {
switch typ {
case "NTFY":
return new(NTFY)
case "PUSHOVER":
return new(Pushover)
case "SCRIPT":
return new(Script)
}
return nil
}
func GetInstance(typ string) Service {
for _, svc := range allServices {
if strings.ToLower(svc.GetType()) == strings.ToLower(typ) {
return svc
}
}
return nil
}
func AvailableServices() []Service {
slices.SortFunc(allServices, func(a, b Service) int {
if a.GetType() < b.GetType() {
return -1
}
if a.GetType() > b.GetType() {
return 1
}
return 0
})
return allServices
}