From 1e811d1f02ebe6924bdc87d36311224c1c7d16d6 Mon Sep 17 00:00:00 2001 From: John Hooks Date: Fri, 21 Jul 2023 15:10:15 -0400 Subject: [PATCH] Add NATS support --- README.md | 9 +++++++++ remote/remote.go | 4 ++++ viper.go | 10 +++++----- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b8c217c..b53e9ab 100644 --- a/README.md +++ b/README.md @@ -488,6 +488,15 @@ err := viper.ReadRemoteConfig() Of course, you're allowed to use `SecureRemoteProvider` also + +#### NATS + +```go +viper.AddRemoteProvider("nats", "nats://127.0.0.1:4222", "myapp.config") +viper.SetConfigType("json") +err := viper.ReadRemoteConfig() +``` + ### Remote Key/Value Store Example - Encrypted ```go diff --git a/remote/remote.go b/remote/remote.go index 63e69c1..d61f880 100644 --- a/remote/remote.go +++ b/remote/remote.go @@ -91,6 +91,8 @@ func getConfigManager(rp viper.RemoteProvider) (crypt.ConfigManager, error) { cm, err = crypt.NewEtcdV3ConfigManager(endpoints, kr) case "firestore": cm, err = crypt.NewFirestoreConfigManager(endpoints, kr) + case "nats": + cm, err = crypt.NewNatsConfigManager(endpoints, kr) default: cm, err = crypt.NewConsulConfigManager(endpoints, kr) } @@ -102,6 +104,8 @@ func getConfigManager(rp viper.RemoteProvider) (crypt.ConfigManager, error) { cm, err = crypt.NewStandardEtcdV3ConfigManager(endpoints) case "firestore": cm, err = crypt.NewStandardFirestoreConfigManager(endpoints) + case "nats": + cm, err = crypt.NewStandardNatsConfigManager(endpoints) default: cm, err = crypt.NewStandardConsulConfigManager(endpoints) } diff --git a/viper.go b/viper.go index 2ff730f..6c9f0e0 100644 --- a/viper.go +++ b/viper.go @@ -301,7 +301,7 @@ func NewWithOptions(opts ...Option) *Viper { func Reset() { v = New() SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "tfvars", "dotenv", "env", "ini"} - SupportedRemoteProviders = []string{"etcd", "etcd3", "consul", "firestore"} + SupportedRemoteProviders = []string{"etcd", "etcd3", "consul", "firestore", "nats"} } // TODO: make this lazy initialization instead @@ -420,7 +420,7 @@ type RemoteProvider interface { var SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "tfvars", "dotenv", "env", "ini"} // SupportedRemoteProviders are universally supported remote providers. -var SupportedRemoteProviders = []string{"etcd", "etcd3", "consul", "firestore"} +var SupportedRemoteProviders = []string{"etcd", "etcd3", "consul", "firestore", "nats"} // OnConfigChange sets the event handler that is called when a config file changes. func OnConfigChange(run func(in fsnotify.Event)) { v.OnConfigChange(run) } @@ -584,8 +584,8 @@ func (v *Viper) AddConfigPath(in string) { // AddRemoteProvider adds a remote configuration source. // Remote Providers are searched in the order they are added. -// provider is a string value: "etcd", "etcd3", "consul" or "firestore" are currently supported. -// endpoint is the url. etcd requires http://ip:port consul requires ip:port +// provider is a string value: "etcd", "etcd3", "consul", "firestore" or "nats" are currently supported. +// endpoint is the url. etcd requires http://ip:port, consul requires ip:port, nats requires nats://ip:port // path is the path in the k/v store to retrieve configuration // To retrieve a config file called myapp.json from /configs/myapp.json // you should set path to /configs and set config name (SetConfigName()) to @@ -615,7 +615,7 @@ func (v *Viper) AddRemoteProvider(provider, endpoint, path string) error { // AddSecureRemoteProvider adds a remote configuration source. // Secure Remote Providers are searched in the order they are added. -// provider is a string value: "etcd", "etcd3", "consul" or "firestore" are currently supported. +// provider is a string value: "etcd", "etcd3", "consul", "firestore" or "nats" are currently supported. // endpoint is the url. etcd requires http://ip:port consul requires ip:port // secretkeyring is the filepath to your openpgp secret keyring. e.g. /etc/secrets/myring.gpg // path is the path in the k/v store to retrieve configuration