mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 22:09:16 +01:00
feat(cli): ♻️ Begin new CLI rewrite with oclif
This commit is contained in:
parent
7b05a34cce
commit
06c30b8af2
21 changed files with 569 additions and 11 deletions
90
packages/cli/commands/user/delete.ts
Normal file
90
packages/cli/commands/user/delete.ts
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
import { Flags } from "@oclif/core";
|
||||
import chalk from "chalk";
|
||||
import { formatArray } from "~packages/cli/utils/format";
|
||||
import confirm from "@inquirer/confirm";
|
||||
import ora from "ora";
|
||||
import { UserFinderCommand } from "~packages/cli/classes";
|
||||
|
||||
export default class UserDelete extends UserFinderCommand<typeof UserDelete> {
|
||||
static override description = "Deletes users";
|
||||
|
||||
static override examples = [
|
||||
"<%= config.bin %> <%= command.id %> johngastron --type username",
|
||||
"<%= config.bin %> <%= command.id %> 018ec11c-c6cb-7a67-bd20-a4c81bf42912",
|
||||
'<%= config.bin %> <%= command.id %> "*badword*" --pattern --type username',
|
||||
];
|
||||
|
||||
static override flags = {
|
||||
confirm: Flags.boolean({
|
||||
description:
|
||||
"Ask for confirmation before deleting the user (default yes)",
|
||||
allowNo: true,
|
||||
default: true,
|
||||
}),
|
||||
};
|
||||
|
||||
static override args = {
|
||||
identifier: UserFinderCommand.baseArgs.identifier,
|
||||
};
|
||||
|
||||
public async run(): Promise<void> {
|
||||
const { flags, args } = await this.parse(UserDelete);
|
||||
|
||||
const users = await this.findUsers();
|
||||
|
||||
if (!users || users.length === 0) {
|
||||
this.log(chalk.bold(`${chalk.red("✗")} No users found`));
|
||||
this.exit(1);
|
||||
}
|
||||
|
||||
// Display user
|
||||
flags.print &&
|
||||
this.log(
|
||||
chalk.bold(
|
||||
`${chalk.green("✓")} Found ${chalk.green(
|
||||
users.length,
|
||||
)} user(s)`,
|
||||
),
|
||||
);
|
||||
|
||||
flags.print &&
|
||||
this.log(
|
||||
formatArray(
|
||||
users.map((u) => u.getUser()),
|
||||
[
|
||||
"id",
|
||||
"username",
|
||||
"displayName",
|
||||
"createdAt",
|
||||
"updatedAt",
|
||||
"isAdmin",
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (flags.confirm) {
|
||||
const choice = await confirm({
|
||||
message: `Are you sure you want to delete these users? ${chalk.red(
|
||||
"This is irreversible.",
|
||||
)}`,
|
||||
});
|
||||
|
||||
if (!choice) {
|
||||
this.log(chalk.bold(`${chalk.red("✗")} Aborted operation`));
|
||||
return this.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
const spinner = ora("Deleting user(s)").start();
|
||||
|
||||
for (const user of users) {
|
||||
await user.delete();
|
||||
}
|
||||
|
||||
spinner.stop();
|
||||
|
||||
this.log(chalk.bold(`${chalk.green("✓")} User(s) deleted`));
|
||||
|
||||
this.exit(0);
|
||||
}
|
||||
}
|
||||
83
packages/cli/commands/user/list.ts
Normal file
83
packages/cli/commands/user/list.ts
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
import { Flags } from "@oclif/core";
|
||||
import { and, eq, isNotNull, isNull } from "drizzle-orm";
|
||||
import { Users } from "~drizzle/schema";
|
||||
import { BaseCommand } from "~packages/cli/base";
|
||||
import { formatArray } from "~packages/cli/utils/format";
|
||||
import { User } from "~packages/database-interface/user";
|
||||
|
||||
export default class UserList extends BaseCommand<typeof UserList> {
|
||||
static override args = {};
|
||||
|
||||
static override description = "List all users";
|
||||
|
||||
static override examples = [
|
||||
"<%= config.bin %> <%= command.id %> --format json --local",
|
||||
"<%= config.bin %> <%= command.id %>",
|
||||
];
|
||||
|
||||
static override flags = {
|
||||
format: Flags.string({
|
||||
char: "f",
|
||||
description: "Output format",
|
||||
options: ["json", "csv"],
|
||||
}),
|
||||
local: Flags.boolean({
|
||||
char: "l",
|
||||
description: "Local users only",
|
||||
exclusive: ["remote"],
|
||||
}),
|
||||
remote: Flags.boolean({
|
||||
char: "r",
|
||||
description: "Remote users only",
|
||||
exclusive: ["local"],
|
||||
}),
|
||||
limit: Flags.integer({
|
||||
char: "n",
|
||||
description: "Limit the number of users",
|
||||
default: 200,
|
||||
}),
|
||||
admin: Flags.boolean({
|
||||
char: "a",
|
||||
description: "Admin users only",
|
||||
allowNo: true,
|
||||
}),
|
||||
"pretty-dates": Flags.boolean({
|
||||
char: "p",
|
||||
description: "Pretty print dates",
|
||||
}),
|
||||
};
|
||||
|
||||
public async run(): Promise<void> {
|
||||
const { flags } = await this.parse(UserList);
|
||||
|
||||
const users = await User.manyFromSql(
|
||||
and(
|
||||
flags.local ? isNull(Users.instanceId) : undefined,
|
||||
flags.remote ? isNotNull(Users.instanceId) : undefined,
|
||||
flags.admin ? eq(Users.isAdmin, flags.admin) : undefined,
|
||||
),
|
||||
undefined,
|
||||
flags.limit,
|
||||
);
|
||||
|
||||
const keys = [
|
||||
"id",
|
||||
"username",
|
||||
"displayName",
|
||||
"createdAt",
|
||||
"updatedAt",
|
||||
"isAdmin",
|
||||
];
|
||||
|
||||
this.log(
|
||||
formatArray(
|
||||
users.map((u) => u.getUser()),
|
||||
keys,
|
||||
flags.format as "json" | "csv" | undefined,
|
||||
flags["pretty-dates"],
|
||||
),
|
||||
);
|
||||
|
||||
this.exit(0);
|
||||
}
|
||||
}
|
||||
114
packages/cli/commands/user/reset.ts
Normal file
114
packages/cli/commands/user/reset.ts
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
import { Flags } from "@oclif/core";
|
||||
import chalk from "chalk";
|
||||
import { formatArray } from "~packages/cli/utils/format";
|
||||
import confirm from "@inquirer/confirm";
|
||||
import { renderUnicodeCompact } from "uqr";
|
||||
import { UserFinderCommand } from "~packages/cli/classes";
|
||||
|
||||
export default class UserReset extends UserFinderCommand<typeof UserReset> {
|
||||
static override description = "Resets users' passwords";
|
||||
|
||||
static override examples = [
|
||||
"<%= config.bin %> <%= command.id %> johngastron --type username",
|
||||
"<%= config.bin %> <%= command.id %> 018ec11c-c6cb-7a67-bd20-a4c81bf42912",
|
||||
];
|
||||
|
||||
static override flags = {
|
||||
confirm: Flags.boolean({
|
||||
description:
|
||||
"Ask for confirmation before deleting the user (default yes)",
|
||||
allowNo: true,
|
||||
default: true,
|
||||
}),
|
||||
limit: Flags.integer({
|
||||
char: "n",
|
||||
description: "Limit the number of users",
|
||||
default: 1,
|
||||
}),
|
||||
raw: Flags.boolean({
|
||||
description:
|
||||
"Only output the password reset link (implies --no-print and --no-confirm)",
|
||||
}),
|
||||
};
|
||||
|
||||
static override args = {
|
||||
identifier: UserFinderCommand.baseArgs.identifier,
|
||||
};
|
||||
|
||||
public async run(): Promise<void> {
|
||||
const { flags, args } = await this.parse(UserReset);
|
||||
|
||||
const users = await this.findUsers();
|
||||
|
||||
if (!users || users.length === 0) {
|
||||
this.log(chalk.bold(`${chalk.red("✗")} No users found`));
|
||||
this.exit(1);
|
||||
}
|
||||
|
||||
// Display user
|
||||
!flags.raw &&
|
||||
this.log(
|
||||
chalk.bold(
|
||||
`${chalk.green("✓")} Found ${chalk.green(
|
||||
users.length,
|
||||
)} user(s)`,
|
||||
),
|
||||
);
|
||||
|
||||
!flags.raw &&
|
||||
flags.print &&
|
||||
this.log(
|
||||
formatArray(
|
||||
users.map((u) => u.getUser()),
|
||||
[
|
||||
"id",
|
||||
"username",
|
||||
"displayName",
|
||||
"createdAt",
|
||||
"updatedAt",
|
||||
"isAdmin",
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (flags.confirm && !flags.raw) {
|
||||
const choice = await confirm({
|
||||
message: `Reset these users's passwords? ${chalk.red(
|
||||
"This is irreversible.",
|
||||
)}`,
|
||||
});
|
||||
|
||||
if (!choice) {
|
||||
this.log(chalk.bold(`${chalk.red("✗")} Aborted operation`));
|
||||
return this.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
const link = "https://example.com/reset-password";
|
||||
|
||||
!flags.raw &&
|
||||
this.log(
|
||||
`${chalk.green("✓")} Password reset for ${
|
||||
users.length
|
||||
} user(s)`,
|
||||
);
|
||||
|
||||
this.log(
|
||||
flags.raw
|
||||
? link
|
||||
: `\nPassword reset link for ${chalk.bold(
|
||||
"@testuser",
|
||||
)}: ${chalk.underline(chalk.blue(link))}\n`,
|
||||
);
|
||||
|
||||
const qrcode = renderUnicodeCompact(link, {
|
||||
border: 2,
|
||||
});
|
||||
|
||||
// Pad all lines of QR code with spaces
|
||||
|
||||
!flags.raw && this.log(` ${qrcode.replaceAll("\n", "\n ")}`);
|
||||
|
||||
this.exit(0);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue