2015-05-30 22:28:33 +03:00
|
|
|
// Copyright © 2015 Steve Francia <spf@spf13.com>.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package remote integrates the remote features of Viper.
|
|
|
|
package remote
|
|
|
|
|
2022-12-12 16:17:28 +03:00
|
|
|
// import (
|
|
|
|
// "bytes"
|
|
|
|
// "io"
|
|
|
|
// "os"
|
2017-12-07 07:26:31 +03:00
|
|
|
|
2022-12-12 16:17:28 +03:00
|
|
|
// crypt "github.com/sagikazarmark/crypt/config"
|
2020-09-11 18:48:38 +03:00
|
|
|
|
2022-12-12 16:17:28 +03:00
|
|
|
// "git.internal/re/viper"
|
|
|
|
// )
|
2015-05-30 22:28:33 +03:00
|
|
|
|
2022-12-12 16:17:28 +03:00
|
|
|
// type remoteConfigProvider struct{}
|
2015-05-30 22:28:33 +03:00
|
|
|
|
2022-12-12 16:17:28 +03:00
|
|
|
// func (rc remoteConfigProvider) Get(rp viper.RemoteProvider) (io.Reader, error) {
|
|
|
|
// cm, err := getConfigManager(rp)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
// b, err := cm.Get(rp.Path())
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
// return bytes.NewReader(b), nil
|
|
|
|
// }
|
2015-05-30 22:28:33 +03:00
|
|
|
|
2022-12-12 16:17:28 +03:00
|
|
|
// func (rc remoteConfigProvider) Watch(rp viper.RemoteProvider) (io.Reader, error) {
|
|
|
|
// cm, err := getConfigManager(rp)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
// resp, err := cm.Get(rp.Path())
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
2015-05-30 22:28:33 +03:00
|
|
|
|
2022-12-12 16:17:28 +03:00
|
|
|
// return bytes.NewReader(resp), nil
|
|
|
|
// }
|
2017-07-23 08:47:47 +03:00
|
|
|
|
2022-12-12 16:17:28 +03:00
|
|
|
// func (rc remoteConfigProvider) WatchChannel(rp viper.RemoteProvider) (<-chan *viper.RemoteResponse, chan bool) {
|
|
|
|
// cm, err := getConfigManager(rp)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, nil
|
|
|
|
// }
|
|
|
|
// quit := make(chan bool)
|
|
|
|
// quitwc := make(chan bool)
|
|
|
|
// viperResponsCh := make(chan *viper.RemoteResponse)
|
|
|
|
// cryptoResponseCh := cm.Watch(rp.Path(), quit)
|
|
|
|
// // need this function to convert the Channel response form crypt.Response to viper.Response
|
|
|
|
// go func(cr <-chan *crypt.Response, vr chan<- *viper.RemoteResponse, quitwc <-chan bool, quit chan<- bool) {
|
|
|
|
// for {
|
|
|
|
// select {
|
|
|
|
// case <-quitwc:
|
|
|
|
// quit <- true
|
|
|
|
// return
|
|
|
|
// case resp := <-cr:
|
|
|
|
// vr <- &viper.RemoteResponse{
|
|
|
|
// Error: resp.Error,
|
|
|
|
// Value: resp.Value,
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }(cryptoResponseCh, viperResponsCh, quitwc, quit)
|
2017-03-15 16:43:09 +03:00
|
|
|
|
2022-12-12 16:17:28 +03:00
|
|
|
// return viperResponsCh, quitwc
|
|
|
|
// }
|
2017-03-15 16:43:09 +03:00
|
|
|
|
2022-12-12 16:17:28 +03:00
|
|
|
// func getConfigManager(rp viper.RemoteProvider) (crypt.ConfigManager, error) {
|
|
|
|
// var cm crypt.ConfigManager
|
|
|
|
// var err error
|
2015-05-30 22:28:33 +03:00
|
|
|
|
2022-12-12 16:17:28 +03:00
|
|
|
// if rp.SecretKeyring() != "" {
|
|
|
|
// var kr *os.File
|
|
|
|
// kr, err = os.Open(rp.SecretKeyring())
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
// defer kr.Close()
|
|
|
|
// switch rp.Provider() {
|
|
|
|
// case "etcd":
|
|
|
|
// cm, err = crypt.NewEtcdConfigManager([]string{rp.Endpoint()}, kr)
|
|
|
|
// case "etcd3":
|
|
|
|
// cm, err = crypt.NewEtcdV3ConfigManager([]string{rp.Endpoint()}, kr)
|
|
|
|
// case "firestore":
|
|
|
|
// cm, err = crypt.NewFirestoreConfigManager([]string{rp.Endpoint()}, kr)
|
|
|
|
// default:
|
|
|
|
// cm, err = crypt.NewConsulConfigManager([]string{rp.Endpoint()}, kr)
|
|
|
|
// }
|
|
|
|
// } else {
|
|
|
|
// switch rp.Provider() {
|
|
|
|
// case "etcd":
|
|
|
|
// cm, err = crypt.NewStandardEtcdConfigManager([]string{rp.Endpoint()})
|
|
|
|
// case "etcd3":
|
|
|
|
// cm, err = crypt.NewStandardEtcdV3ConfigManager([]string{rp.Endpoint()})
|
|
|
|
// case "firestore":
|
|
|
|
// cm, err = crypt.NewStandardFirestoreConfigManager([]string{rp.Endpoint()})
|
|
|
|
// default:
|
|
|
|
// cm, err = crypt.NewStandardConsulConfigManager([]string{rp.Endpoint()})
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
// return cm, nil
|
|
|
|
// }
|
2015-05-30 22:28:33 +03:00
|
|
|
|
2022-12-12 16:17:28 +03:00
|
|
|
// func init() {
|
|
|
|
// viper.RemoteConfig = &remoteConfigProvider{}
|
|
|
|
// }
|