-
Notifications
You must be signed in to change notification settings - Fork 1
/
multi.go
49 lines (42 loc) · 1.02 KB
/
multi.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
package runtime
import (
"context"
"github.com/flowline-io/flowbot/internal/types"
"sync"
"github.com/pkg/errors"
)
type MultiMounter struct {
mounters map[string]Mounter
mu sync.RWMutex
}
func NewMultiMounter() *MultiMounter {
return &MultiMounter{
mounters: map[string]Mounter{},
}
}
func (m *MultiMounter) Mount(ctx context.Context, mnt *types.Mount) error {
m.mu.RLock()
defer m.mu.RUnlock()
mounter, ok := m.mounters[mnt.Type]
if !ok {
return errors.Errorf("unknown mount type: %s", mnt.Type)
}
return mounter.Mount(ctx, mnt)
}
func (m *MultiMounter) Unmount(ctx context.Context, mnt *types.Mount) error {
m.mu.RLock()
defer m.mu.RUnlock()
mounter, ok := m.mounters[mnt.Type]
if !ok {
return errors.Errorf("unknown mount type: %s", mnt.Type)
}
return mounter.Unmount(ctx, mnt)
}
func (m *MultiMounter) RegisterMounter(mtype string, mr Mounter) {
m.mu.Lock()
defer m.mu.Unlock()
if _, ok := m.mounters[mtype]; ok {
panic("mount: Register called twice for mounter")
}
m.mounters[mtype] = mr
}