add CLI support

This commit is contained in:
lesion 2022-03-11 11:34:14 +01:00
parent 564db6961a
commit cef6fee5ac
No known key found for this signature in database
GPG key ID: 352918250B012177
3 changed files with 58 additions and 0 deletions

View file

@ -1,5 +1,7 @@
All notable changes to this project will be documented in this file.
### UNRELEASED
- add CLI support to manage accounts (list / modify / add accounts)
### 1.4.3 - 10 mar '22
- fix [#140](https://framagit.org/les/gancio/-/issues/140) - Invalid date
- fix [#141](https://framagit.org/les/gancio/-/issues/141) - Cannot change logo

View file

@ -1,6 +1,7 @@
#!/usr/bin/env node
const pkg = require('../package.json')
const path = require('path')
const accountsCLI = require('./cli/accounts')
process.env.cwd = process.env.GANCIO_DATA || path.resolve('./')
@ -28,6 +29,7 @@ require('yargs')
process.env.config_path = absolute_config_path
return absolute_config_path
})
.command(['accounts'], 'Manage accounts', accountsCLI)
.command(['start', 'run', '$0'], 'Start gancio', {}, start)
.help('h')
.alias('h', 'help')

54
server/cli/accounts.js Normal file
View file

@ -0,0 +1,54 @@
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) {
}
async function list () {
await _initializeDB()
const User = require('../api/models/user')
const users = await User.findAll()
console.log()
users.forEach(u => console.log(`${u.id}\tadmin: ${u.is_admin}\tenabled: ${u.is_active}\temail: ${u.email} - ${u.password}`))
console.log()
}
const accountsCLI = yargs => {
return yargs
.command('list', 'List all accounts', list)
.command('modify', 'Modify', {
account: {
describe: 'Account to modify'
},
'reset-password': {
describe: 'Resets the password of the given accoun '
}
}, modify)
.command('add', 'Add an account', {}, add)
}
module.exports = accountsCLI