Ejemplo n.º 1
0
func (provider *Kv) list(keys ...string) []string {
	joinedKeys := strings.Join(keys, "")
	keysPairs, err := provider.kvclient.List(joinedKeys)
	if err != nil {
		log.Debugf("Cannot get keys %s %s ", joinedKeys, err)
		return nil
	}
	directoryKeys := make(map[string]string)
	for _, key := range keysPairs {
		directory := strings.Split(strings.TrimPrefix(key.Key, joinedKeys), "/")[0]
		directoryKeys[directory] = joinedKeys + directory
	}
	return fun.Values(directoryKeys).([]string)
}
Ejemplo n.º 2
0
Archivo: kv.go Proyecto: ahjdzx/traefik
func (provider *KvProvider) loadConfig() *Configuration {
	configuration := new(Configuration)
	templateObjects := struct {
		Prefix string
	}{
		provider.Prefix,
	}
	var KvFuncMap = template.FuncMap{
		"List": func(keys ...string) []string {
			joinedKeys := strings.Join(keys, "")
			keysPairs, err := provider.kvclient.List(joinedKeys)
			if err != nil {
				log.Error("Error getting keys: ", joinedKeys, err)
				return nil
			}
			directoryKeys := make(map[string]string)
			for _, key := range keysPairs {
				directory := strings.Split(strings.TrimPrefix(key.Key, strings.TrimPrefix(joinedKeys, "/")), "/")[0]
				directoryKeys[directory] = joinedKeys + directory
			}
			return fun.Values(directoryKeys).([]string)
		},
		"Get": func(keys ...string) string {
			joinedKeys := strings.Join(keys, "")
			keyPair, err := provider.kvclient.Get(joinedKeys)
			if err != nil {
				log.Debug("Error getting key: ", joinedKeys, err)
				return ""
			} else if keyPair == nil {
				return ""
			}
			return string(keyPair.Value)
		},
		"Last": func(key string) string {
			splittedKey := strings.Split(key, "/")
			return splittedKey[len(splittedKey)-1]
		},
	}

	tmpl := template.New(provider.Filename).Funcs(KvFuncMap)
	if len(provider.Filename) > 0 {
		_, err := tmpl.ParseFiles(provider.Filename)
		if err != nil {
			log.Error("Error reading file", err)
			return nil
		}
	} else {
		buf, err := Asset("templates/kv.tmpl")
		if err != nil {
			log.Error("Error reading file", err)
		}
		_, err = tmpl.Parse(string(buf))
		if err != nil {
			log.Error("Error reading file", err)
			return nil
		}
	}

	var buffer bytes.Buffer

	err := tmpl.Execute(&buffer, templateObjects)
	if err != nil {
		log.Error("Error with kv template:", err)
		return nil
	}

	if _, err := toml.Decode(buffer.String(), configuration); err != nil {
		log.Error("Error creating kv configuration:", err)
		log.Error(buffer.String())
		return nil
	}

	return configuration
}