mirror of
https://github.com/balena-io/etcher.git
synced 2025-07-26 20:56:34 +00:00
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:
parent
1e3db916e6
commit
acde20cee1
@ -64,13 +64,15 @@ form.run([
|
|||||||
}, function(state) {
|
}, function(state) {
|
||||||
|
|
||||||
if (options.robot) {
|
if (options.robot) {
|
||||||
log.toStdout([
|
log.toStdout(JSON.stringify({
|
||||||
'progress',
|
command: 'progress',
|
||||||
state.type,
|
data: {
|
||||||
Math.floor(state.percentage) + '%',
|
type: state.type,
|
||||||
state.eta + 's',
|
percentage: Math.floor(state.percentage),
|
||||||
Math.floor(state.speed)
|
eta: state.eta,
|
||||||
].join(' '));
|
speed: Math.floor(state.speed)
|
||||||
|
}
|
||||||
|
}));
|
||||||
} else {
|
} else {
|
||||||
progressBars[state.type].update(state);
|
progressBars[state.type].update(state);
|
||||||
}
|
}
|
||||||
@ -79,7 +81,13 @@ form.run([
|
|||||||
}).then(function(results) {
|
}).then(function(results) {
|
||||||
|
|
||||||
if (options.robot) {
|
if (options.robot) {
|
||||||
log.toStdout(`done ${results.passedValidation} ${results.sourceChecksum}`);
|
log.toStdout(JSON.stringify({
|
||||||
|
command: 'done',
|
||||||
|
data: {
|
||||||
|
passedValidation: results.passedValidation,
|
||||||
|
sourceChecksum: results.sourceChecksum
|
||||||
|
}
|
||||||
|
}));
|
||||||
} else {
|
} else {
|
||||||
if (results.passedValidation) {
|
if (results.passedValidation) {
|
||||||
console.log('Your flash is complete!');
|
console.log('Your flash is complete!');
|
||||||
@ -98,7 +106,12 @@ form.run([
|
|||||||
}).catch(function(error) {
|
}).catch(function(error) {
|
||||||
|
|
||||||
if (options.robot) {
|
if (options.robot) {
|
||||||
log.toStderr(error.message);
|
log.toStderr(JSON.stringify({
|
||||||
|
command: 'error',
|
||||||
|
data: {
|
||||||
|
message: error.message
|
||||||
|
}
|
||||||
|
}));
|
||||||
} else {
|
} else {
|
||||||
utils.printError(error);
|
utils.printError(error);
|
||||||
}
|
}
|
||||||
|
@ -137,45 +137,3 @@ exports.getTemporaryLogFilePath = function() {
|
|||||||
return logFilePath;
|
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)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
@ -72,11 +72,7 @@ return isElevated().then(function(elevated) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
tail.on('line', function(line) {
|
tail.on('line', function(line) {
|
||||||
const data = utils.parseEtcherCLIRobotLine(line);
|
process.send(JSON.parse(line));
|
||||||
|
|
||||||
if (data) {
|
|
||||||
process.send(data);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user