Add --json and --csv to CLI

This commit is contained in:
Jesse Wierzbinski 2023-11-29 08:56:07 -10:00
parent 91838e7aec
commit 5ddacdbce5
No known key found for this signature in database

94
cli.ts
View file

@ -62,6 +62,8 @@ ${chalk.bold("Commands:")}
${alignDotsSmall( ${alignDotsSmall(
chalk.yellow("--email") chalk.yellow("--email")
)} Search in emails (optional) )} Search in emails (optional)
${alignDotsSmall(chalk.yellow("--json"))} Output as JSON (optional)
${alignDotsSmall(chalk.yellow("--csv"))} Output as CSV (optional)
${chalk.bold("Example:")} ${chalk.bgGray( ${chalk.bold("Example:")} ${chalk.bgGray(
`bun cli user search admin` `bun cli user search admin`
)} )}
@ -201,6 +203,8 @@ switch (command) {
const local = args.includes("--local"); const local = args.includes("--local");
const remote = args.includes("--remote"); const remote = args.includes("--remote");
const email = args.includes("--email"); const email = args.includes("--email");
const json = args.includes("--json");
const csv = args.includes("--csv");
const queries: Prisma.UserWhereInput[] = []; const queries: Prisma.UserWhereInput[] = [];
@ -254,36 +258,74 @@ switch (command) {
}, },
}); });
console.log( if (json || csv) {
`${chalk.green(``)} Found ${chalk.blue( if (json) {
users.length console.log(JSON.stringify(users, null, 4));
)} users` }
); if (csv) {
// Convert the outputted JSON to CSV
const table = new Table({ // Remove all object children from each object
head: [ const items = users.map(user => {
chalk.white(chalk.bold("Username")), const item = {
chalk.white(chalk.bold("Email")), ...user,
chalk.white(chalk.bold("Display Name")), instance: undefined,
chalk.white(chalk.bold("Admin?")), endpoints: undefined,
chalk.white(chalk.bold("Instance URL")), source: undefined,
], };
}); return item;
});
const replacer = (key: string, value: any): any =>
value === null ? "" : value; // Null values are returned as empty strings
const header = Object.keys(items[0]);
const csv = [
header.join(","), // header row first
...items.map(row =>
header
.map(fieldName =>
// @ts-expect-error This is fine
JSON.stringify(row[fieldName], replacer)
)
.join(",")
),
].join("\r\n");
for (const user of users) { console.log(csv);
table.push([ }
chalk.yellow(`@${user.username}`), } else {
chalk.green(user.email), console.log(
chalk.blue(user.displayName), `${chalk.green(``)} Found ${chalk.blue(
chalk.red(user.isAdmin ? "Yes" : "No"), users.length
chalk.blue( )} users`
user.instanceId ? user.instance?.base_url : "Local" );
),
]); const table = new Table({
head: [
chalk.white(chalk.bold("Username")),
chalk.white(chalk.bold("Email")),
chalk.white(chalk.bold("Display Name")),
chalk.white(chalk.bold("Admin?")),
chalk.white(chalk.bold("Instance URL")),
],
});
for (const user of users) {
table.push([
chalk.yellow(`@${user.username}`),
chalk.green(user.email),
chalk.blue(user.displayName),
chalk.red(user.isAdmin ? "Yes" : "No"),
chalk.blue(
user.instanceId
? user.instance?.base_url
: "Local"
),
]);
}
console.log(table.toString());
} }
console.log(table.toString());
break; break;
} }
default: default: