cliserver: add RunCommandPipeArgs

This commit is contained in:
Martin Aeschlimann
2019-04-11 10:47:23 +02:00
parent b1a61225d3
commit f9f67997b9

View File

@@ -26,6 +26,12 @@ export interface StatusPipeArgs {
type: 'status';
}
export interface RunCommandPipeArgs {
type: 'command';
command: string;
args: string[];
}
export class CLIServer {
private _server: http.Server;
@@ -61,7 +67,7 @@ export class CLIServer {
req.setEncoding('utf8');
req.on('data', (d: string) => chunks.push(d));
req.on('end', () => {
const data: OpenCommandPipeArgs | StatusPipeArgs | any = JSON.parse(chunks.join(''));
const data: OpenCommandPipeArgs | StatusPipeArgs | RunCommandPipeArgs | any = JSON.parse(chunks.join(''));
switch (data.type) {
case 'open':
this.open(data, res);
@@ -69,6 +75,10 @@ export class CLIServer {
case 'status':
this.getStatus(data, res);
break;
case 'command':
this.runCommand(data, res)
.catch(console.error);
break;
default:
res.writeHead(404);
res.write(`Unkown message type: ${data.type}`, err => {
@@ -135,6 +145,28 @@ export class CLIServer {
}
}
private async runCommand(data: RunCommandPipeArgs, res: http.ServerResponse) {
try {
const { command, args } = data;
const result = await this._commands.executeCommand(command, ...args);
res.writeHead(200);
res.write(JSON.stringify(result), err => {
if (err) {
console.error(err);
}
});
res.end();
} catch (err) {
res.writeHead(500);
res.write(String(err), err => {
if (err) {
console.error(err);
}
});
res.end();
}
}
dispose(): void {
this._server.close();
@@ -142,4 +174,4 @@ export class CLIServer {
fs.unlinkSync(this._ipcHandlePath);
}
}
}
}