2026-10-13 02:33:23 +08:00
|
|
|
import { z } from "incur";
|
|
|
|
|
import { createSqliteDatabaseService } from "../composition/sqlite.js";
|
|
|
|
|
import { CLI_COMMANDS } from "../cliCommandNames.js";
|
|
|
|
|
import { logCliCommand } from "../cliLogging.js";
|
|
|
|
|
import type { Logger } from "pino";
|
|
|
|
|
import type { CliInstance } from "./types.js";
|
|
|
|
|
import { withCommandCancellation } from "./commandCancellation.js";
|
2026-10-24 07:28:09 +08:00
|
|
|
import { localPathStringSchema } from "../domain/localPath.js";
|
2026-10-15 05:19:43 -07:00
|
|
|
import { absoluteCallerPath } from "../filesystem/callerPath.js";
|
2026-10-16 05:09:35 +08:00
|
|
|
import { finishAnalysisCommand } from "./finishAnalysisCommand.js";
|
2026-10-21 16:45:46 +08:00
|
|
|
import { inspectSqliteDatabaseInputSchema } from "../domain/sqlite/sqliteDatabase.js";
|
2026-10-13 02:33:23 +08:00
|
|
|
|
|
|
|
|
/** Resolve an operator-selected SQLite snapshot through the shared inspection workflow. */
|
|
|
|
|
export const registerSqliteCommands = (
|
|
|
|
|
cli: CliInstance,
|
|
|
|
|
logger: Logger,
|
|
|
|
|
environment: Readonly<NodeJS.ProcessEnv>,
|
|
|
|
|
): void => {
|
|
|
|
|
const service = createSqliteDatabaseService(environment);
|
|
|
|
|
cli.command(CLI_COMMANDS.inspectSqliteDatabase, {
|
|
|
|
|
description:
|
|
|
|
|
"Inspect SQLite schema and optional ordinary-table rows from a local snapshot",
|
|
|
|
|
args: z.object({
|
2026-10-24 07:28:09 +08:00
|
|
|
path: localPathStringSchema.describe("Local SQLite database snapshot"),
|
2026-10-13 02:33:23 +08:00
|
|
|
}),
|
|
|
|
|
options: z.object({
|
2026-10-21 16:45:46 +08:00
|
|
|
workerTimeoutMs: inspectSqliteDatabaseInputSchema.shape.worker_timeout_ms,
|
2026-10-13 02:33:23 +08:00
|
|
|
table: z
|
|
|
|
|
.string()
|
|
|
|
|
.optional()
|
|
|
|
|
.describe("Exact ordinary table name to read"),
|
|
|
|
|
column: z
|
|
|
|
|
.array(z.string())
|
|
|
|
|
.optional()
|
|
|
|
|
.describe(
|
|
|
|
|
"Repeat to select exact row columns in the requested order; requires table",
|
|
|
|
|
),
|
|
|
|
|
schemaScope: z
|
|
|
|
|
.enum(["inventory", "selected_table", "database"])
|
|
|
|
|
.optional()
|
|
|
|
|
.describe(
|
|
|
|
|
"Compact inventory by default, or selected-table details when table is supplied; database requests full schema",
|
|
|
|
|
),
|
|
|
|
|
rowLimit: z
|
|
|
|
|
.number()
|
|
|
|
|
.int()
|
|
|
|
|
.min(1)
|
|
|
|
|
.optional()
|
|
|
|
|
.describe(
|
|
|
|
|
"Positive safe integer row count; default 20; requires table",
|
|
|
|
|
),
|
|
|
|
|
}),
|
|
|
|
|
run: ({ args, options }) =>
|
|
|
|
|
withCommandCancellation((signal) =>
|
2026-10-16 05:09:35 +08:00
|
|
|
logCliCommand(logger, CLI_COMMANDS.inspectSqliteDatabase, () =>
|
|
|
|
|
finishAnalysisCommand(
|
|
|
|
|
() =>
|
|
|
|
|
service.inspect(
|
|
|
|
|
{
|
|
|
|
|
path: absoluteCallerPath(args.path),
|
2026-10-21 16:45:46 +08:00
|
|
|
...(options.workerTimeoutMs === undefined
|
|
|
|
|
? {}
|
|
|
|
|
: { worker_timeout_ms: options.workerTimeoutMs }),
|
2026-10-16 05:09:35 +08:00
|
|
|
...(options.table === undefined
|
|
|
|
|
? {}
|
|
|
|
|
: { table: options.table }),
|
|
|
|
|
...(options.rowLimit === undefined
|
|
|
|
|
? {}
|
|
|
|
|
: { row_limit: options.rowLimit }),
|
|
|
|
|
...(options.column === undefined
|
|
|
|
|
? {}
|
|
|
|
|
: { columns: options.column }),
|
|
|
|
|
...(options.schemaScope === undefined
|
|
|
|
|
? {}
|
|
|
|
|
: { schema_scope: options.schemaScope }),
|
|
|
|
|
},
|
|
|
|
|
{ signal },
|
|
|
|
|
),
|
|
|
|
|
service,
|
|
|
|
|
"sqlite",
|
|
|
|
|
"inspect_sqlite_database",
|
2026-10-18 09:48:11 +08:00
|
|
|
signal,
|
2026-10-16 05:09:35 +08:00
|
|
|
),
|
|
|
|
|
),
|
2026-10-13 02:33:23 +08:00
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
};
|