本文整理匯總了Golang中github.com/hashicorp/consul/api.KV類的典型用法代碼示例。如果您正苦於以下問題:Golang KV類的具體用法?Golang KV怎麽用?Golang KV使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了KV類的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: inputFile
func inputFile(kv *consulapi.KV, path string) {
f, err := os.Open(path)
if err != nil {
log.Fatal(err.Error())
}
r := csv.NewReader(f)
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
if len(record) != 2 {
fmt.Printf("Incorrectly formatted line: %v\n", record)
}
d := &consulapi.KVPair{Key: record[0], Value: []byte(record[1])}
_, err = kv.Put(d, nil)
if err != nil {
if consulapi.IsServerError(err) && inRetries < 10 {
log.Println("consul server err: retry after 1s")
<-time.After(time.Second)
inRetries++
inputFile(kv, path)
return
}
log.Fatal(err.Error())
}
}
}
示例2: storeData
func storeData(kv *api.KV, key string, value []byte) {
pair, _, err := kv.Get(key, nil)
if err != nil {
Error.Fatal("Failed reading from Consul for key:", key, "Error:", err)
}
if pair == nil || string(pair.Value) != string(value) {
var p *api.KVPair
if pair == nil {
p = &api.KVPair{Key: key, Value: value}
} else {
p = &api.KVPair{Key: key, Value: value, ModifyIndex: pair.ModifyIndex}
}
success, meta, err := kv.CAS(p, nil)
if err != nil {
Error.Fatal(err)
}
if !success {
storeData(kv, key, value)
} else {
Trace.Print("Request took: ", meta.RequestTime.Seconds(), " seconds")
Info.Print("Updated key '", key, "' to '", string(value), "'")
}
} else {
Trace.Print("not updating key", key)
}
}
示例3: keyExists
func keyExists(key string, kv *consul.KV) bool {
kp, _, err := kv.Get(key, nil)
if err != nil {
log.Warnf("Could not get key path %s: %v", key, err)
return false
}
return kp != nil
}
示例4: fetchConsul
func fetchConsul(kv *consul.KV, env string, key string) (value string, conf_err error) {
pair, _, _ := kv.Get("go-sms/"+env+"/"+key, nil)
if pair == nil {
conf_err = errors.New("Failed to fetch \"" + key + "\" key for you environment. Leave default: " + defaults[key])
value = defaults[key]
return
}
value = string(pair.Value)
return
}
示例5: getSequencer
// getSequencer gets the lockindex, session id and the key which constitutes the sequencer for the current lock.
func getSequencer(kv *api.KV, key string) (*sequencer, error) {
kvPair, _, err := kv.Get(key, nil)
if err != nil {
log.Println("Can't get the sequencer")
return nil, err
}
if kvPair == nil {
return nil, nil
}
seq := sequencer{}
seq.lockIndex = kvPair.LockIndex
seq.session = kvPair.Session
seq.key = kvPair.Key
return &seq, nil
}
示例6: NewPackageCatalog
func NewPackageCatalog(kv *consul.KV, repositoryRoot string) (*packageCatalog, error) {
catalog := &packageCatalog{kv: kv}
pkgIndex := make(map[string]map[string]map[string]string)
keys, _, err := kv.Keys(repositoryRoot, "", nil)
if err != nil {
return nil, err
}
sort.Strings(keys)
// package key example: mantl-install/repository/0/repo/packages/S/spark/3/config.json
for _, key := range keys {
parts := strings.Split(key, "/")
if len(parts) == 9 {
repoIdx := parts[2]
name := parts[6]
verIdx := parts[7]
_, ok := pkgIndex[name]
if !ok {
pkgIndex[name] = make(map[string]map[string]string)
}
_, ok = pkgIndex[name][repoIdx]
if !ok {
pkgIndex[name][repoIdx] = make(map[string]string)
}
_, ok = pkgIndex[name][repoIdx][verIdx]
if !ok {
pkgKey := key[0 : strings.LastIndex(key, "/")+1]
pkgIndex[name][repoIdx][verIdx] = pkgKey
}
}
}
catalog.catalog = pkgIndex
return catalog, nil
}
示例7: outputFile
func outputFile(kv *consulapi.KV, path, prefix string) {
vs, _, err := kv.List(prefix, nil)
if err != nil {
if consulapi.IsServerError(err) && outRetries < 10 {
log.Println("consul server err: retry after 1s")
<-time.After(time.Second)
outRetries++
outputFile(kv, path, prefix)
return
}
log.Fatal(err.Error())
}
f, err := os.Create(path)
if err != nil {
log.Fatal(err.Error())
}
defer f.Close()
for _, val := range vs {
fmt.Fprintf(f, "%s,%s\n", val.Key, val.Value)
}
f.Sync()
}
示例8:
"github.com/hashicorp/consul/api"
)
type Key struct {
Value string
}
type ConsulProvider struct {
Watch bool
Endpoint string
Prefix string
Filename string
consulClient *api.Client
}
var kvClient *api.KV
var ConsulFuncMap = template.FuncMap{
"List": func(keys ...string) []string {
joinedKeys := strings.Join(keys, "")
keysPairs, _, err := kvClient.Keys(joinedKeys, "/", nil)
if err != nil {
log.Error("Error getting keys ", joinedKeys, err)
return nil
}
keysPairs = fun.Filter(func(key string) bool {
if key == joinedKeys {
return false
}
return true
}, keysPairs).([]string)
示例9: consulKeys
func consulKeys(kv *api.KV, prefix string, lastIndex uint64) (interface{}, *api.QueryMeta, error) {
return kv.Keys(prefix, "", &api.QueryOptions{WaitIndex: lastIndex})
}
示例10: consulGet
func consulGet(kv *api.KV, key string, lastIndex uint64) (interface{}, *api.QueryMeta, error) {
return kv.Get(key, &api.QueryOptions{WaitIndex: lastIndex})
}