Make add and reset CLI commands scriptable

Add CLI options `--password` and `--save-logs` (for `add` only) in
order to make adding users and changing user passwords scriptable.

Closes #3913
This commit is contained in:
Taavi Väänänen 2020-10-04 18:03:26 +03:00
parent 6ca3bae73e
commit e790a72e59
2 changed files with 33 additions and 16 deletions

View File

@ -11,7 +11,9 @@ program
.command("add <name>")
.description("Add a new user")
.on("--help", Utils.extraHelp)
.action(function (name) {
.option("--password [password]", "new password, will be prompted if not specified")
.option("--save-logs", "if password is specified, this enables saving logs to disk")
.action(function (name, cmdObj) {
if (!fs.existsSync(Helper.getUsersPath())) {
log.error(`${Helper.getUsersPath()} does not exist.`);
return;
@ -31,6 +33,11 @@ program
return;
}
if (cmdObj.password) {
add(manager, name, cmdObj.password, !!cmdObj.saveLogs);
return;
}
log.prompt(
{
text: "Enter password:",

View File

@ -11,7 +11,8 @@ program
.command("reset <name>")
.description("Reset user password")
.on("--help", Utils.extraHelp)
.action(function (name) {
.option("--password [password]", "new password, will be prompted if not specified")
.action(function (name, cmdObj) {
if (!fs.existsSync(Helper.getUsersPath())) {
log.error(`${Helper.getUsersPath()} does not exist.`);
return;
@ -30,9 +31,10 @@ program
return;
}
const pathReal = Helper.getUserConfigPath(name);
const pathTemp = pathReal + ".tmp";
const user = JSON.parse(fs.readFileSync(pathReal, "utf-8"));
if (cmdObj.password) {
change(name, cmdObj.password);
return;
}
log.prompt(
{
@ -44,6 +46,16 @@ program
return;
}
change(name, password);
}
);
});
function change(name, password) {
const pathReal = Helper.getUserConfigPath(name);
const pathTemp = pathReal + ".tmp";
const user = JSON.parse(fs.readFileSync(pathReal, "utf-8"));
user.password = Helper.password.hash(password);
user.sessions = {};
@ -55,6 +67,4 @@ program
fs.renameSync(pathTemp, pathReal);
log.info(`Successfully reset password for ${colors.bold(name)}.`);
}
);
});
}