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 }
} 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) return keysPairs }, "Get": func(keys ...string) string { joinedKeys := strings.Join(keys, "") keyPair, _, err := kvClient.Get(joinedKeys, nil)
func consulKeys(kv *api.KV, prefix string, lastIndex uint64) (interface{}, *api.QueryMeta, error) { return kv.Keys(prefix, "", &api.QueryOptions{WaitIndex: lastIndex}) }