diff --git a/lib/common.js b/lib/common.js index 0dabe85..d76921b 100755 --- a/lib/common.js +++ b/lib/common.js @@ -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'); + }); } /** diff --git a/lib/do_images.js b/lib/do_images.js index 9556da0..9d1fc36 100644 --- a/lib/do_images.js +++ b/lib/do_images.js @@ -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. diff --git a/lib/do_instances.js b/lib/do_instances.js index d2805e8..06d2c8a 100644 --- a/lib/do_instances.js +++ b/lib/do_instances.js @@ -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, diff --git a/lib/do_keys.js b/lib/do_keys.js index add02bf..0ba8054 100644 --- a/lib/do_keys.js +++ b/lib/do_keys.js @@ -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) { diff --git a/lib/do_networks.js b/lib/do_networks.js index 088eb6c..6d5b37c 100644 --- a/lib/do_networks.js +++ b/lib/do_networks.js @@ -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, { diff --git a/lib/do_packages.js b/lib/do_packages.js index 50c84e3..30c355e 100644 --- a/lib/do_packages.js +++ b/lib/do_packages.js @@ -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];