This repository has been archived on 2020-01-20. You can view files and clone it, but cannot push or open issues or pull requests.
node-spearhead/lib/do_info.js

96 lines
2.4 KiB
JavaScript
Raw Normal View History

2015-08-26 07:16:41 +03:00
/*
* Copyright 2015 Joyent Inc.
*
* `triton info ...`
*/
2015-08-26 07:27:59 +03:00
var assert = require('assert-plus');
2015-08-26 07:16:41 +03:00
var common = require('./common');
2015-08-26 07:27:59 +03:00
var format = require('util').format;
2015-08-26 07:16:41 +03:00
function do_info(subcmd, opts, args, callback) {
var self = this;
if (opts.help) {
this.do_help('help', {}, [subcmd], callback);
return;
} else if (args.length !== 0) {
callback(new Error('invalid args: ' + args));
return;
}
var out = {};
var i = 0;
this.triton.cloudapi.getAccount(cb.bind('account')); i++;
this.triton.cloudapi.listMachines(cb.bind('machines')); i++;
function cb(err, data) {
if (err) {
callback(err);
return;
}
out[this.toString()] = data;
if (--i === 0)
done();
}
function done() {
if (opts.json) {
console.log(JSON.stringify(out));
} 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;
});
Object.keys(states).forEach(function (state) {
console.log('- %d %s', states[state], state);
});
2015-08-26 07:27:59 +03:00
console.log('- %s RAM Total',
common.humanSizeFromBytes(memory * 1000 * 1000));
console.log('- %s Disk Total',
common.humanSizeFromBytes(disk * 1000 * 1000));
2015-08-26 07:16:41 +03:00
}
callback();
}
}
do_info.options = [
{
names: ['help', 'h'],
type: 'bool',
help: 'Show this help.'
},
{
names: ['json', 'j'],
type: 'bool',
help: 'JSON output.'
}
];
do_info.help = (
2015-08-26 07:19:22 +03:00
'Print an account summary.\n'
2015-08-26 07:16:41 +03:00
+ '\n'
+ 'Usage:\n'
+ ' {{name}} account\n'
+ '\n'
+ '{{options}}'
);
module.exports = do_info;