-
Notifications
You must be signed in to change notification settings - Fork 383
/
exec.ts
87 lines (76 loc) · 1.64 KB
/
exec.ts
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
import { getLogger } from 'pinus-logger';
let logger = getLogger(__filename);
import * as util from '../util';
import { consts } from '../consts';
import * as cliff from 'cliff';
import * as fs from 'fs';
import { ICommand, AgentCommand } from '../command';
import { ReadLine } from 'readline';
import { AdminClient } from 'pinus-admin';
export default function (opts:object)
{
return new Command(opts);
};
export let commandId = 'exec';
export let helpCommand = 'help exec';
export class Command implements ICommand
{
constructor(opts:object)
{
}
handle(agent: AgentCommand, comd: string, argv: string, msg: {[key:string]: string}, rl: ReadLine, client: AdminClient): void
{
if (!comd)
{
agent.handle(helpCommand, msg, rl, client);
return;
}
let Context = agent.getContext();
if (Context === 'all')
{
util.log('\n' + consts.COMANDS_CONTEXT_ERROR + '\n');
rl.prompt();
return;
}
let argvs = util.argsFilter(argv);
if (argvs.length > 2)
{
agent.handle(helpCommand, msg, rl, client);
return;
}
let file = null;
if (comd[0] !== '/')
{
comd = process.cwd() + '/' + comd;
}
try
{
file = fs.readFileSync(comd).toString();
} catch (e)
{
util.log(consts.COMANDS_EXEC_ERROR);
rl.prompt();
return;
}
client.request('scripts', {
command: 'run',
serverId: Context,
script: file
}, function (err: Error, msg: any)
{
if (err) console.log(err);
else
{
try
{
msg = <{ msg: { [key: string]: any }}>JSON.parse(msg);
util.formatOutput(commandId, msg);
} catch (e)
{
util.log('\n' + msg + '\n');
}
}
rl.prompt();
});
}
}