diff --git a/server/cli/accounts.js b/server/cli/accounts.js index 34231666..72e31abb 100644 --- a/server/cli/accounts.js +++ b/server/cli/accounts.js @@ -1,8 +1,9 @@ +let db function _initializeDB () { const config = require('../config') config.load() config.log_level = 'error' - const db = require('../api/models/index') + db = require('../api/models/index') return db.initialize() } @@ -25,8 +26,30 @@ async function modify (args) { } } -async function add (args) { - +async function create (args) { + await _initializeDB() + const User = require('../api/models/user') + console.error(args) + const user = await User.create({ + email: args.email, + is_active: true, + is_admin: args.admin || false + }) + console.error(user) + await db.close() +} + + +async function remove (args) { + await _initializeDB() + const User = require('../api/models/user') + const user = await User.findOne({ + where: { email: args.email } + }) + if (user) { + await user.destroy() + } + await db.close() } async function list () { @@ -36,6 +59,7 @@ async function list () { console.log() users.forEach(u => console.log(`${u.id}\tadmin: ${u.is_admin}\tenabled: ${u.is_active}\temail: ${u.email}`)) console.log() + await db.close() } const accountsCLI = yargs => yargs @@ -51,9 +75,12 @@ const accountsCLI = yargs => yargs type: 'boolean' } }, modify) - .command('add', 'Add an account', {}, add) + .command('create ', 'Create an account', { + admin: { describe: 'Define this account as administrator', type: 'boolean' } + }, create) + .positional('email', { describe: '', type: 'string', demandOption: true }) + .command('remove ', 'Remove an account', {}, remove) .recommendCommands() - .strict() .demandCommand(1, '') .argv