Created
December 1, 2015 12:01
-
-
Save asim/1960ae5a9c18ea0fb496 to your computer and use it in GitHub Desktop.
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
package kv | |
import ( | |
"errors" | |
consul "github.com/hashicorp/consul/api" | |
) | |
type consulKV struct { | |
Client *consul.Client | |
} | |
func (c *consulKV) Get(key string) (*Item, error) { | |
keyval, _, err := c.Client.KV().Get(key, nil) | |
if err != nil { | |
return nil, err | |
} | |
if keyval == nil { | |
return nil, errors.New("key not found") | |
} | |
return &Item{ | |
Key: keyval.Key, | |
Value: keyval.Value, | |
}, nil | |
} | |
func (c *consulKV) Del(key string) error { | |
_, err := c.Client.KV().Delete(key, nil) | |
return err | |
} | |
func (c *consulKV) Put(item *Item) error { | |
_, err := c.Client.KV().Put(&consul.KVPair{ | |
Key: item.Key, | |
Value: item.Value, | |
}, nil) | |
return err | |
} | |
func newConsulKV(addrs []string, opt ...Option) KV { | |
config := consul.DefaultConfig() | |
if len(addrs) > 0 { | |
config.Address = addrs[0] | |
} | |
client, _ := consul.NewClient(config) | |
return &consulKV{ | |
Client: client, | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment