-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify.go
61 lines (51 loc) · 1.25 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
// Copyright 2019 Luis Guillén Civera <luisguillenc@gmail.com>. View LICENSE.
package event
import (
"context"
"errors"
"os"
"path/filepath"
)
// Notifier is the interface for event notifiers.
type Notifier interface {
NotifyEvent(ctx context.Context, e Event) (string, error)
}
// NotifyBuffer is the interface for event buffer implementations.
type NotifyBuffer interface {
PushEvent(e Event) error
Close()
}
// SetBuffer sets the default buffer instance.
func SetBuffer(b NotifyBuffer) {
defaultBuffer = b
}
// Notify notifies using the default buffer instance.
func Notify(e Event) error {
if defaultBuffer != nil {
return defaultBuffer.PushEvent(e)
}
return errors.New("event: default buffer not defined")
}
// SetDefaultInstance changes default instance name.
func SetDefaultInstance(label string) {
if instanceRegExp.MatchString(label) {
defaultSource.Instance = label
}
}
// GetDefaultSource returns default notify events source.
func GetDefaultSource() Source {
return defaultSource
}
var defaultSource Source
var defaultBuffer NotifyBuffer
func init() {
hostname, err := os.Hostname()
if err != nil {
hostname = "unknown"
}
defaultSource = Source{
Hostname: hostname,
Program: filepath.Base(os.Args[0]),
PID: os.Getpid(),
}
}