Add etcd store

This commit is contained in:
Asim 2015-02-07 20:12:57 +00:00
parent 9af9140121
commit 9fb4385f6e
2 changed files with 66 additions and 0 deletions

14
store/etcd_item.go Normal file
View File

@ -0,0 +1,14 @@
package store
type EtcdItem struct {
key string
value []byte
}
func (c *EtcdItem) Key() string {
return c.key
}
func (c *EtcdItem) Value() []byte {
return c.value
}

52
store/etcd_store.go Normal file
View File

@ -0,0 +1,52 @@
package store
import (
"errors"
"github.com/coreos/go-etcd/etcd"
)
type EtcdStore struct {
Client *etcd.Client
}
func (e *EtcdStore) Get(key string) (Item, error) {
kv, err := e.Client.Get(key, false, false)
if err != nil {
return nil, err
}
if kv == nil {
return nil, errors.New("key not found")
}
return &EtcdItem{
key: kv.Node.Key,
value: []byte(kv.Node.Value),
}, nil
}
func (e *EtcdStore) Del(key string) error {
_, err := e.Client.Delete(key, false)
return err
}
func (e *EtcdStore) Put(item Item) error {
_, err := e.Client.Set(item.Key(), string(item.Value()), 0)
return err
}
func (e *EtcdStore) NewItem(key string, value []byte) Item {
return &EtcdItem{
key: key,
value: value,
}
}
func NewEtcdStore() Store {
client := etcd.NewClient([]string{})
return &EtcdStore{
Client: client,
}
}