From 79842b82e8c8383ed22fce1edfe1035d5a02fc18 Mon Sep 17 00:00:00 2001 From: Erona Date: Mon, 29 Oct 2018 22:32:14 +0800 Subject: refactor(bin): add function getPass in bin/manage_users Signed-off-by: Erona --- bin/manage_users | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'bin') diff --git a/bin/manage_users b/bin/manage_users index f748e342..0d99f36b 100755 --- a/bin/manage_users +++ b/bin/manage_users @@ -22,6 +22,15 @@ Usage: bin/manage_users [--pass password] (--add | --del) user-email process.exit(1); } +function getPass(argv, action) { + // Find whether we use cmdline or prompt password + if(typeof argv["pass"] !== 'string') { + return readline.question(`Password for ${argv[action]}:`, {hideEchoBack: true}); + } + console.log("Using password from commandline..."); + return argv["pass"]; +} + // Using an async function to be able to use await inside async function createUser(argv) { const existing_user = await models.User.findOne({where: {email: argv["add"]}}); @@ -31,14 +40,8 @@ async function createUser(argv) { process.exit(1); } - // Find whether we use cmdline or prompt password - let pass; - if(argv["pass"] == undefined) { - pass = readline.question(`Password for ${argv["add"]}:`, {hideEchoBack: true}); - } else { - console.log("Using password from commandline..."); - pass = "" + argv["pass"]; - } + const pass = getPass(argv, "add"); + // Lets try to create, and check success const ref = await models.User.create({email: argv["add"], password: pass}); -- cgit v1.2.3