-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproviders.go
121 lines (100 loc) · 4.1 KB
/
providers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package providers
import (
"github.com/acarl005/stripansi"
"github.com/pkg/errors"
"github.com/projectdiscovery/gologger"
"github.com/akityo/notify/pkg/providers/custom"
"github.com/akityo/notify/pkg/providers/discord"
"github.com/akityo/notify/pkg/providers/pushover"
"github.com/akityo/notify/pkg/providers/slack"
"github.com/akityo/notify/pkg/providers/smtp"
"github.com/akityo/notify/pkg/providers/teams"
"github.com/akityo/notify/pkg/providers/telegram"
"github.com/akityo/notify/pkg/types"
"github.com/akityo/notify/pkg/utils"
"go.uber.org/multierr"
)
// ProviderOptions is configuration for notify providers
type ProviderOptions struct {
Slack []*slack.Options `yaml:"slack,omitempty"`
Discord []*discord.Options `yaml:"discord,omitempty"`
Pushover []*pushover.Options `yaml:"pushover,omitempty"`
SMTP []*smtp.Options `yaml:"smtp,omitempty"`
Teams []*teams.Options `yaml:"teams,omitempty"`
Telegram []*telegram.Options `yaml:"telegram,omitempty"`
Custom []*custom.Options `yaml:"custom,omitempty"`
}
// Provider is an interface implemented by providers
type Provider interface {
Send(message, CliFormat string) error
}
type Client struct {
providers []Provider
providerOptions *ProviderOptions
options *types.Options
}
func New(providerOptions *ProviderOptions, options *types.Options) (*Client, error) {
client := &Client{providerOptions: providerOptions, options: options}
if providerOptions.Slack != nil && (len(options.Providers) == 0 || utils.Contains(options.Providers, "slack")) {
provider, err := slack.New(providerOptions.Slack, options.IDs)
if err != nil {
return nil, errors.Wrap(err, "could not create slack provider client")
}
client.providers = append(client.providers, provider)
}
if providerOptions.Discord != nil && (len(options.Providers) == 0 || utils.Contains(options.Providers, "discord")) {
provider, err := discord.New(providerOptions.Discord, options.IDs)
if err != nil {
return nil, errors.Wrap(err, "could not create discord provider client")
}
client.providers = append(client.providers, provider)
}
if providerOptions.Pushover != nil && (len(options.Providers) == 0 || utils.Contains(options.Providers, "pushover")) {
provider, err := pushover.New(providerOptions.Pushover, options.IDs)
if err != nil {
return nil, errors.Wrap(err, "could not create pushover provider client")
}
client.providers = append(client.providers, provider)
}
if providerOptions.SMTP != nil && (len(options.Providers) == 0 || utils.Contains(options.Providers, "smtp")) {
provider, err := smtp.New(providerOptions.SMTP, options.IDs)
if err != nil {
return nil, errors.Wrap(err, "could not create smtp provider client")
}
client.providers = append(client.providers, provider)
}
if providerOptions.Teams != nil && (len(options.Providers) == 0 || utils.Contains(options.Providers, "teams")) {
provider, err := teams.New(providerOptions.Teams, options.IDs)
if err != nil {
return nil, errors.Wrap(err, "could not create teams provider client")
}
client.providers = append(client.providers, provider)
}
if providerOptions.Telegram != nil && (len(options.Providers) == 0 || utils.Contains(options.Providers, "telegram")) {
provider, err := telegram.New(providerOptions.Telegram, options.IDs)
if err != nil {
return nil, errors.Wrap(err, "could not create telegram provider client")
}
client.providers = append(client.providers, provider)
}
if providerOptions.Custom != nil && (len(options.Providers) == 0 || utils.Contains(options.Providers, "custom")) {
provider, err := custom.New(providerOptions.Custom, options.IDs)
if err != nil {
return nil, errors.Wrap(err, "could not create custom provider client")
}
client.providers = append(client.providers, provider)
}
return client, nil
}
func (p *Client) Send(message string) error {
// strip unsupported color control chars
message = stripansi.Strip(message)
for _, v := range p.providers {
if err := v.Send(message, p.options.MessageFormat); err != nil {
for _, v := range multierr.Errors(err) {
gologger.Error().Msgf("%s", v)
}
}
}
return nil
}