Skip to content

Commit

Permalink
allow for a folderPrefix to be specified while seeding from Consul (#133
Browse files Browse the repository at this point in the history
)
  • Loading branch information
wimspaargaren authored Aug 8, 2022
1 parent 4bfdc36 commit 2cd77fc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
16 changes: 11 additions & 5 deletions harvester.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
17 changes: 12 additions & 5 deletions seed/consul/getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,27 @@ package consul

import (
"errors"
"path"
"time"

"github.com/hashicorp/consul/api"
)

// 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")
}
Expand All @@ -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
}
Expand Down

0 comments on commit 2cd77fc

Please sign in to comment.