simple-fs-server/user.ts
2023-01-05 18:18:07 +09:00

63 lines
2.4 KiB
TypeScript

import { Command, Input, Secret } from "https://deno.land/x/cliffy@v0.25.6/mod.ts";
import { connectDB } from "./src/user/db.ts";
import * as users from "./src/user/user.ts";
export const user_command = new Command()
.description("Manage users.")
.command("add", "add a user")
.arguments("[username:string]")
.option("-p, --password <password:string>", "The password for the user.")
.option("-q, --quiet", "quiet output.")
.action(async ({quiet, password}
, username) => {
if(username === undefined){
username = await Input.prompt("Username: ");
}
if(password === undefined){
password = await Secret.prompt("Password: ");
const password2 = await Secret.prompt("Confirm password: ");
if (password !== password2){
console.error("Passwords do not match.");
Deno.exit(1);
}
}
const db = connectDB();
const new_user = await users.createUser( username, password);
await users.addUser(db, new_user);
if (!quiet){
console.log(`Added user ${username}`);
}
})
.command("delete", "delete a user")
.arguments("<username:string>")
.option("-q, --quiet", "Quiet output.")
.action(async ({quiet}, username) => {
const db = connectDB();
await users.deleteUser(db, username);
if (!quiet){
console.log(`Deleting user ${username}`);
}
})
.command("list", "list all users")
.action(async () => {
const db = connectDB();
const all_users = await users.getAllUsers(db);
for (const user of all_users){
console.log(`${user.name}`);
}
})
.command("reset", "reset a user's password")
.arguments("<username:string> <password:string>")
.option("-q, --quiet", "quiet output.")
.action(async ({quiet}, [username, password]) => {
const db = connectDB();
const new_user = await users.createUser( username, password);
await users.updateUser(db, new_user);
if (!quiet){
console.log(`Resetting password for user ${username}`);
}
});
if (import.meta.main){
await user_command.parse(Deno.args);
}