refactor(cli): make --robot option output JSON (#500)

The Etcher CLI `--robot` option outputs space separated lines including
useful information in a predefined order, for example:

```sh
progress write 50% 15s 65536
```

While this is very easy to parse, a stringified JSON line is much more
convenient since:

- We don't have to write any parsing logic.
- We don't have to guess the value types (string, number, boolean, etc).
- We don't have to hardcode property names.
- We don't have to extend the parsing code if we decide to send new
commands from the CLI, or make any change to the data we output.

Signed-off-by: Juan Cruz Viotti <jviottidc@gmail.com>
This commit is contained in:
Juan Cruz Viotti 2016-06-22 10:40:17 -04:00 committed by GitHub
parent 1e3db916e6
commit acde20cee1
3 changed files with 23 additions and 56 deletions

View File

@ -64,13 +64,15 @@ form.run([
}, function(state) {
if (options.robot) {
log.toStdout([
'progress',
state.type,
Math.floor(state.percentage) + '%',
state.eta + 's',
Math.floor(state.speed)
].join(' '));
log.toStdout(JSON.stringify({
command: 'progress',
data: {
type: state.type,
percentage: Math.floor(state.percentage),
eta: state.eta,
speed: Math.floor(state.speed)
}
}));
} else {
progressBars[state.type].update(state);
}
@ -79,7 +81,13 @@ form.run([
}).then(function(results) {
if (options.robot) {
log.toStdout(`done ${results.passedValidation} ${results.sourceChecksum}`);
log.toStdout(JSON.stringify({
command: 'done',
data: {
passedValidation: results.passedValidation,
sourceChecksum: results.sourceChecksum
}
}));
} else {
if (results.passedValidation) {
console.log('Your flash is complete!');
@ -98,7 +106,12 @@ form.run([
}).catch(function(error) {
if (options.robot) {
log.toStderr(error.message);
log.toStderr(JSON.stringify({
command: 'error',
data: {
message: error.message
}
}));
} else {
utils.printError(error);
}

View File

@ -137,45 +137,3 @@ exports.getTemporaryLogFilePath = function() {
return logFilePath;
});
};
/**
* @summary Parse an output line from the Etcher CLI
* @function
* @public
*
* @description
* This function parses an output line for when the CLI
* was called with the `--robot` argument.
*
* @param {String} line - line
* @returns {(Object|Undefined)} parsed output
*
* @example
* const data = utils.parseEtcherCLIRobotLine('progress write 50% 0.5s 400');
*/
exports.parseEtcherCLIRobotLine = function(line) {
const data = line.split(' ');
const command = _.first(data);
if (command === 'progress') {
return {
command: command,
data: {
type: _.nth(data, 1),
percentage: _.parseInt(_.nth(data, 2)),
eta: _.parseInt(_.nth(data, 3)),
speed: _.parseInt(_.nth(data, 4))
}
};
}
if (command === 'done') {
return {
command: command,
data: {
passedValidation: _.nth(data, 1) === 'true',
sourceChecksum: _.nth(data, 2)
}
};
}
};

View File

@ -72,11 +72,7 @@ return isElevated().then(function(elevated) {
});
tail.on('line', function(line) {
const data = utils.parseEtcherCLIRobotLine(line);
if (data) {
process.send(data);
}
process.send(JSON.parse(line));
});
}