Skip to content

Commit

Permalink
新增redis命令行功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Medicean committed Jan 15, 2019
1 parent 03b5537 commit 5e2ca6d
Show file tree
Hide file tree
Showing 13 changed files with 2,544 additions and 1 deletion.
108 changes: 107 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const LANG_T = antSword['language']['toastr'];
// const Database = require('./libs/database');
const RedisUtil = require('./libs/redisutil');
const Core = require('./libs/core');

const WIN = require('ui/window');
const redis_commands = require('redis-commands');
class Plugin {
constructor(opt) {
antSword['test'] = this;
Expand Down Expand Up @@ -140,6 +141,12 @@ class Plugin {
action: ()=>{
this.getRedisKeys(arr[1].split(":")[0], new Buffer(arr[1].split(":")[1],'base64').toString());
}
}, {
text: LANG['list']['bmenu']['database']['terminal'],
icon: 'fa fa-terminal',
action: ()=>{
this.initRedisTerminal(arr[1].split(":")[0], new Buffer(arr[1].split(":")[1],'base64').toString());
}
}
], event);
break;
Expand Down Expand Up @@ -2355,6 +2362,105 @@ class Plugin {
});
});
}
// 创建用户 redis 命令行
initRedisTerminal(id, db) {
let that = this;
const conf = that.pluginconf[id];
that.plugincore.setHost(conf['host']);
let needpass = false;
let basecmd = "";
if (conf['passwd'].length > 0) {
basecmd += that.redisutil.makeCommand('AUTH', conf['passwd']);
needpass = true;
}
basecmd += that.redisutil.makeCommand('SELECT', `${db}`);

let win = new WIN({
title: `${LANG['terminal']['title']} - DB[${db}]`,
height: 544,
width: 820,
});
let hash = win.win.getId().split("_")[1];
win.win.attachHTMLString(`
<div
id="div_redisterminal_${hash}"
style="height:100%;margin:0;padding:0 5px 1px 5px;overflow:scroll;--size:1;"
></div>
`);
let banner = `${LANG['terminal']['banner']}`;
let promptStr = `[[b;cyan;]${conf['host']}][[b;#E80000;](${db})]> `;
let dom = $(`#div_redisterminal_${hash}`);
let terminal = dom.terminal( (cmd, term) => {
if (!cmd) { return false }
cmd = cmd.trim();
if (cmd === 'exit' || cmd === 'quit') { return win.close() }
if (cmd === 'cls' || cmd === 'clear') { return term.clear() }
if (cmd.toLowerCase().startsWith('select')){
toastr.warning(LANG['error']['notimplselect'], LANG_T['warning']);
return
}
term.pause();
let usercmd = basecmd + that.redisutil.makeCommand(...cmd.split(' '));
that.core.request({
_: that.plugincore.template[that.opt['type']](new Buffer(usercmd))
}).then((res)=>{
let ret = res['text'];
that.redisutil.parseResponse(that.plugincore.decode(ret),(valarr, errarr)=>{
var retval;
if(needpass) {
if(errarr.length != 3 || valarr.length != 3){
toastr.warning(LANG['error']['parseerr'],LANG_T['warning']);
that.redisutil.parser = that.redisutil.initParser();
return
}
if(errarr[0] != "") {
toastr.error(LANG['error']['auth'](errarr[0].toString()), LANG_T['error']);
return
}
if(errarr[2] != "") {
toastr.error(errarr[2].toString(), LANG_T['error']);
return
}
retval = valarr[2];
}else{
if(errarr.length != 2 || valarr.length != 2){
toastr.warning(LANG['error']['parseerr'],LANG_T['warning']);
that.redisutil.parser = that.redisutil.initParser();
return
}
if(errarr[1] != "") {
toastr.error(errarr[1].toString(), LANG_T['error']);
return
}
retval = valarr[1];
}
let output = "";
if (retval instanceof Array){
retval.forEach((_val, _index)=>{
output += `${_index+1}) ${_val.toString()}\n`;
});
output = output.slice(0,-1);
}else if(retval instanceof Buffer){
output = retval.toString();
}
term.echo(
antSword.noxss(output, false)
);
});
term.set_prompt(promptStr);
term.resume();
});
}, {
greetings: banner,
name: `redisterminal_${hash}`,
prompt: promptStr,
// numChars: 100,
exit: false,
completion: (value, callback) => {
callback(redis_commands.list)
},
});
}
resetView() {
this.KeyBinaryData = new Buffer('');
this.detail.toolbar.setItemText('data_size', 'Size:0b');
Expand Down
6 changes: 6 additions & 0 deletions language/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
database: {
addkey: 'New Key',
reload: 'Refresh',
terminal: 'Terminal',
}
}
},
Expand Down Expand Up @@ -106,6 +107,7 @@ module.exports = {
getkeys: (err) => antSword.noxss(`Getting Key failed,error msg: ${err}`),
notimpl: (typevalue) => antSword.noxss(`Type ${typevalue} not support.`),
parseerr: `Parse response error, please try again.`,
notimplselect: 'Does not support the SELECT DB in the terminal',
},
add: {
form: {
Expand Down Expand Up @@ -139,4 +141,8 @@ module.exports = {
confirm: 'Are you sure you want to delete this configuration?',
},
},
terminal: {
title: 'Redis Terminal',
banner: `Usage:\n\n 1)Press the [Tab] key twice to automatically complete the command;\n 2)Temporarily does not support switching db under the terminal;\n 3)After executing the command, the data may not be returned normally, and try several times;`,
},
}
6 changes: 6 additions & 0 deletions language/zh.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
database: {
addkey: '新增Key',
reload: '刷新',
terminal: '执行指令',
}
}
},
Expand Down Expand Up @@ -106,6 +107,7 @@ module.exports = {
getkeys: (err) => antSword.noxss(`获取 Key 失败,原因: ${err}`),
notimpl: (typevalue) => antSword.noxss(`暂不支持${typevalue}类型的Key`),
parseerr: `解析数据失败,请重试`,
notimplselect: '暂不支持在终端下切换 DB',
},
add: {
form: {
Expand Down Expand Up @@ -139,4 +141,8 @@ module.exports = {
confirm: '确定要删除该配置吗?',
},
},
terminal: {
title: 'Redis 虚拟命令行',
banner: `使用帮助:\n\n 1)快速按两下[Tab]键自动补全指令;\n 2)暂不支持在终端下切换db;\n 3)执行命令后可能无法正常返回数据,多尝试几次;`,
},
}
9 changes: 9 additions & 0 deletions node_modules/redis-commands/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions node_modules/redis-commands/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions node_modules/redis-commands/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions node_modules/redis-commands/changelog.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5e2ca6d

Please sign in to comment.