From de2a462f16b82f3d6d3ec6e4c831f4e12e859cfe Mon Sep 17 00:00:00 2001 From: Dave Eddy Date: Thu, 3 Sep 2015 18:24:14 -0400 Subject: [PATCH] triton info cleanup, fixes #8 --- lib/do_info.js | 65 +++++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 25 deletions(-) diff --git a/lib/do_info.js b/lib/do_info.js index 7089806..7a26ab2 100644 --- a/lib/do_info.js +++ b/lib/do_info.js @@ -37,35 +37,50 @@ function do_info(subcmd, opts, args, callback) { } function done() { + // parse name + var name; + if (out.account.firstName && out.account.lastName) + name = format('%s %s', out.account.firstName, + out.account.lastName); + else if (out.account.firstName) + name = out.account.firstName; + + // parse machine states and accounting + var states = {}; + var disk = 0; + var memory = 0; + out.machines.forEach(function (machine) { + var state = machine.state; + states[state] = states[state] || 0; + states[state]++; + memory += machine.memory; + disk += machine.disk; + }); + disk *= 1000 * 1000; + memory *= 1000 * 1000; + + var data = {}; + data.login = out.account.login; + if (name) + data.name = name; + data.email = out.account.email; + data.url = self.triton.cloudapi.url; + data.totalInstances = out.machines.length; + data.totalDisk = disk; + data.totalMemory = memory; + if (opts.json) { - console.log(JSON.stringify(out)); + data.machines = states; + console.log(JSON.stringify(data)); } else { - // pretty print - console.log('%s - %s %s <%s>', - out.account.login, - out.account.firstName, - out.account.lastName, - out.account.email); - console.log(self.triton.cloudapi.url); - console.log(); - console.log('%d instance(s)', out.machines.length); - var states = {}; - var disk = 0; - var memory = 0; - out.machines.forEach(function (machine) { - var state = machine.state; - states[state] = states[state] || 0; - states[state]++; - memory += machine.memory; - disk += machine.disk; + data.totalDisk = common.humanSizeFromBytes(disk); + data.totalMemory = common.humanSizeFromBytes(memory); + Object.keys(data).forEach(function (key) { + console.log('%s: %s', key, data[key]); }); - Object.keys(states).forEach(function (state) { - console.log('- %d %s', states[state], state); + Object.keys(states).forEach(function (key) { + console.log('machines.%s: %s', key, states[key]); }); - console.log('- %s RAM Total', - common.humanSizeFromBytes(memory * 1000 * 1000)); - console.log('- %s Disk Total', - common.humanSizeFromBytes(disk * 1000 * 1000)); } callback(); }