enigma-bbs/oputil.js

145 lines
2.9 KiB
JavaScript
Raw Normal View History

2016-01-04 00:02:08 +00:00
#!/usr/bin/env node
2016-01-03 22:50:56 +00:00
/* jslint node: true */
'use strict';
// ENiGMA½
var config = require('./core/config.js');
var db = require('./core/database.js');
var _ = require('lodash');
var async = require('async');
2016-01-03 23:04:37 +00:00
var assert = require('assert');
2016-01-03 22:50:56 +00:00
var argv = require('minimist')(process.argv.slice(2));
const ExitCodes = {
2016-01-03 22:50:56 +00:00
SUCCESS : 0,
ERROR : -1,
BAD_COMMAND : -2,
BAD_ARGS : -3,
}
function printUsage(command) {
var usage;
switch(command) {
case '' :
usage =
'usage: oputil.js [--version] [--help]\n' +
' <command> [<args>]' +
'\n\n' +
2016-01-03 22:50:56 +00:00
'global args:\n' +
' --config PATH : specify config path' +
'\n\n' +
'commands:\n' +
' user : User utilities' +
'\n';
2016-01-03 22:50:56 +00:00
break;
case 'user' :
usage =
'usage: optutil.js user --user USERNAME <args>\n' +
'\n' +
'valid args:\n' +
' --user USERNAME : specify username\n' +
' --password PASS : reset password to PASS';
break;
}
console.error(usage);
}
function initConfig(cb) {
const configPath = argv.config ? argv.config : config.getDefaultPath();
2016-01-03 22:50:56 +00:00
config.init(configPath, cb);
}
function handleUserCommand() {
if(true === argv.help || !_.isString(argv.user) || 0 === argv.user.length) {
process.exitCode = ExitCodes.ERROR;
return printUsage('user');
}
if(_.isString(argv.password)) {
if(0 === argv.password.length) {
process.exitCode = ExitCodes.BAD_ARGS;
return console.error('Invalid password');
}
var user;
async.waterfall(
[
function init(callback) {
initConfig(callback);
},
function initDb(callback) {
db.initializeDatabases(callback);
},
function getUser(callback) {
user = require('./core/user.js');
2016-01-03 23:04:37 +00:00
user.getUserIdAndName(argv.user, function userNameAndId(err, userId) {
2016-01-03 22:50:56 +00:00
if(err) {
process.exitCode = ExitCodes.BAD_ARGS;
callback(new Error('Failed to retrieve user'));
} else {
callback(null, userId);
}
});
},
function setNewPass(userId, callback) {
2016-01-03 23:04:37 +00:00
assert(_.isNumber(userId));
assert(userId > 0);
2016-01-04 00:02:08 +00:00
let u = new user.User();
2016-01-03 22:50:56 +00:00
u.userId = userId;
2016-01-03 23:04:37 +00:00
2016-01-03 22:50:56 +00:00
u.setNewAuthCredentials(argv.password, function credsSet(err) {
if(err) {
process.exitCode = ExitCodes.ERROR;
callback(new Error('Failed setting password'));
} else {
callback(null);
}
});
}
],
function complete(err) {
if(err) {
console.error(err.message);
} else {
console.info('Password set');
}
}
);
}
}
function main() {
process.exitCode = ExitCodes.SUCCESS;
2016-01-04 00:57:57 +00:00
if(true === argv.version) {
return console.info(require('./package.json').version);
}
2016-01-03 22:50:56 +00:00
if(0 === argv._.length ||
'help' === argv._[0])
{
printUsage('');
process.exit(ExitCodes.SUCCESS);
}
switch(argv._[0]) {
case 'user' :
handleUserCommand();
break;
default:
printUsage('');
process.exitCode = ExitCodes.BAD_COMMAND;
}
}
main();