add ago, fix bug
This commit is contained in:
parent
014e63a331
commit
f2dee08b46
@ -106,6 +106,34 @@ function kvToObj(kvs, valid) {
|
||||
return o;
|
||||
}
|
||||
|
||||
/**
|
||||
* return how long ago something happened
|
||||
*
|
||||
* @param {Date} when - a date object in the past
|
||||
* @param {Date} now (optional) - a date object to compare to
|
||||
* @return {String} - printable string
|
||||
*/
|
||||
function longAgo(when, now) {
|
||||
now = now || new Date();
|
||||
var seconds = Math.round((now - when) / 1000);
|
||||
var times = [
|
||||
seconds / 60 / 60 / 24 / 365, // years
|
||||
seconds / 60 / 60 / 24 / 30, // months
|
||||
seconds / 60 / 60 / 24, // days
|
||||
seconds / 60 / 60, // hours
|
||||
seconds / 60, // minutes
|
||||
seconds // seconds
|
||||
];
|
||||
var names = ['y', 'mon', 'd', 'h', 'min', 's'];
|
||||
|
||||
for (var i = 0; i < names.length; i++) {
|
||||
var time = Math.floor(times[i]);
|
||||
if (time > 0)
|
||||
return util.format('%d%s', time, names[i]);
|
||||
}
|
||||
return '0s';
|
||||
}
|
||||
|
||||
//---- exports
|
||||
|
||||
module.exports = {
|
||||
@ -114,6 +142,7 @@ module.exports = {
|
||||
zeroPad: zeroPad,
|
||||
boolFromString: boolFromString,
|
||||
jsonStream: jsonStream,
|
||||
kvToObj: kvToObj
|
||||
kvToObj: kvToObj,
|
||||
longAgo: longAgo
|
||||
};
|
||||
// vim: set softtabstop=4 shiftwidth=4:
|
||||
|
@ -32,7 +32,8 @@ var validFields = [
|
||||
'created',
|
||||
'updated',
|
||||
'package',
|
||||
'image'
|
||||
'image',
|
||||
'ago'
|
||||
];
|
||||
|
||||
function do_instances(subcmd, opts, args, callback) {
|
||||
@ -60,6 +61,14 @@ function do_instances(subcmd, opts, args, callback) {
|
||||
callback(err);
|
||||
return;
|
||||
}
|
||||
|
||||
// add extra fields for nice output
|
||||
var now = new Date();
|
||||
machines.forEach(function (machine) {
|
||||
var created = new Date(machine.created);
|
||||
machine.ago = common.longAgo(created, now);
|
||||
});
|
||||
|
||||
if (opts.json) {
|
||||
console.log(common.jsonStream(machines));
|
||||
} else {
|
||||
@ -88,7 +97,7 @@ do_instances.options = [
|
||||
{
|
||||
names: ['o'],
|
||||
type: 'string',
|
||||
default: 'id,name,state,type,image,memory,disk',
|
||||
default: 'id,name,state,type,image,memory,disk,ago',
|
||||
help: 'Specify fields (columns) to output.',
|
||||
helpArg: 'field1,...'
|
||||
},
|
||||
@ -114,6 +123,6 @@ do_instances.help = (
|
||||
+ '{{options}}'
|
||||
);
|
||||
|
||||
do_packages.aliases = ['insts'];
|
||||
do_instances.aliases = ['insts'];
|
||||
|
||||
module.exports = do_instances;
|
||||
|
Reference in New Issue
Block a user