summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorErona2018-10-29 22:03:05 +0800
committerErona2018-10-29 22:34:45 +0800
commit7b12945c498464370f6f482c273a4ac535da74e8 (patch)
treeb71de28b2f700ef9c06412aaea8dd780ea1d3dc2 /bin
parent279213eb75ddfe29a81be193e9a42f25979d9d06 (diff)
refactor(bin): refactor check args in bin/manage_users
Signed-off-by: Erona <erona@loli.bz>
Diffstat (limited to 'bin')
-rwxr-xr-xbin/manage_users31
1 files changed, 17 insertions, 14 deletions
diff --git a/bin/manage_users b/bin/manage_users
index 74ac1812..8c615251 100755
--- a/bin/manage_users
+++ b/bin/manage_users
@@ -60,30 +60,33 @@ async function deleteUser(argv) {
console.log("Deleted user "+argv["del"]+" ...");
}
+var options = {
+ add: createUser,
+ del: deleteUser,
+};
+
// Perform commandline-parsing
var argv = minimist(process.argv.slice(2));
-// Check for add/delete missing
-if (argv["add"] == undefined && argv["del"] == undefined) {
- console.log("You did not specify either --add or --del!");
+var keys = Object.keys(options);
+var opts = keys.filter((key) => argv[key] !== undefined);
+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);
process.exit(1);
}
// Check if both are specified
-if (argv["add"] != undefined && argv["del"] != undefined) {
- console.log("You cannot add and delete at the same time!");
+if (opts.length > 1) {
+ console.log(`You cannot ${action.join(' and ')} at the same time!`);
console.log(usage);
process.exit(1);
}
// Call respective processing functions
-if (argv["add"] != undefined) {
- createUser(argv).then(function() {
- process.exit(0);
- });
-} else if (argv["del"] != undefined) {
- deleteUser(argv).then(function() {
- process.exit(0);
- })
-}
+options[action](argv).then(function() {
+ process.exit(0);
+});