-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetcd.go
139 lines (116 loc) · 2.67 KB
/
etcd.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package lua
import (
"context"
"os"
log "github.com/sirupsen/logrus"
"github.com/yuin/gopher-lua"
clientv3 "go.etcd.io/etcd/client/v3"
msgstore "github.com/numkem/msgscript/store"
)
func PreloadEtcd(L *lua.LState) {
L.PreloadModule("etcd", etcdLoader)
}
func etcdLoader(L *lua.LState) int {
c, err := msgstore.EtcdClient("127.0.0.1:2379") // Set a default value
if err != nil {
log.WithField("endpoints", os.Getenv("ETCD_ENDPOINTS")).Errorf("failed to connect to etcd: %v", err)
}
l := luaEtcd{client: c}
mod := L.SetFuncs(L.NewTable(), map[string]lua.LGFunction{
"put": l.Put,
"get": l.Get,
"delete": l.Delete,
})
mt := L.NewTypeMetatable("EtcdKV")
L.SetGlobal("EtcdKV", mt)
L.SetField(mt, "new", L.NewFunction(newEtcdKV))
L.SetField(mt, "__index", L.SetFuncs(L.NewTable(), map[string]lua.LGFunction{
"getKey": luaEtcdKvGetKey,
"getValue": luaEtcdKvGetValue,
}))
L.Push(mod)
return 1
}
func luaEtcdKvGetKey(L *lua.LState) int {
L.Push(lua.LString(L.CheckUserData(1).Value.(*luaEtcdKVs).Key))
return 1
}
func luaEtcdKvGetValue(L *lua.LState) int {
L.Push(lua.LString(L.CheckUserData(1).Value.(*luaEtcdKVs).Value))
return 1
}
func newEtcdKV(L *lua.LState) int {
key := L.CheckString(1)
value := L.CheckString(2)
kv := &luaEtcdKVs{
Key: key,
Value: value,
}
ud := L.NewUserData()
ud.Value = kv
L.SetMetatable(ud, L.GetTypeMetatable("EtcdKV"))
L.Push(ud)
return 1
}
type luaEtcd struct {
client *clientv3.Client
}
type luaEtcdKVs struct {
Key string
Value string
}
func (l *luaEtcd) Get(L *lua.LState) int {
key := L.CheckString(1)
prefix := L.CheckBool(2)
var resp *clientv3.GetResponse
var err error
if prefix {
resp, err = l.client.Get(context.TODO(), key, clientv3.WithPrefix())
} else {
resp, err = l.client.Get(context.TODO(), key)
}
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
if resp.Count == 0 {
L.Push(lua.LNil)
L.Push(lua.LNil)
return 2
}
kvTable := L.NewTable()
for _, kv := range resp.Kvs {
ud := L.NewUserData()
ud.Value = &luaEtcdKVs{
Key: string(kv.Key),
Value: string(kv.Value),
}
L.SetMetatable(ud, L.GetTypeMetatable("EtcdKV"))
kvTable.Append(ud)
}
L.Push(kvTable)
L.Push(lua.LNil)
return 2
}
func (l *luaEtcd) Put(L *lua.LState) int {
key := L.CheckString(1)
value := L.CheckString(2)
_, err := l.client.Put(context.TODO(), key, value)
if err != nil {
L.Push(lua.LString(err.Error()))
return 1
}
L.Push(lua.LNil)
return 1
}
func (l *luaEtcd) Delete(L *lua.LState) int {
key := L.CheckString(1)
_, err := l.client.Delete(context.TODO(), key)
if err != nil {
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(lua.LNil)
return 1
}