gancio/server/cli/accounts.js

60 lines
1.5 KiB
JavaScript
Raw Normal View History

2022-03-11 11:34:14 +01:00
function _initializeDB () {
const config = require('../config')
config.load()
config.log_level = 'error'
const db = require('../api/models/index')
return db.initialize()
}
async function modify (args) {
await _initializeDB()
const helpers = require('../helpers')
const User = require('../api/models/user')
const user = await User.findOne({ where: { email: args.account } })
console.log()
if (!user) {
console.error(`User ${args.account} not found`)
return
}
if (args['reset-password']) {
const password = helpers.randomString()
user.password = password
await user.save()
console.log(`New password for user ${user.email} is '${password}'`)
}
}
async function add (args) {
2022-05-05 11:12:53 +02:00
2022-03-11 11:34:14 +01:00
}
async function list () {
await _initializeDB()
const User = require('../api/models/user')
const users = await User.findAll()
console.log()
2022-05-05 11:12:53 +02:00
users.forEach(u => console.log(`${u.id}\tadmin: ${u.is_admin}\tenabled: ${u.is_active}\temail: ${u.email}`))
2022-03-11 11:34:14 +01:00
console.log()
}
2022-05-05 16:01:39 +02:00
const accountsCLI = yargs => yargs
2022-03-11 11:34:14 +01:00
.command('list', 'List all accounts', list)
.command('modify', 'Modify', {
account: {
2022-05-05 11:12:53 +02:00
describe: 'Account to modify',
type: 'string',
demandOption: true
2022-03-11 11:34:14 +01:00
},
'reset-password': {
2022-05-05 16:01:39 +02:00
describe: 'Resets the password of the given account ',
2022-05-05 11:12:53 +02:00
type: 'boolean'
2022-03-11 11:34:14 +01:00
}
}, modify)
.command('add', 'Add an account', {}, add)
2022-05-05 11:12:53 +02:00
.recommendCommands()
.strict()
.demandCommand(1, '')
2022-05-05 16:01:39 +02:00
.argv
2022-03-11 11:34:14 +01:00
2022-05-05 16:01:39 +02:00
module.exports = accountsCLI