-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathcommand.ts
More file actions
99 lines (88 loc) · 1.8 KB
/
Copy pathcommand.ts
File metadata and controls
99 lines (88 loc) · 1.8 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
import { consts } from './consts';
import * as util from './util';
import * as cliff from 'cliff';
import * as fs from 'fs';
import { isFunction } from 'util';
import { AdminClient } from 'pinus-admin';
import { ReadLine } from 'readline';
export interface ICommand
{
handle(agent: AgentCommand, comd: string, argv: string, msg: {[key:string]: string}, rl: ReadLine, client: AdminClient): void;
}
export class AgentCommand
{
commands: {[key:string]:any} = {};
Context = 'all';
constructor()
{
this.init();
}
init()
{
let self:AgentCommand = this;
fs.readdirSync(__dirname + '/commands').forEach(function (filename)
{
if (/\.js$/.test(filename))
{
let name = filename.substr(0, filename.lastIndexOf('.'));
let _command = require('./commands/' + name).default;
if (isFunction(_command))
{
self.commands[name] = _command;
}
}
});
}
handle(argv: string, msg: any, rl: ReadLine, client: AdminClient): void
{
let self = this;
let argvs = util.argsFilter(argv);
let comd = argvs[0];
let comd1 = argvs[1] || "";
comd1 = comd1.trim();
let m = this.commands[comd];
if (m)
{
let _command = m();
_command.handle(self, comd1, argv, rl, client, msg);
} else
{
util.errorHandle(argv, rl);
}
}
quit(rl: ReadLine)
{
rl.emit('close');
}
kill(rl:ReadLine, client:AdminClient)
{
rl.question(consts.KILL_QUESTION_INFO, function (answer)
{
if (answer === 'yes')
{
client.request(consts.CONSOLE_MODULE, {
signal: "kill"
}, function (err: Error, data: any)
{
if (err) console.log(err);
rl.prompt();
});
} else
{
rl.prompt();
}
});
}
getContext()
{
return this.Context;
}
setContext(context: string)
{
this.Context = context;
}
}
export default function ()
{
return new AgentCommand();
}