-
Notifications
You must be signed in to change notification settings - Fork 3
/
backends_toml.go
69 lines (54 loc) · 1.4 KB
/
backends_toml.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
// Copyright 2018 The OpenPitrix Authors. All rights reserved.
// Use of this source code is governed by a Apache license
// that can be found in the LICENSE file.
package libconfd
import (
"fmt"
"strings"
"github.com/BurntSushi/toml"
)
const TomlBackendType = "libconfd-backend-toml"
var _ BackendClient = (*TomlBackend)(nil)
type TomlBackend struct {
TOMLFile string
}
func init() {
RegisterBackendClient(
(*TomlBackend)(nil).Type(),
func(cfg *BackendConfig) (BackendClient, error) {
p := NewTomlBackendClient(cfg)
return p, nil
},
)
}
func NewTomlBackendClient(cfg *BackendConfig) *TomlBackend {
GetLogger().Assert(cfg.Type == (*TomlBackend)(nil).Type())
return &TomlBackend{TOMLFile: cfg.Host[0]}
}
func (_ *TomlBackend) Close() error {
return nil
}
func (_ *TomlBackend) Type() string {
return TomlBackendType
}
func (_ *TomlBackend) WatchEnabled() bool {
return false
}
func (_ *TomlBackend) WatchPrefix(prefix string, keys []string, waitIndex uint64, stopChan chan bool) (uint64, error) {
return 0, fmt.Errorf("do not support watch")
}
func (p *TomlBackend) GetValues(keys []string) (m map[string]string, err error) {
var dataMap map[string]string
_, err = toml.DecodeFile(p.TOMLFile, &dataMap)
if err != nil {
return nil, err
}
// skip invalid key
m = make(map[string]string)
for k, v := range dataMap {
if strings.HasPrefix(k, "/") {
m[k] = v
}
}
return m, nil
}