mirror of
https://framagit.org/les/gancio.git
synced 2025-01-31 16:42:22 +01:00
improve accounts cli
This commit is contained in:
parent
f5e1412e2b
commit
82dcaf9902
1 changed files with 32 additions and 5 deletions
|
@ -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 <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()
|
||||
.strict()
|
||||
.demandCommand(1, '')
|
||||
.argv
|
||||
|
||||
|
|
Loading…
Reference in a new issue