/
link.go
81 lines (64 loc) · 1.72 KB
/
link.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
package channels
import (
"context"
"encoding/json"
"github.com/n0rad/go-erlog/data"
"github.com/n0rad/go-erlog/errs"
"github.com/n0rad/go-erlog/logs"
)
type Link interface {
Init() error
GetName() string
GetDescription() string
Watch(shutdown <-chan struct{}, events chan<- Event)
Trigger(cxt context.Context, action Action) error
GetFields() data.Fields
}
/////////////////////////
func LinkFromJson(content []byte) (Link, error) {
lc := &CommonLink{}
if err := json.Unmarshal([]byte(content), lc); err != nil {
return nil, errs.WithE(err, "Failed to unmarshall link type")
}
channel, ok := ChannelByIds[lc.Channel]
if !ok {
return nil, errs.WithF(data.WithField("chan", lc.Channel), "Unknown channel")
}
link := channel.NewLink()
if err := json.Unmarshal([]byte(content), link); err != nil {
return nil, errs.WithE(err, "Failed to unmarshall link")
}
if err := link.Init(); err != nil {
return nil, errs.WithEF(err, link.GetFields(), "Failed to init link")
}
return link, nil
}
type CommonLink struct {
Channel string
Name string `json:",omitempty"`
Description string `json:",omitempty"`
fields data.Fields
}
func (l *CommonLink) GetFields() data.Fields {
return l.fields
}
func (l *CommonLink) Init() error {
l.fields = data.WithField("link", l.Name)
return nil
}
func (l *CommonLink) GetName() string {
return l.Name
}
func (l *CommonLink) GetDescription() string {
return l.Description
}
func (l *CommonLink) Watch(shutdown <-chan struct{}, events chan<- Event) {
select {
case <-shutdown:
return
}
}
func (l *CommonLink) Trigger(cxt context.Context, action Action) error {
logs.WithFields(l.fields).WithField("action", action).Debug("Receiving action on common link")
return nil
}