refactor(bin): refactor check args in bin/manage_users

Signed-off-by: Erona <erona@loli.bz>
This commit is contained in:
Erona 2018-10-29 22:03:05 +08:00
parent 279213eb75
commit 7b12945c49
No known key found for this signature in database
GPG key ID: C9B930B72E7104C0

View file

@ -60,30 +60,33 @@ async function deleteUser(argv) {
console.log("Deleted user "+argv["del"]+" ..."); console.log("Deleted user "+argv["del"]+" ...");
} }
var options = {
add: createUser,
del: deleteUser,
};
// Perform commandline-parsing // Perform commandline-parsing
var argv = minimist(process.argv.slice(2)); var argv = minimist(process.argv.slice(2));
// Check for add/delete missing var keys = Object.keys(options);
if (argv["add"] == undefined && argv["del"] == undefined) { var opts = keys.filter((key) => argv[key] !== undefined);
console.log("You did not specify either --add or --del!"); var action = opts[0];
// Check for options missing
if (opts.length === 0) {
console.log(`You did not specify either ${keys.map((key) => `--${key}`).join(' or ')}!`);
console.log(usage); console.log(usage);
process.exit(1); process.exit(1);
} }
// Check if both are specified // Check if both are specified
if (argv["add"] != undefined && argv["del"] != undefined) { if (opts.length > 1) {
console.log("You cannot add and delete at the same time!"); console.log(`You cannot ${action.join(' and ')} at the same time!`);
console.log(usage); console.log(usage);
process.exit(1); process.exit(1);
} }
// Call respective processing functions // Call respective processing functions
if (argv["add"] != undefined) { options[action](argv).then(function() {
createUser(argv).then(function() { process.exit(0);
process.exit(0); });
});
} else if (argv["del"] != undefined) {
deleteUser(argv).then(function() {
process.exit(0);
})
}