From 2cd77fcb4b9ef575341368f93b6940d42d5b76da Mon Sep 17 00:00:00 2001 From: Wim Spaargaren Date: Mon, 8 Aug 2022 12:28:09 +0200 Subject: [PATCH] allow for a folderPrefix to be specified while seeding from Consul (#133) --- harvester.go | 16 +++++++++++----- seed/consul/getter.go | 17 ++++++++++++----- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/harvester.go b/harvester.go index 284e632e..9e52803a 100644 --- a/harvester.go +++ b/harvester.go @@ -86,14 +86,20 @@ func (b *Builder) WithNotification(chNotify chan<- config.ChangeNotification) *B // WithConsulSeed enables support for seeding values with consul. func (b *Builder) WithConsulSeed(addr, dataCenter, token string, timeout time.Duration) *Builder { + return b.WithConsulSeedWithPrefix(addr, dataCenter, token, "", timeout) +} + +// WithConsulSeedWithPrefix enables support for seeding values with consul including a folder prefix. +func (b *Builder) WithConsulSeedWithPrefix(addr, dataCenter, token, folderPrefix string, timeout time.Duration) *Builder { if b.err != nil { return b } b.seedConsulCfg = &consulConfig{ - addr: addr, - dataCenter: dataCenter, - token: token, - timeout: timeout, + addr: addr, + dataCenter: dataCenter, + token: token, + folderPrefix: folderPrefix, + timeout: timeout, } return b } @@ -202,7 +208,7 @@ func (b *Builder) setupConsulSeeding() (*seed.Param, error) { return nil, nil } - getter, err := seedconsul.New(b.seedConsulCfg.addr, b.seedConsulCfg.dataCenter, b.seedConsulCfg.token, + getter, err := seedconsul.NewWithFolderPrefix(b.seedConsulCfg.addr, b.seedConsulCfg.dataCenter, b.seedConsulCfg.token, b.seedConsulCfg.folderPrefix, b.seedConsulCfg.timeout) if err != nil { return nil, err diff --git a/seed/consul/getter.go b/seed/consul/getter.go index cfc3f499..c650f027 100644 --- a/seed/consul/getter.go +++ b/seed/consul/getter.go @@ -3,6 +3,7 @@ package consul import ( "errors" + "path" "time" "github.com/hashicorp/consul/api" @@ -10,13 +11,19 @@ import ( // Getter implementation of the getter interface. type Getter struct { - kv *api.KV - dc string - token string + kv *api.KV + dc string + token string + folderPrefix string } // New constructor. Timeout is set to 60s when 0 is provided. func New(addr, dc, token string, timeout time.Duration) (*Getter, error) { + return NewWithFolderPrefix(addr, dc, token, "", timeout) +} + +// NewWithFolderPrefix constructor. Timeout is set to 60s when 0 is provided. +func NewWithFolderPrefix(addr, dc, token, folderPrefix string, timeout time.Duration) (*Getter, error) { if addr == "" { return nil, errors.New("address is empty") } @@ -38,12 +45,12 @@ func New(addr, dc, token string, timeout time.Duration) (*Getter, error) { if err != nil { return nil, err } - return &Getter{kv: consul.KV(), dc: dc, token: token}, nil + return &Getter{kv: consul.KV(), dc: dc, token: token, folderPrefix: folderPrefix}, nil } // Get the specific key value from consul. func (g *Getter) Get(key string) (*string, uint64, error) { - pair, _, err := g.kv.Get(key, &api.QueryOptions{Datacenter: g.dc, Token: g.token}) + pair, _, err := g.kv.Get(path.Join(g.folderPrefix, key), &api.QueryOptions{Datacenter: g.dc, Token: g.token}) if err != nil { return nil, 0, err }