mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 16:38:19 +01:00
Add --json and --csv to CLI
This commit is contained in:
parent
91838e7aec
commit
5ddacdbce5
44
cli.ts
44
cli.ts
|
|
@ -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,6 +258,41 @@ switch (command) {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (json || csv) {
|
||||||
|
if (json) {
|
||||||
|
console.log(JSON.stringify(users, null, 4));
|
||||||
|
}
|
||||||
|
if (csv) {
|
||||||
|
// Convert the outputted JSON to CSV
|
||||||
|
|
||||||
|
// Remove all object children from each object
|
||||||
|
const items = users.map(user => {
|
||||||
|
const item = {
|
||||||
|
...user,
|
||||||
|
instance: undefined,
|
||||||
|
endpoints: undefined,
|
||||||
|
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");
|
||||||
|
|
||||||
|
console.log(csv);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
console.log(
|
console.log(
|
||||||
`${chalk.green(`✓`)} Found ${chalk.blue(
|
`${chalk.green(`✓`)} Found ${chalk.blue(
|
||||||
users.length
|
users.length
|
||||||
|
|
@ -277,12 +316,15 @@ switch (command) {
|
||||||
chalk.blue(user.displayName),
|
chalk.blue(user.displayName),
|
||||||
chalk.red(user.isAdmin ? "Yes" : "No"),
|
chalk.red(user.isAdmin ? "Yes" : "No"),
|
||||||
chalk.blue(
|
chalk.blue(
|
||||||
user.instanceId ? user.instance?.base_url : "Local"
|
user.instanceId
|
||||||
|
? user.instance?.base_url
|
||||||
|
: "Local"
|
||||||
),
|
),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(table.toString());
|
console.log(table.toString());
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue