-
Notifications
You must be signed in to change notification settings - Fork 14
/
viper_remote.go
64 lines (54 loc) · 1.42 KB
/
viper_remote.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
package nacos_viper_remote
import (
"bytes"
"errors"
"github.com/spf13/viper"
"io"
)
//var nacosOptions = &Option{}
func SetOptions(option *Option) {
manager, _ := NewNacosConfigManager(option)
viper.SupportedRemoteProviders = []string{"nacos"}
viper.RemoteConfig = &remoteConfigProvider{ConfigManager: manager}
}
type remoteConfigProvider struct {
ConfigManager *nacosConfigManager
}
func (rc *remoteConfigProvider) Get(rp viper.RemoteProvider) (io.Reader, error) {
cmt, err := rc.getConfigManager(rp)
if err != nil {
return nil, err
}
var b []byte
switch cm := cmt.(type) {
case viperConfigManager:
b, err = cm.Get(rp.Path())
}
if err != nil {
return nil, err
}
return bytes.NewReader(b), nil
}
func (rc *remoteConfigProvider) Watch(rp viper.RemoteProvider) (io.Reader, error) {
return rc.Get(rp)
}
func (rc *remoteConfigProvider) WatchChannel(rp viper.RemoteProvider) (<-chan *viper.RemoteResponse, chan bool) {
cmt, err := rc.getConfigManager(rp)
if err != nil {
return nil, nil
}
switch cm := cmt.(type) {
case viperConfigManager:
quit := make(chan bool)
viperResponseCh := cm.Watch("dataId", quit)
return viperResponseCh, quit
}
return nil, nil
}
func (rc *remoteConfigProvider) getConfigManager(rp viper.RemoteProvider) (interface{}, error) {
if rp.Provider() == "nacos" {
return rc.ConfigManager, nil
} else {
return nil, errors.New("The Nacos configuration manager is not supported!")
}
}