Merge branch 'connect-command-spawn' into feature-connect-modals

This commit is contained in:
Susmitha Girumala authored and GitHub committed 2020-12-09 16:33:51 -05:00
commit 45f82764df
4 files changed
+22 -25

No files matched your search

@@ -48,11 +48,16 @@ export default class ScopesScopeProjectsTargetsRoute extends Route {
@action
async connect(model) {
try {
await this.ipc.invoke('cli');
const connection = await this.ipc.invoke('connect', {
// Check for cli
const cliPath = await this.ipc.invoke('cliPath');
if(!cliPath) throw new Error('Cannot find boundary cli.')
// Create target session
const connection = this.ipc.invoke('connect', {
target_id: model.target.id,
token: this.session.data.authenticated.token,
});
// Show the user a modal with basic connection info.
this.confirm.confirm(connection, { isConnectSuccess: true });
} catch(e) {
@@ -34,6 +34,7 @@
<Rose::Badge @style='informational'>
{{model.project.name}}
</Rose::Badge>
<p>{{model.project.id}}</p>
</row.cell>
<row.cell>
<Rose::Button @style="secondary" {{on "click" (route-action "connect" model)}}>
+2 -2
View File
@@ -21,9 +21,9 @@ handle('getOrigin', () => origin);
handle('setOrigin', (requestOrigin) => origin = requestOrigin);
/**
* Detect boundary cli in env path
* Detect boundary cli path
*/
handle('cli', async () => {
handle('cliPath', async () => {
const cliPath = await lookpath('boundary');
if(!cliPath) throw new Error({ message: 'Cannot find boundary cli.'})
return cliPath;
+12 -21
View File
@@ -1,17 +1,17 @@
const { spawn } = require('child_process');
/**
* Attempt to parse output string to JSON.
* When JSON is successfully parsed, consider stdout fully read.
*/
// TODO: Potential use for BufferList?
const resolveOutput = (resolve, outputStream) => {
// Verify json
const isJSON = (data) => {
let jsonData;
if(typeof data !== 'string') data = JSON.stringify(data)
try {
resolve(JSON.parse(outputStream));
jsonData = JSON.parse(data);
} catch (e) {
// Ignore JSON parse errors while stdout buffers.
// Ignore parse errors
}
};
return (typeof jsonData === 'object');
}
// You can throw exceptions, or allow them to occur, and this is supported.
// Exceptions thrown in this way will be returned to the UI as an
@@ -27,15 +27,6 @@ const resolveOutput = (resolve, outputStream) => {
// error from the underlying exec implementation. It's not a very
// helpful message for users, so it's recommended to craft nice
// POJO representation and throw it or promise->reject it.
const rejectError = (reject, message) => {
try {
const errorMessage = JSON.parse(message);
reject(new Error(errorMessage.error));
} catch (e) {
// Ignore JSON parse errors while stderr buffers.
}
};
module.exports = function spawnPromise (command) {
return new Promise((resolve, reject) => {
const childProcess = spawn('boundary', command);
@@ -44,12 +35,12 @@ module.exports = function spawnPromise (command) {
childProcess.stdout.on('data', (data) => {
outputStream += data.toString();
resolveOutput(resolve, outputStream);
if(isJSON(outputStream)) resolve(JSON.parse(outputStream));
});
childProcess.stderr.on('data', (data) => {
errorStream += data.toString();
rejectError(reject, errorStream);
if(isJSON(errorStream)) reject(new Error(JSON.parse(errorStream)?.error));
});
});
}
}