feat(cockpit/console): display cmds from cockpit in embark console

This commit is contained in:
Jonathan Rainville 2019-02-26 11:31:29 -05:00
parent 46221a3bc0
commit e3396417da
3 changed files with 17 additions and 24 deletions

View File

@ -19,8 +19,8 @@ function action(type, payload = {}) {
export const AUTHENTICATE = createRequestTypes('AUTHENTICATE');
export const authenticate = {
request: (host, token) => action(AUTHENTICATE[REQUEST], {host, token}),
success: (result, payload) => {
return action(AUTHENTICATE[SUCCESS], {host: payload.host, token: result.token})
success: (result, payload) => {
return action(AUTHENTICATE[SUCCESS], {host: payload.host, token: result.token})
},
failure: (error) => action(AUTHENTICATE[FAILURE], {error})
};
@ -121,17 +121,7 @@ export const COMMANDS = createRequestTypes('COMMANDS');
export const commands = {
post: (command) => action(COMMANDS[REQUEST], {command}),
success: (command, payload) => {
return action(COMMANDS[SUCCESS], {
processLogs: [
{
timestamp: new Date().getTime(),
name: EMBARK_PROCESS_NAME,
msg: `${ansiToHtml(command.result || '')}`,
command: `console> ${payload.command}<br>`,
result: command.result
}
]
});
return action(COMMANDS[SUCCESS], {});
},
failure: (error) => action(COMMANDS[FAILURE], {error})
};

View File

@ -57,10 +57,9 @@ class Console extends Component {
);
}
isJsonObject(item) {
if (!item.result) return false;
isJsonObject(msg) {
try {
return typeof (JSON.parse(item.result)) === 'object';
return typeof (JSON.parse(msg)) === 'object';
} catch(_err) {
return false;
}
@ -87,17 +86,18 @@ class Console extends Component {
.filter((item) => item.name === process.name)
.reverse()
.map((item, i) => {
const msg = item.result || item.msg;
if (this.isJsonObject(item)) {
if (this.isJsonObject(msg)) {
return(
<div>
<p key={i} className={this.logClassName(item)} dangerouslySetInnerHTML={{__html: (convert.toHtml(item.command || ""))}}></p>
<ReactJson src={JSON.parse(item.result)} theme="monokai" sortKeys={true} collapsed={1} />
<div key={`message-${i}`}>
<p className={this.logClassName(item)} dangerouslySetInnerHTML={{__html: (convert.toHtml(item.command || ""))}}/>
<ReactJson src={JSON.parse(msg)} theme="monokai" sortKeys={true} collapsed={1} />
</div>
);
}
return (
<p key={i} className={this.logClassName(item)} dangerouslySetInnerHTML={{__html: (convert.toHtml(item.command || "") + convert.toHtml(item.msg))}}></p>
<p key={i} className={this.logClassName(item)} dangerouslySetInnerHTML={{__html: (convert.toHtml(item.command || "") + convert.toHtml(msg))}}/>
);
})
}

View File

@ -94,14 +94,17 @@ class Console {
private registerApi() {
const plugin = this.plugins.createPlugin("consoleApi", {});
plugin.registerAPICall("post", "/embark-api/command", (req: any, res: any) => {
this.logger.info(`Cockpit> ${req.body.command}`.cyan);
this.executeCmd(req.body.command, (err: any, result: any) => {
if (err) {
return res.send({ result: err.message || err });
}
if (typeof result === "string") {
return res.send({ result });
let response = result;
if (typeof result !== "string") {
response = stringify(result, utils.jsonFunctionReplacer, 2);
}
res.send({ result: stringify(result, utils.jsonFunctionReplacer, 2) });
this.logger.info(response);
return res.send({ result: response });
});
});
}