-
Notifications
You must be signed in to change notification settings - Fork 20
/
truststore.go
66 lines (51 loc) · 1.63 KB
/
truststore.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
package trust
import (
"fmt"
"sync"
"github.com/fsnotify/fsnotify"
"github.com/canonical/microcluster/internal/sys"
)
// Store represents a directory of remotes watched by the fsnotify Watcher.
type Store struct {
remotesMu sync.RWMutex // Mutex for coordinating manual and fsnotify access to remotes.
remotes *Remotes // Should never be called directly, instead use Remotes().
refresh func(path string) error
}
// Init initializes the remotes in the truststore, seeds the rand package for selecting remotes at random, and watches
// the truststore directory for updates.
func Init(watcher *sys.Watcher, onUpdate func(oldRemotes, newRemotes Remotes) error, dir string) (*Store, error) {
ts := &Store{remotes: &Remotes{}}
ts.remotesMu.Lock()
defer ts.remotesMu.Unlock()
ts.remotes.data = map[string]Remote{}
err := ts.remotes.Load(dir)
if err != nil {
return nil, err
}
ts.refresh = func(path string) error {
ts.remotesMu.Lock()
defer func() {
ts.remotesMu.Unlock()
}()
err := ts.remotes.Load(dir)
if err != nil {
return fmt.Errorf("Unable to refresh remotes in path %q: %w", path, err)
}
return nil
}
// Watch on the truststore directory for yaml updates.
watcher.Watch(dir, "yaml", func(path string, event fsnotify.Op) error {
return ts.refresh(path)
})
return ts, nil
}
// Remotes returns a thread-safe list of the remotes in the truststore, as watched by fsnotify.
func (ts *Store) Remotes() *Remotes {
ts.remotesMu.RLock()
defer ts.remotesMu.RUnlock()
return ts.remotes
}
// Refresh reloads the truststore and runs any associated hooks.
func (ts *Store) Refresh() error {
return ts.refresh("*")
}