-
Notifications
You must be signed in to change notification settings - Fork 6
/
keystore_keys.go
53 lines (46 loc) · 1.14 KB
/
keystore_keys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package object
import (
"path/filepath"
"sort"
"github.com/danwakefield/fnmatch"
"github.com/opensvc/om3/util/xmap"
)
// MatchingDirs returns the list of all directories and parent directories
// hosting keys in the store's virtual filesystem.
//
// Example: []key{"a/b/c", "a/c/b"} => []dir{"a", "a/b", "a/c"}
func (t *keystore) MatchingDirs(pattern string) ([]string, error) {
m := make(map[string]interface{})
keys, err := t.MatchingKeys(pattern)
if err != nil {
return []string{}, err
}
for _, k := range keys {
for {
k = filepath.Dir(k)
if k == "" || k == "/" || k == "." {
break
}
m[k] = nil
}
}
dirs := xmap.Keys(m)
sort.Strings(dirs)
return dirs, nil
}
func (t *keystore) AllDirs() ([]string, error) {
return t.MatchingDirs("")
}
func (t *keystore) AllKeys() ([]string, error) {
return t.MatchingKeys("")
}
func (t *keystore) MatchingKeys(pattern string) ([]string, error) {
data := make([]string, 0)
f := fnmatch.FNM_PATHNAME | fnmatch.FNM_LEADING_DIR
for _, s := range t.config.Keys(dataSectionName) {
if pattern == "" || fnmatch.Match(pattern, s, f) {
data = append(data, s)
}
}
return data, nil
}