2014-02-07 23:21:24 +02:00
|
|
|
#!/usr/bin/env node
|
|
|
|
/**
|
2015-08-25 22:14:16 +03:00
|
|
|
* Copyright (c) 2015 Joyent Inc. All rights reserved.
|
2014-02-07 23:21:24 +02:00
|
|
|
*/
|
|
|
|
|
2015-08-25 22:14:16 +03:00
|
|
|
|
2014-02-07 23:21:24 +02:00
|
|
|
var assert = require('assert-plus');
|
|
|
|
var sprintf = require('extsprintf').sprintf;
|
|
|
|
var util = require('util'),
|
|
|
|
format = util.format;
|
|
|
|
|
|
|
|
var errors = require('./errors'),
|
|
|
|
InternalError = errors.InternalError;
|
|
|
|
|
|
|
|
|
2015-08-25 22:14:16 +03:00
|
|
|
// ---- globals
|
|
|
|
|
|
|
|
var p = console.log;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ---- support stuff
|
|
|
|
|
2014-02-07 23:21:24 +02:00
|
|
|
function objCopy(obj, target) {
|
|
|
|
if (target === undefined) {
|
|
|
|
target = {};
|
|
|
|
}
|
|
|
|
Object.keys(obj).forEach(function (k) {
|
|
|
|
target[k] = obj[k];
|
|
|
|
});
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function deepObjCopy(obj) {
|
|
|
|
return JSON.parse(JSON.stringify(obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function zeroPad(n, width) {
|
|
|
|
var s = String(n);
|
|
|
|
while (s.length < width) {
|
|
|
|
s = '0' + s;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a boolean or string representation into a boolean, or
|
|
|
|
* raise TypeError trying.
|
|
|
|
*
|
|
|
|
* @param value {Boolean|String} The input value to convert.
|
|
|
|
* @param default_ {Boolean} The default value is `value` is undefined.
|
|
|
|
* @param errName {String} The variable name to quote in the possibly
|
|
|
|
* raised TypeError.
|
|
|
|
*/
|
|
|
|
function boolFromString(value, default_, errName) {
|
|
|
|
if (value === undefined) {
|
|
|
|
return default_;
|
|
|
|
} else if (value === 'false' || value === '0') {
|
|
|
|
return false;
|
|
|
|
} else if (value === 'true' || value === '1') {
|
|
|
|
return true;
|
|
|
|
} else if (typeof (value) === 'boolean') {
|
|
|
|
return value;
|
|
|
|
} else {
|
|
|
|
throw new TypeError(
|
|
|
|
format('invalid value for "%s": %j', errName, value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 01:30:25 +03:00
|
|
|
/**
|
|
|
|
* 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');
|
|
|
|
}
|
2014-02-07 23:21:24 +02:00
|
|
|
|
|
|
|
/**
|
2015-08-26 01:30:25 +03:00
|
|
|
* given an array of key=value pairs, break them into an object
|
2014-02-07 23:21:24 +02:00
|
|
|
*
|
2015-08-26 01:30:25 +03:00
|
|
|
* @param {Array} kvs - an array of key=value pairs
|
|
|
|
* @param {Array} valid (optional) - an array to validate pairs
|
2014-02-07 23:21:24 +02:00
|
|
|
*/
|
2015-08-26 01:30:25 +03:00
|
|
|
function kvToObj(kvs, valid) {
|
|
|
|
var o = {};
|
|
|
|
for (var i = 0; i < kvs.length; i++) {
|
|
|
|
var kv = kvs[i];
|
|
|
|
var idx = kv.indexOf('=');
|
|
|
|
if (idx === -1)
|
|
|
|
throw new errors.UsageError(format(
|
|
|
|
'invalid filter: "%s" (must be of the form "field=value")',
|
|
|
|
kv));
|
|
|
|
var k = kv.slice(0, idx);
|
|
|
|
var v = kv.slice(idx + 1);
|
|
|
|
if (valid.indexOf(k) === -1)
|
|
|
|
throw new errors.UsageError(format(
|
|
|
|
'invalid filter name: "%s" (must be one of "%s")',
|
|
|
|
k, valid.join('", "')));
|
|
|
|
o[k] = v;
|
2014-02-07 23:21:24 +02:00
|
|
|
}
|
2015-08-26 01:30:25 +03:00
|
|
|
return o;
|
2014-02-07 23:21:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---- exports
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
objCopy: objCopy,
|
|
|
|
deepObjCopy: deepObjCopy,
|
|
|
|
zeroPad: zeroPad,
|
|
|
|
boolFromString: boolFromString,
|
2015-08-26 01:30:25 +03:00
|
|
|
jsonStream: jsonStream,
|
|
|
|
kvToObj: kvToObj
|
2014-02-07 23:21:24 +02:00
|
|
|
};
|
|
|
|
// vim: set softtabstop=4 shiftwidth=4:
|