2024-05-05 10:10:04 +02:00
|
|
|
package notification
|
|
|
|
|
|
|
|
import (
|
|
|
|
// External
|
|
|
|
werr "git.gibonuddevalla.se/go/wrappederror"
|
|
|
|
|
|
|
|
// Standard
|
|
|
|
"log/slog"
|
2024-06-28 15:28:52 +02:00
|
|
|
"slices"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
allServices []Service
|
2024-05-05 10:10:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2024-06-02 10:59:06 +02:00
|
|
|
ntfy.SetLogger(logger)
|
2024-05-05 10:10:04 +02:00
|
|
|
return ntfy, nil
|
2024-06-02 10:59:06 +02:00
|
|
|
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
|
2024-05-05 10:10:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, werr.New("Unknown notification service, '%s'", t).WithCode("002-0000")
|
|
|
|
}
|
2024-06-28 15:28:52 +02:00
|
|
|
|
2024-06-28 17:18:08 +02:00
|
|
|
func NewInstance(typ string) Service {
|
|
|
|
switch typ {
|
|
|
|
case "NTFY":
|
|
|
|
return new(NTFY)
|
|
|
|
case "SCRIPT":
|
|
|
|
return new(Script)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-28 15:28:52 +02:00
|
|
|
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
|
|
|
|
}
|