sysinfo: async'ify
in the process, provision, dyndns, mail, dns also got further asyncified
This commit is contained in:
@@ -5,35 +5,32 @@ exports = module.exports = {
|
||||
testConfig
|
||||
};
|
||||
|
||||
var assert = require('assert'),
|
||||
const assert = require('assert'),
|
||||
BoxError = require('../boxerror.js'),
|
||||
debug = require('debug')('box:sysinfo/network-interface'),
|
||||
os = require('os');
|
||||
os = require('os'),
|
||||
safe = require('safetydance');
|
||||
|
||||
function getServerIp(config, callback) {
|
||||
async function getServerIp(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
|
||||
const ifaces = os.networkInterfaces();
|
||||
const iface = ifaces[config.ifname]; // array of addresses
|
||||
if (!iface) return callback(new BoxError(BoxError.NETWORK_ERROR, `No interface named ${config.ifname}`));
|
||||
if (!iface) throw new BoxError(BoxError.NETWORK_ERROR, `No interface named ${config.ifname}`);
|
||||
|
||||
const addresses = iface.filter(i => i.family === 'IPv4').map(i => i.address);
|
||||
if (addresses.length === 0) return callback(new BoxError(BoxError.NETWORK_ERROR, `${config.ifname} does not have any IPv4 address`));
|
||||
if (addresses.length === 0) throw new BoxError(BoxError.NETWORK_ERROR, `${config.ifname} does not have any IPv4 address`);
|
||||
if (addresses.length > 1) debug(`${config.ifname} has multiple ipv4 - ${JSON.stringify(addresses)}. choosing the first one.`);
|
||||
|
||||
return callback(null, addresses[0]);
|
||||
return addresses[0];
|
||||
}
|
||||
|
||||
function testConfig(config, callback) {
|
||||
async function testConfig(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
|
||||
if (typeof config.ifname !== 'string') return callback(new BoxError(BoxError.BAD_FIELD, 'ifname is not a string'));
|
||||
if (typeof config.ifname !== 'string') return new BoxError(BoxError.BAD_FIELD, 'ifname is not a string');
|
||||
|
||||
getServerIp(config, function (error) {
|
||||
if (error) return callback(error);
|
||||
|
||||
callback(null);
|
||||
});
|
||||
const [error] = await safe(getServerIp(config));
|
||||
return error || null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user