2023-01-06 18:24:27 +09:00
|
|
|
import {
|
|
|
|
Command,
|
|
|
|
Input,
|
|
|
|
Secret,
|
|
|
|
} from "https://deno.land/x/cliffy@v0.25.6/mod.ts";
|
2023-01-05 18:18:07 +09:00
|
|
|
import { connectDB } from "./src/user/db.ts";
|
|
|
|
import * as users from "./src/user/user.ts";
|
|
|
|
|
|
|
|
export const user_command = new Command()
|
2023-01-06 18:24:27 +09:00
|
|
|
.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);
|
|
|
|
}
|
|
|
|
}
|
2023-01-06 23:22:00 +09:00
|
|
|
const db = await connectDB();
|
2023-01-06 18:24:27 +09:00
|
|
|
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) => {
|
2023-01-06 23:22:00 +09:00
|
|
|
const db = await connectDB();
|
2023-01-06 18:24:27 +09:00
|
|
|
await users.deleteUser(db, username);
|
|
|
|
if (!quiet) {
|
|
|
|
console.log(`Deleting user ${username}`);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.command("list", "list all users")
|
|
|
|
.action(async () => {
|
2023-01-06 23:22:00 +09:00
|
|
|
const db = await connectDB();
|
2023-01-06 18:24:27 +09:00
|
|
|
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]) => {
|
2023-01-06 23:22:00 +09:00
|
|
|
const db = await connectDB();
|
2023-01-06 18:24:27 +09:00
|
|
|
const new_user = await users.createUser(username, password);
|
|
|
|
await users.updateUser(db, new_user);
|
|
|
|
if (!quiet) {
|
|
|
|
console.log(`Resetting password for user ${username}`);
|
|
|
|
}
|
|
|
|
});
|
2023-01-05 18:18:07 +09:00
|
|
|
|
2023-01-06 18:24:27 +09:00
|
|
|
if (import.meta.main) {
|
|
|
|
await user_command.parse(Deno.args);
|
|
|
|
}
|