bin/manage_users: fix formatting

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2022-04-03 17:54:32 +02:00
parent 337173bb38
commit 445a3787d9

View file

@ -1,15 +1,15 @@
#!/usr/bin/env node #!/usr/bin/env node
// First configure the logger so it does not spam the console // First configure the logger, so it does not spam the console
const logger = require("../lib/logger"); const logger = require('../lib/logger')
logger.transports.forEach((transport) => transport.level = "warning") logger.transports.forEach((transport) => transport.level = 'warning')
const models = require("../lib/models/"); const models = require('../lib/models/')
const readline = require("readline-sync"); const readline = require('readline-sync')
const minimist = require("minimist"); const minimist = require('minimist')
function showUsage(tips) { function showUsage (tips) {
console.log(`${tips} console.log(`${tips}
Command-line utility to create users for email-signin. Command-line utility to create users for email-signin.
@ -19,104 +19,102 @@ Usage: bin/manage_users [--pass password] (--add | --del) user-email
--del Delete user with specified user-email --del Delete user with specified user-email
--reset Reset user password with specified user-email --reset Reset user password with specified user-email
--pass Use password from cmdline rather than prompting --pass Use password from cmdline rather than prompting
`); `)
process.exit(1); process.exit(1)
} }
function getPass(argv, action) { function getPass (argv, action) {
// Find whether we use cmdline or prompt password // Find whether we use cmdline or prompt password
if(typeof argv["pass"] !== 'string') { if (typeof argv['pass'] !== 'string') {
return readline.question(`Password for ${argv[action]}:`, {hideEchoBack: true}); return readline.question(`Password for ${argv[action]}:`, { hideEchoBack: true })
} }
console.log("Using password from commandline..."); console.log('Using password from commandline...')
return argv["pass"]; return argv['pass']
} }
// Using an async function to be able to use await inside // Using an async function to be able to use await inside
async function createUser(argv) { async function createUser (argv) {
const existing_user = await models.User.findOne({where: {email: argv["add"]}}); const existing_user = await models.User.findOne({ where: { email: argv['add'] } })
// Cannot create already-existing users // Cannot create already-existing users
if(existing_user) { if (existing_user) {
console.log(`User with e-mail ${existing_user.email} already exists! Aborting ...`); console.log(`User with e-mail ${existing_user.email} already exists! Aborting ...`)
process.exit(2); process.exit(2)
}
const pass = getPass(argv, "add");
if (pass.length === 0) {
console.log("Password cannot be empty!");
process.exit(1);
} }
// Lets try to create, and check success const pass = getPass(argv, 'add')
const ref = await models.User.create({email: argv["add"], password: pass}); if (pass.length === 0) {
if(ref == undefined) { console.log('Password cannot be empty!')
console.log(`Could not create user with email ${argv["add"]}`); process.exit(1)
process.exit(1); }
} else
console.log(`Created user with email ${argv["add"]}`); // Lets try to create, and check success
const ref = await models.User.create({ email: argv['add'], password: pass })
if (ref === undefined) {
console.log(`Could not create user with email ${argv['add']}`)
process.exit(1)
} else {
console.log(`Created user with email ${argv['add']}`)
}
} }
// Using an async function to be able to use await inside // Using an async function to be able to use await inside
async function deleteUser(argv) { async function deleteUser (argv) {
// Cannot delete non-existing users // Cannot delete non-existing users
const existing_user = await models.User.findOne({where: {email: argv["del"]}}); const existing_user = await models.User.findOne({ where: { email: argv['del'] } })
if(!existing_user) { if (!existing_user) {
console.log(`User with e-mail ${argv["del"]} does not exist, cannot delete`); console.log(`User with e-mail ${argv['del']} does not exist, cannot delete`)
process.exit(1); process.exit(1)
} }
// Sadly .destroy() does not return any success value with all // Sadly .destroy() does not return any success value with all
// backends. See sequelize #4124 // backends. See sequelize #4124
await existing_user.destroy(); await existing_user.destroy()
console.log(`Deleted user ${argv["del"]} ...`); console.log(`Deleted user ${argv['del']} ...`)
} }
// Using an async function to be able to use await inside // Using an async function to be able to use await inside
async function resetUser(argv) { async function resetUser (argv) {
const existing_user = await models.User.findOne({where: {email: argv["reset"]}}); const existing_user = await models.User.findOne({ where: { email: argv['reset'] } })
// Cannot reset non-existing users // Cannot reset non-existing users
if(!existing_user) { if (!existing_user) {
console.log(`User with e-mail ${argv["reset"]} does not exist, cannot reset`); console.log(`User with e-mail ${argv['reset']} does not exist, cannot reset`)
process.exit(1); process.exit(1)
} }
const pass = getPass(argv, "reset"); // set password and save
existing_user.password = getPass(argv, 'reset')
// set password and save await existing_user.save()
existing_user.password = pass; console.log(`User with email ${argv['reset']} password has been reset`)
await existing_user.save();
console.log(`User with email ${argv["reset"]} password has been reset`);
} }
const options = { const options = {
add: createUser, add: createUser,
del: deleteUser, del: deleteUser,
reset: resetUser, reset: resetUser,
}; }
// Perform commandline-parsing and always treat 'pass' argument as string // Perform commandline-parsing and always treat 'pass' argument as string
const argv = minimist(process.argv.slice(2), {string: ["pass"]}); const argv = minimist(process.argv.slice(2), { string: ['pass'] })
const keys = Object.keys(options); const keys = Object.keys(options)
const opts = keys.filter((key) => argv[key] !== undefined); const opts = keys.filter((key) => argv[key] !== undefined)
const action = opts[0]; const action = opts[0]
// Check for options missing // Check for options missing
if (opts.length === 0) { if (opts.length === 0) {
showUsage(`You did not specify either ${keys.map((key) => `--${key}`).join(' or ')}!`); showUsage(`You did not specify either ${keys.map((key) => `--${key}`).join(' or ')}!`)
} }
// Check if both are specified // Check if both are specified
if (opts.length > 1) { if (opts.length > 1) {
showUsage(`You cannot ${action.join(' and ')} at the same time!`); showUsage(`You cannot ${action.join(' and ')} at the same time!`)
} }
// Check if not string // Check if not string
if (typeof argv[action] !== 'string') { if (typeof argv[action] !== 'string') {
showUsage(`You must follow an email after --${action}`); showUsage(`You must follow an email after --${action}`)
} }
// Call respective processing functions // Call respective processing functions
options[action](argv).then(function() { options[action](argv).then(function () {
process.exit(0); process.exit(0);
}); });