-
Notifications
You must be signed in to change notification settings - Fork 0
/
whitelist.ts
103 lines (91 loc) · 2.58 KB
/
whitelist.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
async function addUser(userId: string) {
await prisma.whitelistedUser.create({
data: { userId },
});
console.log(`Added ${userId} to whitelist.`);
}
async function removeUser(userId: string) {
await prisma.whitelistedUser.delete({
where: { userId },
});
console.log(`Removed ${userId} from whitelist.`);
}
async function listUsers() {
const users = await prisma.whitelistedUser.findMany();
console.log("Whitelisted Users:");
users.forEach((user) =>
console.log(`${user.userId}${user.isAdmin ? " (Admin)" : ""}`)
);
}
async function addAdmin(userId: string) {
await prisma.whitelistedUser.upsert({
where: { userId },
update: { isAdmin: true },
create: { userId, isAdmin: true },
});
console.log(`Added ${userId} as an admin.`);
}
async function removeAdmin(userId: string) {
await prisma.whitelistedUser.update({
where: { userId },
data: { isAdmin: false },
});
console.log(`Removed admin status from ${userId}.`);
}
function printHelp() {
console.log(`
Usage: bun run whitelist <command> [userId]
Commands:
add <userId> Add a user to the whitelist
remove <userId> Remove a user from the whitelist
list List all whitelisted users
addadmin <userId> Add a user as an admin
removeadmin <userId> Remove admin status from a user
help Show this help message
Example:
bun run whitelist add [email protected]
`);
}
async function main() {
const args = process.argv.slice(2);
const command = args[0];
const userId = args[1];
try {
switch (command) {
case "add":
if (!userId) throw new Error("UserId is required for add command");
await addUser(userId);
break;
case "remove":
if (!userId) throw new Error("UserId is required for remove command");
await removeUser(userId);
break;
case "list":
await listUsers();
break;
case "addadmin":
if (!userId) throw new Error("UserId is required for addadmin command");
await addAdmin(userId);
break;
case "removeadmin":
if (!userId)
throw new Error("UserId is required for removeadmin command");
await removeAdmin(userId);
break;
case "help":
case "--help":
printHelp();
break;
default:
console.log("Invalid command. Use 'help' for usage information.");
printHelp();
}
} catch (error) {
console.error("Error:", error);
} finally {
await prisma.$disconnect();
}
}
main();