TRITON-33 node-triton use common functions to cut down on code duplication

Reviewed by: Trent Mick <trentm@gmail.com>
Approved by: Trent Mick <trentm@gmail.com>
This commit is contained in:
Dave Eddy 2018-03-06 15:15:06 -05:00
parent bf64899685
commit 002171ea06
8 changed files with 24 additions and 99 deletions

View File

@ -72,12 +72,8 @@ function do_update(subcmd, opts, args, callback) {
next(); next();
return; return;
} }
var stdin = '';
process.stdin.resume(); common.readStdin(function gotStdin(stdin) {
process.stdin.on('data', function (chunk) {
stdin += chunk;
});
process.stdin.on('end', function () {
try { try {
ctx.data = JSON.parse(stdin); ctx.data = JSON.parse(stdin);
} catch (err) { } catch (err) {
@ -92,36 +88,18 @@ function do_update(subcmd, opts, args, callback) {
}, },
function validateIt(ctx, next) { function validateIt(ctx, next) {
var keys = Object.keys(ctx.data); try {
for (var i = 0; i < keys.length; i++) { common.validateObject(ctx.data, UPDATE_ACCOUNT_FIELDS);
var key = keys[i]; } catch (e) {
var value = ctx.data[key]; next(e);
var type = UPDATE_ACCOUNT_FIELDS[key]; return;
if (!type) {
next(new errors.UsageError(format('unknown or ' +
'unupdateable field: %s (updateable fields are: %s)',
key,
Object.keys(UPDATE_ACCOUNT_FIELDS).sort().join(', '))));
return;
}
if (typeof (value) !== type) {
next(new errors.UsageError(format('field "%s" must be ' +
'of type "%s", but got a value of type "%s"', key,
type, typeof (value))));
return;
}
} }
next(); next();
}, },
function updateAway(ctx, next) { function updateAway(ctx, next) {
var keys = Object.keys(ctx.data); var keys = Object.keys(ctx.data);
if (keys.length === 0) {
console.log('No fields given for account update');
next();
return;
}
tritonapi.cloudapi.updateAccount(ctx.data, function (err) { tritonapi.cloudapi.updateAccount(ctx.data, function (err) {
if (err) { if (err) {

View File

@ -84,14 +84,7 @@ function do_update(subcmd, opts, args, cb) {
return; return;
} }
var stdin = ''; common.readStdin(function gotStdin(stdin) {
process.stdin.resume();
process.stdin.on('data', function (chunk) {
stdin += chunk;
});
process.stdin.on('end', function () {
try { try {
ctx.data = JSON.parse(stdin); ctx.data = JSON.parse(stdin);
} catch (err) { } catch (err) {
@ -107,33 +100,13 @@ function do_update(subcmd, opts, args, cb) {
}, },
function validateIt(ctx, next) { function validateIt(ctx, next) {
var keys = Object.keys(ctx.data); try {
common.validateObject(ctx.data, UPDATE_FWRULE_FIELDS);
if (keys.length === 0) { } catch (e) {
console.log('No fields given for firewall rule update'); next(e);
next();
return; return;
} }
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var value = ctx.data[key];
var type = UPDATE_FWRULE_FIELDS[key];
if (!type) {
next(new errors.UsageError(format('unknown or ' +
'unupdateable field: %s (updateable fields are: %s)',
key,
Object.keys(UPDATE_FWRULE_FIELDS).sort().join(', '))));
return;
}
if (typeof (value) !== type) {
next(new errors.UsageError(format('field "%s" must be ' +
'of type "%s", but got a value of type "%s"', key,
type, typeof (value))));
return;
}
}
next(); next();
}, },

View File

@ -47,13 +47,7 @@ function do_add(subcmd, opts, args, cb) {
return next(); return next();
} }
var stdin = ''; common.readStdin(function gotStdin(stdin) {
process.stdin.resume();
process.stdin.on('data', function (chunk) {
stdin += chunk;
});
process.stdin.on('end', function () {
ctx.data = stdin; ctx.data = stdin;
ctx.from = '<stdin>'; ctx.from = '<stdin>';
next(); next();

View File

@ -93,12 +93,8 @@ function _createProfile(opts, cb) {
next(); next();
return; return;
} }
var stdin = '';
process.stdin.resume(); common.readStdin(function gotStdin(stdin) {
process.stdin.on('data', function (chunk) {
stdin += chunk;
});
process.stdin.on('end', function () {
try { try {
data = JSON.parse(stdin); data = JSON.parse(stdin);
} catch (err) { } catch (err) {

View File

@ -125,12 +125,8 @@ function _addUserKey(opts, cb) {
if (opts.file !== '-') { if (opts.file !== '-') {
return next(); return next();
} }
var stdin = '';
process.stdin.resume(); common.readStdin(function gotStdin(stdin) {
process.stdin.on('data', function (chunk) {
stdin += chunk;
});
process.stdin.on('end', function () {
ctx.data = stdin; ctx.data = stdin;
ctx.from = '<stdin>'; ctx.from = '<stdin>';
next(); next();

View File

@ -291,12 +291,8 @@ function _addPolicy(opts, cb) {
if (opts.file !== '-') { if (opts.file !== '-') {
return next(); return next();
} }
var stdin = '';
process.stdin.resume(); common.readStdin(function gotStdin(stdin) {
process.stdin.on('data', function (chunk) {
stdin += chunk;
});
process.stdin.on('end', function () {
try { try {
data = JSON.parse(stdin); data = JSON.parse(stdin);
} catch (err) { } catch (err) {

View File

@ -287,12 +287,8 @@ function _addRole(opts, cb) {
if (opts.file !== '-') { if (opts.file !== '-') {
return next(); return next();
} }
var stdin = '';
process.stdin.resume(); common.readStdin(function gotStdin(stdin) {
process.stdin.on('data', function (chunk) {
stdin += chunk;
});
process.stdin.on('end', function () {
try { try {
data = JSON.parse(stdin); data = JSON.parse(stdin);
} catch (err) { } catch (err) {

View File

@ -282,12 +282,8 @@ function _addUser(opts, cb) {
if (opts.file !== '-') { if (opts.file !== '-') {
return next(); return next();
} }
var stdin = '';
process.stdin.resume(); common.readStdin(function gotStdin(stdin) {
process.stdin.on('data', function (chunk) {
stdin += chunk;
});
process.stdin.on('end', function () {
try { try {
data = JSON.parse(stdin); data = JSON.parse(stdin);
} catch (err) { } catch (err) {