From f11bf0c247fd808b2c31c1bfadbb0cace8971528 Mon Sep 17 00:00:00 2001 From: Dave Eddy Date: Wed, 26 Aug 2015 16:53:23 -0400 Subject: [PATCH] jsonStream should actually stream --- lib/common.js | 9 +++++---- lib/do_images.js | 2 +- lib/do_instances.js | 2 +- lib/do_keys.js | 2 +- lib/do_networks.js | 2 +- lib/do_packages.js | 2 +- 6 files changed, 10 insertions(+), 9 deletions(-) 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];