-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstore.go
71 lines (56 loc) · 1.36 KB
/
store.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
package config
import (
"fmt"
"net/url"
"sort"
"sync"
"github.com/deixis/spine/config/adapter"
"github.com/deixis/spine/config/adapter/consul"
"github.com/deixis/spine/config/adapter/file"
)
var (
sfMu sync.RWMutex
adapters = make(map[string]store.Adapter)
)
func init() {
// Register default adapters
Register(consul.Name, consul.New)
Register(file.Name, file.New)
}
// Adapters returns the list of registered adapters
func Adapters() []string {
sfMu.RLock()
defer sfMu.RUnlock()
var l []string
for a := range adapters {
l = append(l, a)
}
sort.Strings(l)
return l
}
// Register makes a store adapter available by the provided name.
// If an adapter is registered twice or if an adapter is nil, it will panic.
func Register(name string, adapter store.Adapter) {
sfMu.Lock()
defer sfMu.Unlock()
if adapter == nil {
panic("config: Registered adapter is nil")
}
if _, dup := adapters[name]; dup {
panic("config: Duplicated adapter")
}
adapters[name] = adapter
}
// NewStore returns a loaded config store defined by the ConfigStorage env
func NewStore(configStoreURI string) (store.Store, error) {
sfMu.RLock()
defer sfMu.RUnlock()
uri, err := url.Parse(configStoreURI)
if err != nil {
return nil, err
}
if f, ok := adapters[uri.Scheme]; ok {
return f(uri)
}
return nil, fmt.Errorf("store adapter not found <%s>", uri.Scheme)
}