@lark-apaas/miaoda-cli 0.1.2-alpha.ccfdc05 → 0.1.2-alpha.d5d1878
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -8
- package/dist/api/db/api.js +317 -6
- package/dist/api/db/client.js +36 -0
- package/dist/api/db/index.js +11 -1
- package/dist/api/file/api.js +15 -0
- package/dist/api/file/index.js +2 -1
- package/dist/api/index.js +1 -7
- package/dist/cli/commands/db/index.js +137 -0
- package/dist/cli/commands/file/index.js +7 -0
- package/dist/cli/commands/index.js +6 -10
- package/dist/cli/commands/shared.js +3 -81
- package/dist/cli/handlers/db/audit.js +285 -0
- package/dist/cli/handlers/db/changelog.js +117 -0
- package/dist/cli/handlers/db/data.js +1 -1
- package/dist/cli/handlers/db/index.js +17 -1
- package/dist/cli/handlers/db/migration.js +235 -0
- package/dist/cli/handlers/{app/update.js → db/quota.js} +29 -20
- package/dist/cli/handlers/db/recovery.js +281 -0
- package/dist/cli/handlers/file/index.js +3 -1
- package/dist/cli/handlers/{deploy/error-log.js → file/quota.js} +27 -22
- package/dist/main.js +7 -27
- package/dist/utils/http.js +0 -118
- package/dist/utils/index.js +3 -13
- package/dist/utils/output.js +29 -338
- package/dist/utils/poll.js +27 -0
- package/package.json +5 -7
- package/dist/api/app/api.js +0 -25
- package/dist/api/app/index.js +0 -15
- package/dist/api/app/schemas.js +0 -79
- package/dist/api/app/types.js +0 -58
- package/dist/api/deploy/api.js +0 -60
- package/dist/api/deploy/index.js +0 -16
- package/dist/api/deploy/schemas.js +0 -103
- package/dist/api/deploy/types.js +0 -22
- package/dist/api/observability/api.js +0 -52
- package/dist/api/observability/index.js +0 -16
- package/dist/api/observability/schemas.js +0 -60
- package/dist/api/observability/types.js +0 -27
- package/dist/cli/commands/app/index.js +0 -62
- package/dist/cli/commands/deploy/index.js +0 -145
- package/dist/cli/commands/observability/index.js +0 -237
- package/dist/cli/handlers/app/get.js +0 -48
- package/dist/cli/handlers/app/index.js +0 -7
- package/dist/cli/handlers/deploy/deploy.js +0 -83
- package/dist/cli/handlers/deploy/get.js +0 -70
- package/dist/cli/handlers/deploy/helpers.js +0 -41
- package/dist/cli/handlers/deploy/history.js +0 -70
- package/dist/cli/handlers/deploy/index.js +0 -14
- package/dist/cli/handlers/deploy/polling.js +0 -139
- package/dist/cli/handlers/observability/analytics.js +0 -212
- package/dist/cli/handlers/observability/helpers.js +0 -66
- package/dist/cli/handlers/observability/index.js +0 -12
- package/dist/cli/handlers/observability/log.js +0 -94
- package/dist/cli/handlers/observability/metric.js +0 -208
- package/dist/cli/handlers/observability/trace.js +0 -102
- package/dist/cli/version.js +0 -15
- package/dist/utils/devops-error.js +0 -28
- package/dist/utils/git.js +0 -29
- package/dist/utils/time.js +0 -203
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.registerDbCommands = registerDbCommands;
|
|
4
|
+
const error_1 = require("../../../utils/error");
|
|
4
5
|
const index_1 = require("../../../cli/handlers/db/index");
|
|
6
|
+
function parsePositiveInt(raw) {
|
|
7
|
+
const n = Number(raw);
|
|
8
|
+
if (!Number.isInteger(n) || n < 1) {
|
|
9
|
+
throw new error_1.AppError("ARGS_INVALID", `--limit must be a positive integer (got '${raw}')`);
|
|
10
|
+
}
|
|
11
|
+
return n;
|
|
12
|
+
}
|
|
5
13
|
function registerDbCommands(program) {
|
|
6
14
|
const dbCmd = program
|
|
7
15
|
.command("db")
|
|
@@ -206,4 +214,133 @@ Examples:
|
|
|
206
214
|
Error: Output file 'users.csv' already exists
|
|
207
215
|
hint: Use -f to specify a different path, or --force to overwrite.
|
|
208
216
|
`);
|
|
217
|
+
// ── changelog ──
|
|
218
|
+
dbCmd
|
|
219
|
+
.command("changelog")
|
|
220
|
+
.summary("查看 DDL 变更历史")
|
|
221
|
+
.description("列出 DDL 变更(CREATE / ALTER / DROP),支持按表、时间窗筛选与游标分页。")
|
|
222
|
+
.usage("[flags]")
|
|
223
|
+
.option("--table <name>", "只看某张表的 DDL 变更")
|
|
224
|
+
.option("--since <time>", "起始时间(含),相对 1h/2d / 日期 YYYY-MM-DD / ISO 8601")
|
|
225
|
+
.option("--until <time>", "结束时间(含),同 --since 格式")
|
|
226
|
+
.option("--limit <n>", "单次返回上限(默认 20)", parsePositiveInt, 20)
|
|
227
|
+
.option("--cursor <token>", "分页游标,从上次响应的 next_cursor 取值")
|
|
228
|
+
.option("--all", "自动翻页直到取完所有记录")
|
|
229
|
+
.action(async function () {
|
|
230
|
+
await (0, index_1.handleDbChangelog)(this.optsWithGlobals());
|
|
231
|
+
});
|
|
232
|
+
// ── audit ──
|
|
233
|
+
const auditCmd = dbCmd
|
|
234
|
+
.command("audit")
|
|
235
|
+
.summary("表级数据审计开关与查询")
|
|
236
|
+
.description("配置表的数据审计 (enable/disable/retention),查询审计状态与日志。")
|
|
237
|
+
.usage("<command> [flags]");
|
|
238
|
+
auditCmd.action(() => {
|
|
239
|
+
auditCmd.outputHelp();
|
|
240
|
+
});
|
|
241
|
+
auditCmd
|
|
242
|
+
.command("status")
|
|
243
|
+
.summary("查看审计开关状态(不传 table 返列表)")
|
|
244
|
+
.usage("[table] [flags]")
|
|
245
|
+
.argument("[table]", "表名;省略时返所有已配置审计的表")
|
|
246
|
+
.action(async function (table) {
|
|
247
|
+
await (0, index_1.handleDbAuditStatus)(table, this.optsWithGlobals());
|
|
248
|
+
});
|
|
249
|
+
auditCmd
|
|
250
|
+
.command("enable")
|
|
251
|
+
.summary("启用表审计")
|
|
252
|
+
.usage("<table> [flags]")
|
|
253
|
+
.argument("<table>", "目标表名")
|
|
254
|
+
.option("--retention <ttl>", "保留时长 7d / 30d / 180d / 360d / forever", "7d")
|
|
255
|
+
.action(async function (table) {
|
|
256
|
+
await (0, index_1.handleDbAuditEnable)(table, this.optsWithGlobals());
|
|
257
|
+
});
|
|
258
|
+
auditCmd
|
|
259
|
+
.command("disable")
|
|
260
|
+
.summary("关闭表审计")
|
|
261
|
+
.usage("<table> [flags]")
|
|
262
|
+
.argument("<table>", "目标表名")
|
|
263
|
+
.action(async function (table) {
|
|
264
|
+
await (0, index_1.handleDbAuditDisable)(table, this.optsWithGlobals());
|
|
265
|
+
});
|
|
266
|
+
auditCmd
|
|
267
|
+
.command("list")
|
|
268
|
+
.summary("查询审计日志")
|
|
269
|
+
.description("按表 + 时间窗查询审计日志(INSERT / UPDATE / DELETE)。")
|
|
270
|
+
.usage("<table...> [flags]")
|
|
271
|
+
.argument("<tables...>", "一个或多个表名(多表时空表会汇总到 stderr)")
|
|
272
|
+
.option("--since <time>", "起始时间")
|
|
273
|
+
.option("--until <time>", "结束时间")
|
|
274
|
+
.option("--limit <n>", "单次返回上限(默认 50)", parsePositiveInt, 50)
|
|
275
|
+
.option("--cursor <ts>", "分页游标,传上一页末条 event_time")
|
|
276
|
+
.action(async function (tables) {
|
|
277
|
+
await (0, index_1.handleDbAuditList)(tables, this.optsWithGlobals());
|
|
278
|
+
});
|
|
279
|
+
// ── migration ──
|
|
280
|
+
const migrationCmd = dbCmd
|
|
281
|
+
.command("migration")
|
|
282
|
+
.summary("多环境管理(dev / online,仅专家模式应用)")
|
|
283
|
+
.description("init 拆分双环境,diff 预览待发布变更,apply 把 dev 同步到 online。")
|
|
284
|
+
.usage("<command> [flags]");
|
|
285
|
+
migrationCmd.action(() => {
|
|
286
|
+
migrationCmd.outputHelp();
|
|
287
|
+
});
|
|
288
|
+
migrationCmd
|
|
289
|
+
.command("init")
|
|
290
|
+
.summary("初始化多环境(不可逆)")
|
|
291
|
+
.usage("[flags]")
|
|
292
|
+
.option("--sync-data", "同时把现有数据复制到 dev")
|
|
293
|
+
.option("--yes", "跳过 TTY 确认")
|
|
294
|
+
.action(async function () {
|
|
295
|
+
await (0, index_1.handleDbMigrationInit)(this.optsWithGlobals());
|
|
296
|
+
});
|
|
297
|
+
migrationCmd
|
|
298
|
+
.command("diff")
|
|
299
|
+
.summary("预览 dev → online 待发布变更")
|
|
300
|
+
.usage("[flags]")
|
|
301
|
+
.action(async function () {
|
|
302
|
+
await (0, index_1.handleDbMigrationDiff)(this.optsWithGlobals());
|
|
303
|
+
});
|
|
304
|
+
migrationCmd
|
|
305
|
+
.command("apply")
|
|
306
|
+
.summary("应用 dev 变更到 online(多 DDL 单事务原子)")
|
|
307
|
+
.usage("[flags]")
|
|
308
|
+
.option("--yes", "跳过 TTY 二次确认")
|
|
309
|
+
.action(async function () {
|
|
310
|
+
await (0, index_1.handleDbMigrationApply)(this.optsWithGlobals());
|
|
311
|
+
});
|
|
312
|
+
// ── recovery(PITR)──
|
|
313
|
+
const recoveryCmd = dbCmd
|
|
314
|
+
.command("recovery")
|
|
315
|
+
.summary("基于时间点恢复(PITR)")
|
|
316
|
+
.description("把数据库整体恢复到指定时间点,diff 预览影响范围,apply 不可逆覆盖。")
|
|
317
|
+
.usage("<command> [flags]");
|
|
318
|
+
recoveryCmd.action(() => {
|
|
319
|
+
recoveryCmd.outputHelp();
|
|
320
|
+
});
|
|
321
|
+
recoveryCmd
|
|
322
|
+
.command("diff")
|
|
323
|
+
.summary("预览恢复到指定时间点的影响范围")
|
|
324
|
+
.usage("<timestamp> [flags]")
|
|
325
|
+
.argument("<timestamp>", "目标时间,YYYY-MM-DD / YYYY-MM-DD HH:MM[:SS] / ISO 8601")
|
|
326
|
+
.action(async function (target) {
|
|
327
|
+
await (0, index_1.handleDbRecoveryDiff)(target, this.optsWithGlobals());
|
|
328
|
+
});
|
|
329
|
+
recoveryCmd
|
|
330
|
+
.command("apply")
|
|
331
|
+
.summary("触发恢复到指定时间点(不可逆覆盖)")
|
|
332
|
+
.usage("<timestamp> [flags]")
|
|
333
|
+
.argument("<timestamp>", "目标时间,YYYY-MM-DD / YYYY-MM-DD HH:MM[:SS] / ISO 8601")
|
|
334
|
+
.option("--yes", "跳过 TTY 二次确认")
|
|
335
|
+
.action(async function (target) {
|
|
336
|
+
await (0, index_1.handleDbRecoveryApply)(target, this.optsWithGlobals());
|
|
337
|
+
});
|
|
338
|
+
// ── quota ──
|
|
339
|
+
dbCmd
|
|
340
|
+
.command("quota")
|
|
341
|
+
.summary("查看数据库用量与限额")
|
|
342
|
+
.usage("[flags]")
|
|
343
|
+
.action(async function () {
|
|
344
|
+
await (0, index_1.handleDbQuota)(this.optsWithGlobals());
|
|
345
|
+
});
|
|
209
346
|
}
|
|
@@ -209,4 +209,11 @@ Examples:
|
|
|
209
209
|
Error: Expires duration '60d' exceeds the maximum of 30d
|
|
210
210
|
hint: Maximum allowed value is 30d. Use \`--expires 30d\` for the longest link.
|
|
211
211
|
`);
|
|
212
|
+
fileCmd
|
|
213
|
+
.command("quota")
|
|
214
|
+
.summary("查看文件存储用量与限额")
|
|
215
|
+
.usage("[flags]")
|
|
216
|
+
.action(async function () {
|
|
217
|
+
await (0, index_1.handleFileQuota)(this.optsWithGlobals());
|
|
218
|
+
});
|
|
212
219
|
}
|
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.registerCommands = registerCommands;
|
|
4
|
-
const index_1 = require("../../cli/commands/
|
|
5
|
-
const index_2 = require("../../cli/commands/
|
|
6
|
-
const index_3 = require("../../cli/commands/
|
|
7
|
-
const index_4 = require("../../cli/commands/app/index");
|
|
8
|
-
const index_5 = require("../../cli/commands/deploy/index");
|
|
4
|
+
const index_1 = require("../../cli/commands/plugin/index");
|
|
5
|
+
const index_2 = require("../../cli/commands/file/index");
|
|
6
|
+
const index_3 = require("../../cli/commands/db/index");
|
|
9
7
|
function registerCommands(program) {
|
|
10
|
-
(0, index_1.
|
|
11
|
-
(0, index_2.
|
|
12
|
-
(0, index_3.
|
|
13
|
-
(0, index_4.registerAppCommands)(program);
|
|
14
|
-
(0, index_5.registerDeployCommands)(program);
|
|
8
|
+
(0, index_1.registerPluginCommands)(program);
|
|
9
|
+
(0, index_2.registerFileCommands)(program);
|
|
10
|
+
(0, index_3.registerDbCommands)(program);
|
|
15
11
|
}
|
|
@@ -1,24 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.appIdOption = appIdOption;
|
|
4
|
-
exports.branchOption = branchOption;
|
|
5
3
|
exports.softRequiredOption = softRequiredOption;
|
|
6
4
|
exports.resolveAppId = resolveAppId;
|
|
7
5
|
exports.withHelp = withHelp;
|
|
8
6
|
exports.failArgs = failArgs;
|
|
9
|
-
exports.caseInsensitiveChoice = caseInsensitiveChoice;
|
|
10
|
-
exports.validateTimeOptions = validateTimeOptions;
|
|
11
|
-
exports.rejectCliOverride = rejectCliOverride;
|
|
12
7
|
const commander_1 = require("commander");
|
|
13
8
|
const error_1 = require("../../utils/error");
|
|
14
|
-
const time_1 = require("../../utils/time");
|
|
15
|
-
/** --app-id option,需要应用上下文的命令自行 .addOption(appIdOption()) */
|
|
16
|
-
function appIdOption() {
|
|
17
|
-
return new commander_1.Option("--app-id <id>", "指定目标应用").env("MIAODA_APP_ID");
|
|
18
|
-
}
|
|
19
|
-
function branchOption() {
|
|
20
|
-
return new commander_1.Option("--branch <branch>", "分支(优先级:--branch > 当前仓库 HEAD 分支;非 git 目录回退应用默认分支)").env("LOCAL_MOCK_MIAODA_DEPLOY_BRANCH");
|
|
21
|
-
}
|
|
22
9
|
/**
|
|
23
10
|
* soft-required: Commander 类型上 optional,runtime 校验必填。
|
|
24
11
|
*/
|
|
@@ -40,15 +27,11 @@ function resolveAppId(opts) {
|
|
|
40
27
|
}
|
|
41
28
|
return id;
|
|
42
29
|
}
|
|
43
|
-
/**
|
|
44
|
-
* 包裹 handler,缺 soft-required 参数 / 自定义校验失败时打 help 并退出。
|
|
45
|
-
*
|
|
46
|
-
* 兼容带位置参数的 action 签名(如 `(arg1: string, opts) => ...`)。
|
|
47
|
-
*/
|
|
30
|
+
/** 包裹 handler,缺 soft-required 参数时打 help 并退出 */
|
|
48
31
|
function withHelp(cmd, handler) {
|
|
49
|
-
return async (
|
|
32
|
+
return async (opts) => {
|
|
50
33
|
try {
|
|
51
|
-
await handler(
|
|
34
|
+
await handler(opts);
|
|
52
35
|
}
|
|
53
36
|
catch (err) {
|
|
54
37
|
if (err instanceof error_1.AppError && err.code === "ARGS_INVALID") {
|
|
@@ -65,64 +48,3 @@ function withHelp(cmd, handler) {
|
|
|
65
48
|
function failArgs(message) {
|
|
66
49
|
throw new error_1.AppError("ARGS_INVALID", message);
|
|
67
50
|
}
|
|
68
|
-
/**
|
|
69
|
-
* 大小写不敏感的 choice argParser:把用户输入按 lowerCase 匹配回 canonical 数组里
|
|
70
|
-
* 的同名值(保留 canonical 大小写),未命中抛 Commander 的 InvalidArgumentError
|
|
71
|
-
* (exit code 1,自带友好错误)。
|
|
72
|
-
*
|
|
73
|
-
* 必须先 .choices(canonical)、再 .argParser()——Commander 内部 .choices() 会
|
|
74
|
-
* 重置 parseArg,反过来调会让 argParser 失效。.choices() 仅留给 help 渲染
|
|
75
|
-
* "choices: A, B, C",实际白名单校验由 argParser 接管。
|
|
76
|
-
*
|
|
77
|
-
* new Option("--level <level>", "日志级别(不区分大小写)")
|
|
78
|
-
* .choices(["DEBUG", "INFO", "WARN", "ERROR"])
|
|
79
|
-
* .argParser(caseInsensitiveChoice(["DEBUG", "INFO", "WARN", "ERROR"]));
|
|
80
|
-
*/
|
|
81
|
-
function caseInsensitiveChoice(canonical) {
|
|
82
|
-
const map = new Map(canonical.map((v) => [v.toLowerCase(), v]));
|
|
83
|
-
return (raw) => {
|
|
84
|
-
const hit = map.get(raw.toLowerCase());
|
|
85
|
-
if (hit === undefined) {
|
|
86
|
-
throw new commander_1.InvalidArgumentError(`Allowed choices are ${canonical.join(", ")} (case-insensitive).`);
|
|
87
|
-
}
|
|
88
|
-
return hit;
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* --since / --until 等时间参数的 action 阶段校验。
|
|
93
|
-
*
|
|
94
|
-
* Commander 的 argParser 会在 action 前直接按 parser error 退出(exit 1),
|
|
95
|
-
* 绕过 withHelp 的 ARGS_INVALID → exit 2 路径;所以时间参数在 action 内先
|
|
96
|
-
* 校验一遍,再把原始字符串交给 handler 解析成接口需要的单位。
|
|
97
|
-
*/
|
|
98
|
-
function validateTimeOptions(opts, ...names) {
|
|
99
|
-
for (const name of names) {
|
|
100
|
-
const value = opts[name];
|
|
101
|
-
if (value === undefined)
|
|
102
|
-
continue;
|
|
103
|
-
if (typeof value !== "string") {
|
|
104
|
-
failArgs(`--${name.replace(/([A-Z])/g, "-$1").toLowerCase()} 必须是时间字符串`);
|
|
105
|
-
}
|
|
106
|
-
(0, time_1.parseTimeToMs)(value);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* 拒绝 CLI 显式覆盖:用于 hideHelp() + .env() 的"沙箱注入参数"(如 --app-id / --branch)。
|
|
111
|
-
*
|
|
112
|
-
* Commander 的 getOptionValueSource() 区分来源(cli / env / default / config / undefined);
|
|
113
|
-
* 用户在 CLI 上显式传时 source === "cli",此时抛 ARGS_INVALID(exit 2 + help)。
|
|
114
|
-
*
|
|
115
|
-
* 用法:在 action 头部一行调用——
|
|
116
|
-
* cmd.action(withHelp(cmd, async (rawOpts) => {
|
|
117
|
-
* rejectCliOverride(cmd, "appId", "branch");
|
|
118
|
-
* ...
|
|
119
|
-
* }));
|
|
120
|
-
*/
|
|
121
|
-
function rejectCliOverride(cmd, ...optNames) {
|
|
122
|
-
for (const name of optNames) {
|
|
123
|
-
if (cmd.getOptionValueSource(name) === "cli") {
|
|
124
|
-
const flag = "--" + name.replace(/([A-Z])/g, "-$1").toLowerCase();
|
|
125
|
-
failArgs(`${flag} 由沙箱/本地配置注入,不允许显式传递`);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.handleDbAuditStatus = handleDbAuditStatus;
|
|
37
|
+
exports.handleDbAuditEnable = handleDbAuditEnable;
|
|
38
|
+
exports.handleDbAuditDisable = handleDbAuditDisable;
|
|
39
|
+
exports.handleDbAuditList = handleDbAuditList;
|
|
40
|
+
const api = __importStar(require("../../../api/index"));
|
|
41
|
+
const index_1 = require("../../../api/file/index");
|
|
42
|
+
const shared_1 = require("../../../cli/commands/shared");
|
|
43
|
+
const colors_1 = require("../../../utils/colors");
|
|
44
|
+
const error_1 = require("../../../utils/error");
|
|
45
|
+
const output_1 = require("../../../utils/output");
|
|
46
|
+
const render_1 = require("../../../utils/render");
|
|
47
|
+
const VALID_RETENTION = new Set(["7d", "30d", "180d", "360d", "forever"]);
|
|
48
|
+
async function handleDbAuditStatus(table, opts) {
|
|
49
|
+
const appId = (0, shared_1.resolveAppId)(opts);
|
|
50
|
+
const items = await api.db.getAuditStatus({ appId, table, dbBranch: opts.env });
|
|
51
|
+
const rows = items.map(toAuditStatus);
|
|
52
|
+
// 单表查询但后端没记录 → 占位 enabled=false
|
|
53
|
+
if (table !== undefined && table !== "" && rows.length === 0) {
|
|
54
|
+
rows.push({ table, enabled: false, enabled_at: null, retention: null });
|
|
55
|
+
}
|
|
56
|
+
// PRD JSON:单表返 object,多表返 array
|
|
57
|
+
if ((0, output_1.isJsonMode)()) {
|
|
58
|
+
if (table !== undefined && rows.length === 1) {
|
|
59
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)(rows[0]));
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)(rows));
|
|
63
|
+
}
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (rows.length === 0) {
|
|
67
|
+
(0, output_1.emit)("No audit configuration found.");
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const tty = (0, render_1.isStdoutTty)();
|
|
71
|
+
// 单表 → key:value 形态
|
|
72
|
+
if (table !== undefined && rows.length === 1) {
|
|
73
|
+
const it = rows[0];
|
|
74
|
+
(0, output_1.emit)((0, render_1.renderKeyValue)([
|
|
75
|
+
["Table", it.table],
|
|
76
|
+
["Enabled", boolToYesNo(it.enabled)],
|
|
77
|
+
["Enabled at", it.enabled_at ? (0, render_1.formatTime)(it.enabled_at, tty) : "—"],
|
|
78
|
+
["Retention", it.retention ?? "—"],
|
|
79
|
+
], tty));
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
// 列表 → table 形态
|
|
83
|
+
const headers = ["table", "enabled", "enabled_at", "retention"];
|
|
84
|
+
const out = rows.map((it) => [
|
|
85
|
+
it.table,
|
|
86
|
+
boolToYesNo(it.enabled),
|
|
87
|
+
it.enabled_at ? (0, render_1.formatTime)(it.enabled_at, tty) : "—",
|
|
88
|
+
it.retention ?? "—",
|
|
89
|
+
]);
|
|
90
|
+
(0, output_1.emit)(tty ? (0, render_1.renderAlignedTable)(headers, out) : (0, render_1.renderTsv)(headers, out));
|
|
91
|
+
}
|
|
92
|
+
function toAuditStatus(s) {
|
|
93
|
+
return {
|
|
94
|
+
table: s.table,
|
|
95
|
+
enabled: s.enabled,
|
|
96
|
+
enabled_at: s.enabledAt ?? null,
|
|
97
|
+
retention: s.retention ?? null,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
async function handleDbAuditEnable(table, opts) {
|
|
101
|
+
if (!table) {
|
|
102
|
+
throw new error_1.AppError("ARGS_INVALID", "table name is required", {
|
|
103
|
+
next_actions: [
|
|
104
|
+
"Usage: miaoda db audit enable <table> [--retention 7d|30d|180d|360d|forever]",
|
|
105
|
+
],
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
const retention = opts.retention ?? "7d";
|
|
109
|
+
if (!VALID_RETENTION.has(retention)) {
|
|
110
|
+
throw new error_1.AppError("INVALID_RETENTION", `Invalid retention '${retention}'`, {
|
|
111
|
+
next_actions: ["Allowed values: 7d, 30d, 180d, 360d, forever."],
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
const appId = (0, shared_1.resolveAppId)(opts);
|
|
115
|
+
let status;
|
|
116
|
+
try {
|
|
117
|
+
status = await api.db.setAuditConfig({
|
|
118
|
+
appId,
|
|
119
|
+
table,
|
|
120
|
+
enabled: true,
|
|
121
|
+
retention,
|
|
122
|
+
dbBranch: opts.env,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
catch (err) {
|
|
126
|
+
// PRD: 重复 enable 报错时附带 hint,引导用户去 status 看 retention 或换值更新
|
|
127
|
+
if (err instanceof error_1.AppError && err.code === "DB_API_k_dl_1300030") {
|
|
128
|
+
throw new error_1.AppError(err.code, err.message, {
|
|
129
|
+
next_actions: [
|
|
130
|
+
`Use \`miaoda db audit status ${table}\` to check current retention, or run this command with a different --retention to update.`,
|
|
131
|
+
],
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
throw err;
|
|
135
|
+
}
|
|
136
|
+
// PRD JSON:{"data": {"table": "...", "enabled": true, "retention": "..."}}
|
|
137
|
+
if ((0, output_1.isJsonMode)()) {
|
|
138
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)({
|
|
139
|
+
table: status.table,
|
|
140
|
+
enabled: status.enabled,
|
|
141
|
+
retention: status.retention ?? retention,
|
|
142
|
+
}));
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const tty = (0, render_1.isStdoutTty)();
|
|
146
|
+
const prefix = tty ? "✓" : "OK";
|
|
147
|
+
// c.highlight 内部按 TTY/NO_COLOR/FORCE_COLOR 自动决定是否染色,无需再判 tty
|
|
148
|
+
const tableLabel = colors_1.c.highlight(`'${status.table}'`);
|
|
149
|
+
(0, output_1.emit)(`${prefix} Audit enabled for table ${tableLabel} (retention: ${status.retention ?? retention})`);
|
|
150
|
+
}
|
|
151
|
+
async function handleDbAuditDisable(table, opts) {
|
|
152
|
+
if (!table) {
|
|
153
|
+
throw new error_1.AppError("ARGS_INVALID", "table name is required", {
|
|
154
|
+
next_actions: ["Usage: miaoda db audit disable <table>"],
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
const appId = (0, shared_1.resolveAppId)(opts);
|
|
158
|
+
let status;
|
|
159
|
+
try {
|
|
160
|
+
status = await api.db.setAuditConfig({
|
|
161
|
+
appId,
|
|
162
|
+
table,
|
|
163
|
+
enabled: false,
|
|
164
|
+
dbBranch: opts.env,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
catch (err) {
|
|
168
|
+
// PRD: 重复 disable / 未启用就 disable 报错时附带 hint,引导用户去看哪些表已开启
|
|
169
|
+
if (err instanceof error_1.AppError && err.code === "DB_API_k_dl_1300031") {
|
|
170
|
+
throw new error_1.AppError(err.code, err.message, {
|
|
171
|
+
next_actions: ["Use `miaoda db audit status` to see which tables have audit enabled."],
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
throw err;
|
|
175
|
+
}
|
|
176
|
+
if ((0, output_1.isJsonMode)()) {
|
|
177
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)({
|
|
178
|
+
table: status.table,
|
|
179
|
+
enabled: status.enabled,
|
|
180
|
+
}));
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
const tty = (0, render_1.isStdoutTty)();
|
|
184
|
+
const prefix = tty ? "✓" : "OK";
|
|
185
|
+
(0, output_1.emit)(`${prefix} Audit disabled for table ${colors_1.c.highlight(`'${status.table}'`)}`);
|
|
186
|
+
}
|
|
187
|
+
async function handleDbAuditList(tables, opts) {
|
|
188
|
+
if (tables.length === 0) {
|
|
189
|
+
throw new error_1.AppError("ARGS_INVALID", "at least one table is required", {
|
|
190
|
+
next_actions: [
|
|
191
|
+
"Usage: miaoda db audit list <table> [<table>...] [--since ...] [--until ...]",
|
|
192
|
+
],
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
const appId = (0, shared_1.resolveAppId)(opts);
|
|
196
|
+
const limit = opts.limit ?? 20;
|
|
197
|
+
// 时间字段归一化为 ISO 8601 UTC(服务端按 event_time 字段比较)。cursor 由
|
|
198
|
+
// 后端管理(本质也是 ISO 时间),CLI 不再混用 cursor/since。
|
|
199
|
+
const since = normalizeTime(opts.since, "--since");
|
|
200
|
+
const until = normalizeTime(opts.until, "--until");
|
|
201
|
+
const result = await api.db.listAuditLog({
|
|
202
|
+
appId,
|
|
203
|
+
tables,
|
|
204
|
+
since,
|
|
205
|
+
until,
|
|
206
|
+
limit,
|
|
207
|
+
cursor: opts.cursor,
|
|
208
|
+
dbBranch: opts.env,
|
|
209
|
+
});
|
|
210
|
+
const visible = result.items;
|
|
211
|
+
const skipped = result.skipped;
|
|
212
|
+
// PRD JSON:data 是数组,元素是事件对象(snake_case),分页信封 next_cursor / has_more
|
|
213
|
+
if ((0, output_1.isJsonMode)()) {
|
|
214
|
+
const items = visible.map((it) => ({
|
|
215
|
+
event_id: it.eventId,
|
|
216
|
+
event_time: it.eventTime,
|
|
217
|
+
target_table: it.targetTable,
|
|
218
|
+
type: it.type,
|
|
219
|
+
operator: it.operator,
|
|
220
|
+
summary: it.summary,
|
|
221
|
+
// before/after 服务端是 JSON 字符串,反序列化回结构化对象供下游消费
|
|
222
|
+
...(it.before !== undefined ? { before: safeParseJson(it.before) } : {}),
|
|
223
|
+
...(it.after !== undefined ? { after: safeParseJson(it.after) } : {}),
|
|
224
|
+
}));
|
|
225
|
+
(0, output_1.emitPaged)(items, result.nextCursor, result.hasMore);
|
|
226
|
+
if (skipped.length > 0) {
|
|
227
|
+
process.stderr.write(`(${String(skipped.length)} table(s) skipped: ${skipped.join(", ")})\n`);
|
|
228
|
+
}
|
|
229
|
+
if (visible.length === 0)
|
|
230
|
+
process.exitCode = 1;
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
if (visible.length === 0) {
|
|
234
|
+
(0, output_1.emit)("No audit log entries found.");
|
|
235
|
+
if (skipped.length > 0) {
|
|
236
|
+
process.stderr.write(`(${String(skipped.length)} table(s) skipped: ${skipped.join(", ")})\n`);
|
|
237
|
+
}
|
|
238
|
+
process.exitCode = 1;
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
const tty = (0, render_1.isStdoutTty)();
|
|
242
|
+
// PRD:多表渲染时第一列是 target_table,单表时去掉这一列
|
|
243
|
+
const isMultiTable = tables.length > 1;
|
|
244
|
+
const headers = isMultiTable
|
|
245
|
+
? ["target_table", "event_time", "type", "event_id", "operator", "summary"]
|
|
246
|
+
: ["event_time", "type", "event_id", "operator", "summary"];
|
|
247
|
+
const rows = visible.map((it) => {
|
|
248
|
+
const eventTime = (0, render_1.formatTime)(it.eventTime, tty);
|
|
249
|
+
// event_id 完整透传——PRD 截图里的 "..." 只是文档省略写法,不是 CLI 行为
|
|
250
|
+
const cells = [eventTime, it.type, it.eventId, it.operator || "—", it.summary];
|
|
251
|
+
return isMultiTable ? [it.targetTable, ...cells] : cells;
|
|
252
|
+
});
|
|
253
|
+
(0, output_1.emit)(tty ? (0, render_1.renderAlignedTable)(headers, rows) : (0, render_1.renderTsv)(headers, rows));
|
|
254
|
+
if (skipped.length > 0) {
|
|
255
|
+
process.stderr.write(`(${String(skipped.length)} table(s) skipped: ${skipped.join(", ")})\n`);
|
|
256
|
+
}
|
|
257
|
+
if (result.hasMore && result.nextCursor) {
|
|
258
|
+
process.stderr.write(`(more results; use --cursor '${result.nextCursor}')\n`);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
// ── helpers ──
|
|
262
|
+
function normalizeTime(input, flagName) {
|
|
263
|
+
if (input === undefined || input === "")
|
|
264
|
+
return undefined;
|
|
265
|
+
// ISO 8601 直接透传,避免 parseTimeFilterMs 在严格 ISO 上重做一次损精度
|
|
266
|
+
if (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:?\d{2})$/.test(input)) {
|
|
267
|
+
return new Date(input).toISOString();
|
|
268
|
+
}
|
|
269
|
+
const ms = (0, index_1.parseTimeFilterMs)(input, flagName);
|
|
270
|
+
return new Date(ms).toISOString();
|
|
271
|
+
}
|
|
272
|
+
// 服务端 before/after 是 JSON 字符串透传,CLI JSON 输出再反序列化回对象,
|
|
273
|
+
// 给下游 jq 等工具消费。失败时透传原始字符串避免数据丢失。
|
|
274
|
+
function safeParseJson(s) {
|
|
275
|
+
try {
|
|
276
|
+
return JSON.parse(s);
|
|
277
|
+
}
|
|
278
|
+
catch {
|
|
279
|
+
return s;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
// PRD 输出 enabled 列用 yes/no 而非 true/false
|
|
283
|
+
function boolToYesNo(b) {
|
|
284
|
+
return b ? "yes" : "no";
|
|
285
|
+
}
|