-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconsul.go
51 lines (41 loc) · 1.05 KB
/
consul.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"fmt"
"strings"
"github.com/hashicorp/consul/api"
)
// Consul is a client for fetching values from Consul's KV store
type Consul struct {
kv *api.KV
}
// NewConsul returns a new instance of the Consul client using the given address
func NewConsul(addr string) (*Consul, error) {
cfg := api.DefaultConfig()
cfg.Address = addr
c, err := api.NewClient(cfg)
if err != nil {
return nil, fmt.Errorf("could not connect to %s: %w", addr, err)
}
return &Consul{c.KV()}, nil
}
// Load fetches values from the given paths
func (c *Consul) Load(path string) (Dict, error) {
kv := make(Dict)
pairs, _, err := c.kv.List(path, &api.QueryOptions{})
if err != nil {
return kv, fmt.Errorf("could not load %s: %w", path, err)
}
for _, p := range pairs {
if p.Value == nil {
continue
}
kv[consulKey(p.Key)] = string(p.Value)
}
return kv, nil
}
// consulKey converts the full path to the env var name
func consulKey(path string) string {
parts := strings.Split(path, "/")
key := parts[len(parts)-1]
return strings.ToUpper(key)
}