本文整理汇总了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())
}
}
}
开发者ID:ecofit-networks,项目名称:consul-kv-tool,代码行数:35,代码来源:main.go
示例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)
}
}
开发者ID:flyhard,项目名称:git2AnyConsul,代码行数:26,代码来源:storage.go
示例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
}
开发者ID:CiscoCloud,项目名称:mantl-api,代码行数:9,代码来源:catalog.go
示例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
}
开发者ID:hypersleep,项目名称:go-sms,代码行数:11,代码来源:consul.go
示例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
}
开发者ID:samitpal,项目名称:consul-client-master-election,代码行数:16,代码来源:consul-client-master-election.go
示例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
}
开发者ID:CiscoCloud,项目名称:mantl-api,代码行数:38,代码来源:catalog.go
示例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()
}
开发者ID:ecofit-networks,项目名称:consul-kv-tool,代码行数:24,代码来源:main.go
示例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)
开发者ID:hotelzululima,项目名称:traefik,代码行数:31,代码来源:consul.go
示例9: consulKeys
func consulKeys(kv *api.KV, prefix string, lastIndex uint64) (interface{}, *api.QueryMeta, error) {
return kv.Keys(prefix, "", &api.QueryOptions{WaitIndex: lastIndex})
}
开发者ID:nsaje,项目名称:dagger,代码行数:3,代码来源:consul.go
示例10: consulGet
func consulGet(kv *api.KV, key string, lastIndex uint64) (interface{}, *api.QueryMeta, error) {
return kv.Get(key, &api.QueryOptions{WaitIndex: lastIndex})
}
开发者ID:nsaje,项目名称:dagger,代码行数:3,代码来源:consul.go
注:本文中的github.com/hashicorp/consul/api.KV类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论