improve accounts cli

This commit is contained in:
lesion
2022-06-06 16:56:54 +02:00
parent f5e1412e2b
commit 82dcaf9902

View File

@@ -1,8 +1,9 @@
let db
function _initializeDB () { function _initializeDB () {
const config = require('../config') const config = require('../config')
config.load() config.load()
config.log_level = 'error' config.log_level = 'error'
const db = require('../api/models/index') db = require('../api/models/index')
return db.initialize() 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 () { async function list () {
@@ -36,6 +59,7 @@ async function list () {
console.log() console.log()
users.forEach(u => console.log(`${u.id}\tadmin: ${u.is_admin}\tenabled: ${u.is_active}\temail: ${u.email}`)) users.forEach(u => console.log(`${u.id}\tadmin: ${u.is_admin}\tenabled: ${u.is_active}\temail: ${u.email}`))
console.log() console.log()
await db.close()
} }
const accountsCLI = yargs => yargs const accountsCLI = yargs => yargs
@@ -51,9 +75,12 @@ const accountsCLI = yargs => yargs
type: 'boolean' type: 'boolean'
} }
}, modify) }, modify)
.command('add', 'Add an account', {}, add) .command('create <email|username>', 'Create an account', {
admin: { describe: 'Define this account as administrator', type: 'boolean' }
}, create)
.positional('email', { describe: '', type: 'string', demandOption: true })
.command('remove <email|username>', 'Remove an account', {}, remove)
.recommendCommands() .recommendCommands()
.strict()
.demandCommand(1, '') .demandCommand(1, '')
.argv .argv