-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.py
200 lines (177 loc) · 6.35 KB
/
redis.py
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import json
import re
import sys
import os
from BaseServer import BaseServer
# 环境变量
separator = os.environ.get("separator", " | ")
dataFileName = os.environ.get("REDIS_DATA_FILE_NAME", "data/redis-data.json")
class Server(BaseServer):
data = []
workFlowPath = os.getcwd().replace(" ", "\\ ")
args = []
subtitle = {
'operate': 'Tips: input <Enter> to use | <Shift> to delete | <Cmd> to copy!',
'addTip': 'Please add data by this format: redis add server-name ip [port] [db] [pass]'
}
queryList = []
def __init__(self, serverDataFile):
super().__init__(serverDataFile)
self.data = self.getJsonDataByFile()
self.args = [
f"/usr/bin/expect {self.workFlowPath}/login.expect",
]
def run(self, methodName, *args, **kwargs):
method = getattr(self, methodName, None)
if method and callable(method):
result = method(*args, **kwargs)
print(result, end='')
else:
raise AttributeError(f"Method {methodName} not found")
def delete(self, keywordIndex):
keywordIndex = int(keywordIndex)
try:
item = self.data[keywordIndex]
del self.data[keywordIndex]
# sort
self.data = sorted(self.data, key=lambda x: x['name'])
# save
self.setJsonDataByFile(self.data)
output = [
item['name'],
item['host'],
]
if 'port' in item:
output.append(item['port'])
if 'db' in item:
output.append(item['db'])
if 'auth' in item:
output.append(item['auth'])
return "\n".join(output)
except IndexError:
return 'Delete Failed! Server may be remove!'
def copy(self, keywordIndex):
keywordIndex = int(keywordIndex)
try:
item = self.data[keywordIndex]
output = [
item['name'],
item['host'],
]
if 'port' in item:
output.append(item['port'])
if 'db' in item:
output.append(item['db'])
if 'auth' in item:
output.append(item['auth'])
return "\n".join(output)
except IndexError:
return 'Copy Failed! Server may be remove!'
def add(self, info, addLabel="add"):
info = info.split(addLabel)[1]
serviceArr = info.strip().split(" ")
item = {}
if len(serviceArr) < 2:
return False
if "redis-cli" in info:
item = {
"name": serviceArr.pop(0),
}
item['host'] = " ".join(serviceArr)
elif len(serviceArr) > 3:
item = {
"name": serviceArr[0],
"host": serviceArr[1],
"port": serviceArr[2],
"db": serviceArr[3],
}
if len(serviceArr) > 4:
item['auth'] = serviceArr[4]
elif len(serviceArr) == 3:
item = {
"name": serviceArr[0],
"host": serviceArr[1],
"port": "6379",
"db": "0",
"auth": serviceArr[2],
}
else:
item = {
"name": serviceArr[0],
"host": serviceArr[1],
}
self.data.append(item)
# sort
self.data = sorted(self.data, key=lambda x: x['name'])
# save
self.setJsonDataByFile(self.data)
if 'port' in item:
self.args.append("redis-cli")
if 'host' in item:
self.args.append(f"-h {item['host']}")
if 'port' in item:
self.args.append(f"-p {item['port']}")
if 'db' in item:
self.args.append(f"-n {item['db']}")
if 'auth' in item:
self.args.append(f"--pass {item['auth']}")
self.args = [self.args.pop(0), "'" + " ".join(self.args) + "'"]
else:
self.args.append(f"'{item['host']}'")
return " ".join(self.args)
def get(self, keyword):
queryList = []
for (key, item) in enumerate(self.data):
if 'name' not in item:
continue
if keyword in item['name'] or keyword in item['host'] or ('port' in item and keyword in item['port']):
title = [item['name']]
if 'port' in item:
title.append(f"-h {item['host']} -p {item['port']}")
else:
title.append(f"{item['host']}")
queryList.append({
'uid': key,
'arg': key,
'title' : separator.join(title),
'subtitle' : self.subtitle['operate'],
'valid' : True,
'icon' : {
'path': 'icon.png'
},
})
if len(queryList) == 0:
item = {
'uid': 'codezm',
'arg': keyword,
'title': 'Auto login by redis-cli - Tips: You haven\'t added any thing.',
'subtitle': self.subtitle['addTip'],
'valid': False,
}
if "add" in keyword:
item['title'] = 'Input <enter> to save.'
item['valid'] = True
queryList.append(item)
return json.dumps({ 'items': queryList })
def getByIndex(self, keywordIndex):
keywordIndex = int(keywordIndex)
try:
item = self.data[keywordIndex]
if 'port' in item:
self.args.append(f"redis-cli -h {item['host']} -p {item['port']}")
if 'db' in item:
self.args.append(f"-n {item['db']}")
if 'auth' in item:
self.args.append(f"--pass {item['auth']}")
self.args = [self.args.pop(0), "'" + " ".join(self.args) + "'"]
else:
self.args.append(f"'" + item['host'] + "'")
return " ".join(self.args)
except IndexError:
return 'Execute Failed! Server may be remove!'
# 获取命令行参数
args = sys.argv
if args[1] == "add" and "add" not in args[2]:
args[1] = 'getByIndex'
Server(dataFileName).run(args[1], args[2])
exit()