Add setuser to cli

This commit is contained in:
Ethanell 2020-04-28 23:05:53 +02:00
parent 3713240618
commit e683096a90
2 changed files with 21 additions and 5 deletions

20
cli.js
View file

@ -1,7 +1,7 @@
let yargs = require("yargs"); let yargs = require("yargs");
let user = require("./user"); let user = require("./user");
yargs.scriptName("node cli.js") yargs.scriptName("node app.js")
.command("adduser [username] [password]", "Add user", (yargs) => { .command("adduser [username] [password]", "Add user", (yargs) => {
yargs.positional("username", { yargs.positional("username", {
describe: "Username for the new user", describe: "Username for the new user",
@ -22,6 +22,22 @@ yargs.scriptName("node cli.js")
user.setUser(argv.username, argv.password); user.setUser(argv.username, argv.password);
process.exit(0); process.exit(0);
}) })
.command("setuser <username> [password]", "Change an user password", (yargs) => {
yargs.positional("username", {
describe: "The user to change password",
type: "string"
});
}, async (argv) => {
let file = user.getFile();
if (!(argv.username in file)) {
console.error("Invalid username !")
process.exit(1);
}
if (!("password" in argv))
argv.password = await user.getPassword();
user.setUser(argv.username, argv.password);
process.exit(0);
})
.command("deluser <username>", "Remove user", (yargs) => { .command("deluser <username>", "Remove user", (yargs) => {
yargs.positional("username", { yargs.positional("username", {
type: "string", type: "string",
@ -30,7 +46,7 @@ yargs.scriptName("node cli.js")
}, async (argv) => { }, async (argv) => {
process.exit(user.delUser(argv.username)); process.exit(user.delUser(argv.username));
}) })
.command("listuser", "List all users", (argv) => { .command("listuser", "List all users", () => {
let users = []; let users = [];
let file = user.getFile(); let file = user.getFile();
for (let u in file) for (let u in file)

View file

@ -36,14 +36,14 @@ async function getPassword() {
let rl = getReadLine(); let rl = getReadLine();
let password = await new Promise(resolve => rl.question("Password: ", resolve)); let password = await new Promise(resolve => rl.question("Password: ", resolve));
rl.close(); rl.close();
return passwordHash.generate(password); return password;
} }
function setUser(username, password) { function setUser(username, password) {
let file = getFile(); let file = getFile();
file[username] = password; file[username] = passwordHash.generate(password);
fs.writeFileSync("users.json", JSON.stringify(file)); fs.writeFileSync("users.json", JSON.stringify(file));
console.log("User " + username + " created"); console.log("User " + username + " set");
} }
async function addUser() { async function addUser() {