-
Notifications
You must be signed in to change notification settings - Fork 222
/
notify.go
98 lines (80 loc) · 2.87 KB
/
notify.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
package notify
import (
context "context"
"errors"
)
// ErrSendNotification signals that the notifier failed to send a notification.
var ErrSendNotification = errors.New("send notification")
// Notifier defines the behavior for notification services.
//
// The Send function simply sends a subject and a message string to the internal destination Notifier.
//
// E.g. for telegram.Telegram it sends the message to the specified group chat.
type Notifier interface {
Send(context.Context, string, string) error
}
// Compile-time check to ensure Notify implements Notifier.
var _ Notifier = (*Notify)(nil)
// Notify is the central struct for managing notification services and sending messages to them.
type Notify struct {
Disabled bool
notifiers []Notifier
}
// Option is a function that can be used to configure a Notify instance. It is used by the WithOptions and
// NewWithOptions functions. It's a function because it's a bit more flexible than using a struct. The only required
// parameter is the Notify instance itself.
type Option func(*Notify)
// Enable is an Option function that enables the Notify instance. This is the default behavior.
func Enable(n *Notify) {
if n != nil {
n.Disabled = false
}
}
// Disable is an Option function that disables the Notify instance. It is enabled by default.
func Disable(n *Notify) {
if n != nil {
n.Disabled = true
}
}
// WithOptions applies the given options to the Notify instance. If no options are provided, it returns the Notify
// instance unchanged.
func (n *Notify) WithOptions(options ...Option) *Notify {
if options == nil {
return n
}
for _, option := range options {
if option != nil {
option(n)
}
}
return n
}
// NewWithOptions returns a new instance of Notify with the given options. If no options are provided, it returns a new
// Notify instance with default options. By default, the Notify instance is enabled.
func NewWithOptions(options ...Option) *Notify {
n := &Notify{
Disabled: false, // Enabled by default.
notifiers: make([]Notifier, 0), // Avoid nil list.
}
return n.WithOptions(options...)
}
// New returns a new instance of Notify. It returns a new Notify instance with default options. By default, the Notify
// instance is enabled.
func New() *Notify {
return NewWithOptions()
}
// NewWithServices returns a new instance of Notify with the given services. By default, the Notify instance is enabled.
// If no services are provided, it returns a new Notify instance with default options.
func NewWithServices(services ...Notifier) *Notify {
n := New()
n.UseServices(services...)
return n
}
// Create the package level Notify instance.
//
//nolint:gochecknoglobals // I agree with the linter, won't bother fixing this now, will be fixed in v2.
var std = New()
// Default returns the standard Notify instance used by the package-level send function.
func Default() *Notify {
return std
}