forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
directory.go
51 lines (42 loc) · 1.03 KB
/
directory.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
package consultopo
import (
"path"
"strings"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/vt/topo"
)
// ListDir is part of the topo.Backend interface.
func (s *Server) ListDir(ctx context.Context, cell, dirPath string) ([]string, error) {
c, err := s.clientForCell(ctx, cell)
if err != nil {
return nil, err
}
nodePath := path.Join(c.root, dirPath) + "/"
keys, _, err := c.kv.Keys(nodePath, "", nil)
if err != nil {
return nil, err
}
if len(keys) == 0 {
// No key starts with this prefix, means the directory
// doesn't exist.
return nil, topo.ErrNoNode
}
prefixLen := len(nodePath)
var result []string
for _, p := range keys {
// Remove the prefix, base path.
if !strings.HasPrefix(p, nodePath) {
return nil, ErrBadResponse
}
p = p[prefixLen:]
// Keep only the part until the first '/'.
if i := strings.Index(p, "/"); i >= 0 {
p = p[:i]
}
// Remove duplicates, add to list.
if len(result) == 0 || result[len(result)-1] != p {
result = append(result, p)
}
}
return result, nil
}