當前位置: 首頁>>代碼示例>>Golang>>正文


Golang api.KV類代碼示例

本文整理匯總了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;未經允許,請勿轉載。