-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package consul | ||
|
||
import ( | ||
"github.com/armon/consul-kv" | ||
) | ||
|
||
// Client provides a wrapper around the consulkv client | ||
type Client struct { | ||
client *consulkv.Client | ||
} | ||
|
||
// NewConsulClient returns a new client to Consul for the given address | ||
func NewConsulClient(addr string) (*Client, error) { | ||
conf := consulkv.DefaultConfig() | ||
conf.Address = addr | ||
client, err := consulkv.NewClient(conf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
c := &Client{ | ||
client: client, | ||
} | ||
return c, nil | ||
} | ||
|
||
// GetValues queries Consul for keys prefixed by prefix. | ||
func (c *Client) GetValues(keys []string) (map[string]interface{}, error) { | ||
vars := make(map[string]interface{}) | ||
for _, key := range keys { | ||
_, pairs, err := c.client.List(key) | ||
if err != nil { | ||
return vars, err | ||
} | ||
for _, p := range pairs { | ||
vars[p.Key] = p.Value | ||
} | ||
} | ||
return vars, nil | ||
} |