forked from jigish/gozk-recipes
/
helpers.go
74 lines (68 loc) · 1.71 KB
/
helpers.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package recipes
import (
"errors"
"fmt"
gozk "github.com/scalingdata/gozk"
"path"
"strings"
)
// Touch + Set data to a node
func (zk *ZkConn) TouchAndSet(nodePath, data string) (*gozk.Stat, error) {
zk.Touch(nodePath)
return zk.Set(nodePath, data, -1)
}
// Create a node if it doesn't exist, otherwise do nothing
func (zk *ZkConn) Touch(nodePath string) (string, error) {
if stat, err := zk.Exists(nodePath); err == nil && stat != nil {
return nodePath, nil
}
// touch all directores above nodePath because zookeeper sucks
dir := path.Dir(nodePath)
if dir != "/" && dir != "." {
zk.Touch(dir)
}
return zk.Create(nodePath, "", 0, gozk.WorldACL(gozk.PERM_ALL))
}
/*
* recursive wrapper around Delete
*/
func (zk *ZkConn) RecursiveDelete(dirPath string) error {
if stat, err := zk.Exists(dirPath); err != nil || stat == nil {
return errors.New(fmt.Sprintf("Node at %s does not exist!", dirPath))
}
children, _, err := zk.Children(dirPath)
if err != nil {
return errors.New(fmt.Sprintf("Unable to list children. Error: %s.", err.Error()))
}
for _, child := range children {
if err := zk.RecursiveDelete(dirPath + "/" + child); err != nil {
return err
}
}
return zk.Delete(dirPath, -1)
}
/*
* Filters out hidden nodes
*/
func FilterHidden(nodes []string) []string {
if nodes == nil || len(nodes) == 0 {
return nodes
}
out := []string{}
for _, item := range nodes {
if !strings.HasPrefix(item, ".") {
out = append(out, item)
}
}
return out
}
/*
* Lists only those children that are not hidden
*/
func (zk *ZkConn) VisibleChildren(dir string) ([]string, *gozk.Stat, error) {
list, stat, err := zk.Children(dir)
if err != nil {
return list, stat, err
}
return FilterHidden(list), stat, nil
}