open-consul/watch/funcs.go

45 lines
1021 B
Go
Raw Normal View History

2014-08-20 20:45:34 +00:00
package watch
import (
"fmt"
"github.com/armon/consul-api"
)
// watchFactory is a function that can create a new WatchFunc
// from a parameter configuration
type watchFactory func(params map[string][]string) (WatchFunc, error)
// watchFuncFactory maps each type to a factory function
var watchFuncFactory map[string]watchFactory
func init() {
watchFuncFactory = map[string]watchFactory{
"key": keyWatch,
}
}
// keyWatch is used to return a key watching function
func keyWatch(params map[string][]string) (WatchFunc, error) {
keys := params["key"]
delete(params, "key")
if len(keys) != 1 {
return nil, fmt.Errorf("Must specify a single key to watch")
}
key := keys[0]
fn := func(p *WatchPlan) (uint64, interface{}, error) {
kv := p.client.KV()
opts := consulapi.QueryOptions{WaitIndex: p.lastIndex}
pair, meta, err := kv.Get(key, &opts)
if err != nil {
return 0, nil, err
}
2014-08-20 22:18:08 +00:00
if pair == nil {
return meta.LastIndex, nil, err
}
2014-08-20 20:45:34 +00:00
return meta.LastIndex, pair, err
}
return fn, nil
}