From 6d316c475f09ef0fac06b295dd103923a7383b66 Mon Sep 17 00:00:00 2001 From: yoyofx Date: Wed, 12 May 2021 13:34:53 +0800 Subject: [PATCH] viper provider --- config_remote_provider.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 config_remote_provider.go diff --git a/config_remote_provider.go b/config_remote_provider.go new file mode 100644 index 0000000..003c6ee --- /dev/null +++ b/config_remote_provider.go @@ -0,0 +1,39 @@ +package nacos_viper_remote + +import ( + "fmt" + "github.com/spf13/viper" +) + +type ViperRemoteProvider struct { + configSet string +} + +func NewRemoteProvider() *ViperRemoteProvider { + return &ViperRemoteProvider{ + configSet: "yoyogo.cloud.discovery.metadata"} +} + +func (provider *ViperRemoteProvider) GetProvider(runtime_viper *viper.Viper) *viper.Viper { + var option *Option + err := runtime_viper.Sub("yoyogo.cloud.discovery.metadata").Unmarshal(&option) + if err != nil { + panic(err) + return nil + } + SetOptions(option) + remote_viper := viper.New() + err = remote_viper.AddRemoteProvider("nacos", "localhost", "") + remote_viper.SetConfigType("yaml") + err = remote_viper.ReadRemoteConfig() + if err == nil { + err = remote_viper.WatchRemoteConfigOnChannel() + if err == nil { + fmt.Println("used remote viper") + return remote_viper + } + } else { + panic(err) + } + return runtime_viper +}