-
Notifications
You must be signed in to change notification settings - Fork 0
/
catalogue.go
142 lines (129 loc) · 2.83 KB
/
catalogue.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package catalogue
import (
"context"
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/sirupsen/logrus"
"sync"
"time"
)
type Catalogue interface {
fmt.Stringer
SetTeams([]Team)
SyncWithFile(ctx context.Context, log *logrus.Entry, path string, interval time.Duration) error
Teams() []Team
TeamByServiceName(service string) []Team
Team(id string) (Team, bool)
}
type Service struct {
Name string
Description string
Url string
DashboardUrl string
DocumentationUrl string
SourceCodeUrl string
ImChannelUrl string
}
type OnCall struct {
ScheduleName string
Vendor string
}
type Team struct {
Id string
Name string
Email string
IMChannelURL string
DashboardURL string
DocumentationURL string
Services []Service
OnCall OnCall
}
func New() Catalogue {
return &catalogue{
mtx: sync.RWMutex{},
teams: []Team{},
}
}
type catalogue struct {
mtx sync.RWMutex
teams []Team
}
func (c *catalogue) String() string {
return "team info catalogue"
}
func (c *catalogue) SetTeams(newTeams []Team) {
if len(newTeams) == 0 {
return
}
c.mtx.Lock()
defer c.mtx.Unlock()
c.teams = newTeams
}
func (c *catalogue) Teams() []Team {
c.mtx.RLock()
defer c.mtx.RUnlock()
return c.teams
}
func (c *catalogue) TeamByServiceName(serviceName string) []Team {
res := []Team{}
for _, t := range c.Teams() {
for _, s := range t.Services {
if s.Name == serviceName {
res = append(res, t)
}
}
}
return res
}
func (c *catalogue) Team(id string) (Team, bool) {
c.mtx.RLock()
defer c.mtx.RUnlock()
for _, t := range c.teams {
if t.Id == id {
return t, true
}
}
return Team{}, false
}
func (c *catalogue) SyncWithFile(ctx context.Context, log *logrus.Entry, path string, interval time.Duration) error {
newTeams, err := LoadFromFile(path)
if err != nil {
return fmt.Errorf("error loading catalogue file: %v", err)
}
c.SetTeams(newTeams)
log.Info("updated catalogue from file")
watcher, err := fsnotify.NewWatcher()
if err != nil {
return fmt.Errorf("error watching catalogue file: %v", err)
}
if err := watcher.Add(path); err != nil {
return fmt.Errorf("error watching catalogue file: %v", err)
}
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return nil
}
if event.Op&fsnotify.Write != fsnotify.Write {
continue
}
log.WithField("path", path).Info("synchronizing catalogue with file")
newTeams, err := LoadFromFile(path)
if err != nil {
log.Errorf("failed to update catalogue from file: %v", err)
} else {
c.SetTeams(newTeams)
}
case err, ok := <-watcher.Errors:
if !ok {
return nil
}
log.Errorf("error watching catalogue file: %v", err)
case <-ctx.Done():
_ = watcher.Close()
log.Info("stopping catalog file update")
return nil
}
}
}