jsonStream should actually stream

This commit is contained in:
Dave Eddy 2015-08-26 16:53:23 -04:00
parent ffb0a935a3
commit f11bf0c247
6 changed files with 10 additions and 9 deletions

View File

@ -72,10 +72,11 @@ function boolFromString(value, default_, errName) {
* given an array return a string with each element
* JSON-stringifed separated by newlines
*/
function jsonStream(arr) {
return arr.map(function (elem) {
return JSON.stringify(elem);
}).join('\n');
function jsonStream(arr, stream) {
stream = stream || process.stdout;
arr.forEach(function (elem) {
stream.write(JSON.stringify(elem) + '\n');
});
}
/**

View File

@ -50,7 +50,7 @@ function do_images(subcmd, opts, args, callback) {
// XXX we should have a common method for all these:
// XXX sorting
// XXX if opts.o is given, then filter to just those fields?
console.log(common.jsonStream(imgs));
common.jsonStream(imgs);
} else {
// Add some convenience fields
// Added fields taken from imgapi-cli.git.

View File

@ -99,7 +99,7 @@ function do_instances(subcmd, opts, args, callback) {
});
if (opts.json) {
console.log(common.jsonStream(machines));
common.jsonStream(machines);
} else {
tabula(machines, {
skipHeader: opts.H,

View File

@ -22,7 +22,7 @@ function do_keys(subcmd, opts, args, callback) {
}
if (opts.json) {
console.log(common.jsonStream(keys));
common.jsonStream(keys);
} else {
// pretty print
keys.forEach(function (key) {

View File

@ -55,7 +55,7 @@ function do_networks(subcmd, opts, args, callback) {
}
if (opts.json) {
console.log(common.jsonStream(networks));
common.jsonStream(networks);
} else {
// pretty print
tabula(networks, {

View File

@ -41,7 +41,7 @@ function do_packages (subcmd, opts, args, callback) {
return;
}
if (opts.json) {
console.log(common.jsonStream(pkgs));
common.jsonStream(pkgs);
} else {
for (var i = 0; i < pkgs.length; i++) {
var pkg = pkgs[i];