|
| 1 | +import type { Argv } from "yargs" |
| 2 | +import { spawn } from "child_process" |
| 3 | +import { Database } from "../../storage/db" |
| 4 | +import { Database as BunDatabase } from "bun:sqlite" |
| 5 | +import { UI } from "../ui" |
| 6 | +import { cmd } from "./cmd" |
| 7 | + |
| 8 | +const QueryCommand = cmd({ |
| 9 | + command: "$0 [query]", |
| 10 | + describe: "open an interactive sqlite3 shell or run a query", |
| 11 | + builder: (yargs: Argv) => { |
| 12 | + return yargs |
| 13 | + .positional("query", { |
| 14 | + type: "string", |
| 15 | + describe: "SQL query to execute", |
| 16 | + }) |
| 17 | + .option("format", { |
| 18 | + type: "string", |
| 19 | + choices: ["json", "tsv"], |
| 20 | + default: "tsv", |
| 21 | + describe: "Output format", |
| 22 | + }) |
| 23 | + }, |
| 24 | + handler: async (args: { query?: string; format: string }) => { |
| 25 | + const query = args.query as string | undefined |
| 26 | + if (query) { |
| 27 | + const db = new BunDatabase(Database.Path, { readonly: true }) |
| 28 | + try { |
| 29 | + const result = db.query(query).all() as Record<string, unknown>[] |
| 30 | + if (args.format === "json") { |
| 31 | + console.log(JSON.stringify(result, null, 2)) |
| 32 | + } else if (result.length > 0) { |
| 33 | + const keys = Object.keys(result[0]) |
| 34 | + console.log(keys.join("\t")) |
| 35 | + for (const row of result) { |
| 36 | + console.log(keys.map((k) => row[k]).join("\t")) |
| 37 | + } |
| 38 | + } |
| 39 | + } catch (err) { |
| 40 | + UI.error(err instanceof Error ? err.message : String(err)) |
| 41 | + process.exit(1) |
| 42 | + } |
| 43 | + db.close() |
| 44 | + return |
| 45 | + } |
| 46 | + const child = spawn("sqlite3", [Database.Path], { |
| 47 | + stdio: "inherit", |
| 48 | + }) |
| 49 | + await new Promise((resolve) => child.on("close", resolve)) |
| 50 | + }, |
| 51 | +}) |
| 52 | + |
| 53 | +const PathCommand = cmd({ |
| 54 | + command: "path", |
| 55 | + describe: "print the database path", |
| 56 | + handler: () => { |
| 57 | + console.log(Database.Path) |
| 58 | + }, |
| 59 | +}) |
| 60 | + |
| 61 | +export const DbCommand = cmd({ |
| 62 | + command: "db", |
| 63 | + describe: "database tools", |
| 64 | + builder: (yargs: Argv) => { |
| 65 | + return yargs.command(QueryCommand).command(PathCommand).demandCommand() |
| 66 | + }, |
| 67 | + handler: () => {}, |
| 68 | +}) |
0 commit comments