From cef6fee5ac536971f89350bb9e3da162569d2d5e Mon Sep 17 00:00:00 2001 From: lesion Date: Fri, 11 Mar 2022 11:34:14 +0100 Subject: [PATCH] add CLI support --- CHANGELOG | 2 ++ server/cli.js | 2 ++ server/cli/accounts.js | 54 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 server/cli/accounts.js diff --git a/CHANGELOG b/CHANGELOG index 75375072..6f5fad7c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 diff --git a/server/cli.js b/server/cli.js index f6d2b81a..fbc2d4b9 100755 --- a/server/cli.js +++ b/server/cli.js @@ -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') diff --git a/server/cli/accounts.js b/server/cli/accounts.js new file mode 100644 index 00000000..05d181dc --- /dev/null +++ b/server/cli/accounts.js @@ -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