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 * given an array return a string with each element
* JSON-stringifed separated by newlines * JSON-stringifed separated by newlines
*/ */
function jsonStream(arr) { function jsonStream(arr, stream) {
return arr.map(function (elem) { stream = stream || process.stdout;
return JSON.stringify(elem); arr.forEach(function (elem) {
}).join('\n'); 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 we should have a common method for all these:
// XXX sorting // XXX sorting
// XXX if opts.o is given, then filter to just those fields? // XXX if opts.o is given, then filter to just those fields?
console.log(common.jsonStream(imgs)); common.jsonStream(imgs);
} else { } else {
// Add some convenience fields // Add some convenience fields
// Added fields taken from imgapi-cli.git. // Added fields taken from imgapi-cli.git.

View File

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

View File

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

View File

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

View File

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