-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathcli.ts
More file actions
144 lines (134 loc) · 3.4 KB
/
cli.ts
File metadata and controls
144 lines (134 loc) · 3.4 KB
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
import * as readline from 'readline';
import { AdminClient } from 'pinus-admin';
import * as command from './command';
import { consts } from './consts';
import * as util from './util';
import { argv } from 'optimist';
let username = argv['u'] = argv['u'] || 'monitor';
let password = argv['p'] = argv['p'] || 'monitor';
let host = argv['h'] = argv['h'] || 'localhost';
let port = argv['P'] = argv['P'] || 3005;
let context = 'all';
let client:AdminClient = null;
export default function doConnect()
{
client = new AdminClient({
username: username,
password: password,
md5: true
});
let id = 'pomelo_cli_' + Date.now();
client.connect(id, host, port, function (err: Error)
{
if (err)
{
util.log('\n' + err + '\n');
process.exit(0);
} else
{
let ASCII_LOGO = consts.ASCII_LOGO;
for (let i = 0; i < ASCII_LOGO.length; i++)
{
util.log(ASCII_LOGO[i]);
}
let WELCOME_INFO = consts.WELCOME_INFO;
for (let i = 0, l = WELCOME_INFO.length; i < l; i++)
{
util.log(WELCOME_INFO[i]);
}
startCli();
}
});
client.on('close', function ()
{
client.socket.disconnect();
util.log('\ndisconnect from master');
process.exit(0);
});
}
function startCli()
{
let rl = readline.createInterface(process.stdin, process.stdout, completer);
let PROMPT = username + consts.PROMPT + context + '>';
rl.setPrompt(PROMPT);
rl.prompt();
let rootCommand = command.default();
rl.on('line', function (line)
{
let key = line.trim();
if (!key)
{
util.help();
rl.prompt();
return;
}
switch (key)
{
case 'help':
util.help();
rl.prompt();
break;
case '?':
util.help();
rl.prompt();
break;
case 'quit':
rootCommand.quit(rl);
break;
case 'kill':
rootCommand.kill(rl, client);
break;
default:
rootCommand.handle(key, {
user: username
}, rl, client);
break;
}
}).on('close', function ()
{
util.log('bye ' + username);
process.exit(0);
});
}
function completer(line: string)
{
line = line.trim();
let completions = consts.COMANDS_COMPLETE;
let hits = [];
// commands tab for infos
if (consts.COMPLETE_TWO[line as keyof typeof consts.COMPLETE_TWO])
{
if (line === "show")
{
for (let k in consts.SHOW_COMMAND)
{
hits.push(k);
}
} else if (line === "help")
{
for (let k in consts.COMANDS_COMPLETE_INFO)
{
hits.push(k);
}
} else if (line === "enable" || line === "disable")
{
hits.push("app");
hits.push("module");
} else if (line === "dump")
{
hits.push("memory");
hits.push("cpu");
}
}
hits = util.tabComplete(hits, line, consts.COMANDS_COMPLETE_INFO, "complete");
hits = util.tabComplete(hits, line, consts.COMANDS_COMPLETE_INFO, "help");
hits = util.tabComplete(hits, line, consts.SHOW_COMMAND, "show");
hits = util.tabComplete(hits, line, null, "enable");
hits = util.tabComplete(hits, line, null, "disable");
hits = util.tabComplete(hits, line, null, "disable");
hits = util.tabComplete(hits, line, null, "dump");
hits = util.tabComplete(hits, line, null, "use");
hits = util.tabComplete(hits, line, null, "stop");
// show all completions if none found
return [hits.length ? hits : completions, line];
}