forked from st3v/go-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
68 lines (56 loc) · 1.23 KB
/
util.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
package zookeeper
import (
"encoding/json"
"path"
"strings"
"github.com/micro/go-micro/registry"
"github.com/samuel/go-zookeeper/zk"
)
func encode(s *registry.Service) ([]byte, error) {
return json.Marshal(s)
}
func decode(ds []byte) (*registry.Service, error) {
var s *registry.Service
err := json.Unmarshal(ds, &s)
return s, err
}
func nodePath(s, id string) string {
service := strings.Replace(s, "/", "-", -1)
node := strings.Replace(id, "/", "-", -1)
return path.Join(prefix, service, node)
}
func servicePath(s string) string {
return path.Join(prefix, strings.Replace(s, "/", "-", -1))
}
func createPath(path string, data []byte, client *zk.Conn) error {
exists, _, err := client.Exists(path)
if err != nil {
return err
}
if exists {
return nil
}
name := "/"
p := strings.Split(path, "/")
for _, v := range p[1 : len(p)-1] {
name += v
e, _, _ := client.Exists(name)
if !e {
_, err = client.Create(name, []byte{}, int32(0), zk.WorldACL(zk.PermAll))
if err != nil {
return err
}
}
name += "/"
}
_, err = client.Create(path, data, int32(0), zk.WorldACL(zk.PermAll))
return err
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}