mirror of
https://github.com/larksuite/cli.git
synced 2026-08-03 08:32:46 +08:00
* fix(apps): make db --environment optional, auto-select branch server-side All db shortcuts defaulted --environment to "dev", which forced single-env apps (whose DB lives on the online branch, with no dev branch) to fail with "Invalid DB Branch: dev" unless the user explicitly passed --environment online. Change the default to empty: when --environment is omitted the CLI sends no env, letting the server pick the branch by the app's multi-env state (multi-env → dev, single-env → online), matching miaoda-cli's behavior of not carrying dbBranch when unset. Explicit --environment dev|online is unchanged; explicit dev on a single-env app still errors as expected. - 10 db shortcuts: dbEnvFlags default "dev" → "" (+db-execute, +db-table-list, +db-table-get, +db-quota-get, +db-data-export, +db-data-import, +db-changelog-list, +db-audit-list/-set/-status) - dry-run e2e assertions updated: default env is now unset, not "dev" - skill docs (lark-apps-db, lark-apps-db-execute) describe the auto-select * fix(apps): omit empty --environment param; refine dry-run tests and skill doc Address PR #1735 review: - omit-empty: when --environment is unset, drop the env query key entirely instead of sending env="" — matches the family's omit-empty convention (cf. page_token) and miaoda-cli's "no dbBranch when unset". Add dbEnvParams helper; apply across all db shortcuts (execute, table-list/-get, quota-get, changelog-list, audit-list/-set/-status, data-export/-import) plus the export/import query params, queryExportTotal and audit-list table/status probes. - e2e dry-run assertions pin env is omitted via .Exists() (was Equal ""). - skill doc (lark-apps-db): rewrite the --environment guidance from an agent's decision POV — read vs write, single-env writes hit online prod, explicit dev on single-env as a probe; drop redundant/changelog phrasing. * fix(apps): db recovery --environment support + diff/migrate display fixes - +db-recovery-diff/-apply: add --environment (env → query param on submit and both status polls), aligned with the recovery env IDL - recovery diff: parse string row counts (inserted/deleted arrive as strings) so they render as "-N rows" instead of "no changes"; drop the redundant per-table data-row line when a schema action (drop/restore/alter) exists for the same table; count tables_affected by distinct tables - +db-env-migrate: run a dry_run preview before apply to backfill the change count when the server reports changes_applied=0 on a cold apply (matches miaoda-cli's diff-then-apply) - lark-apps-db.md: drop the redundant recovery clause (recovery follows the standard --environment rule) * test(apps): cover no-env dry-run defaults + numericAsFloat string path Address CodeRabbit review threads on PR #1735: - numericAsFloat: add numeric-string cases ("13.5", " 13.5 ", int, empty) - db-data-import: assert dry-run omits env when --environment unset (table still defaults to file basename) - db-quota-get: assert dry-run omits env when --environment unset
145 lines
5.4 KiB
Go
145 lines
5.4 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
package apps
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"io"
|
||
"strings"
|
||
|
||
"github.com/larksuite/cli/shortcuts/common"
|
||
)
|
||
|
||
// 审计保留期合法取值。
|
||
var auditRetentions = []string{"7d", "30d", "180d", "360d", "forever"}
|
||
|
||
const dbAuditSetHint = "verify --app-id and --table; check current config with `lark-cli apps +db-audit-status --app-id <app_id>`"
|
||
|
||
// AppsDBAuditEnable 为某张表开启行级审计(变更追溯)。
|
||
//
|
||
// POST /apps/{app_id}/db/audit_set,body {table, enabled:true, retention}。--retention 默认 7d。
|
||
var AppsDBAuditEnable = common.Shortcut{
|
||
Service: appsService,
|
||
Command: "+db-audit-enable",
|
||
Description: "Enable row-change audit logging for a table",
|
||
Risk: "write",
|
||
Tips: []string{
|
||
"Example: lark-cli apps +db-audit-enable --app-id <app_id> --table orders --retention 30d",
|
||
},
|
||
Scopes: []string{"spark:app:write"},
|
||
AuthTypes: []string{"user"},
|
||
HasFormat: true,
|
||
Flags: append([]common.Flag{
|
||
{Name: "app-id", Desc: "Miaoda app id", Required: true},
|
||
{Name: "table", Desc: "table to enable audit for", Required: true},
|
||
{Name: "retention", Default: "7d", Enum: auditRetentions, Desc: "how long to keep audit logs"},
|
||
}, dbEnvFlags("", []string{"dev", "online"}, "target db environment; leave unset to auto-select (multi-env app uses dev, single-env uses online), or pass dev/online")...),
|
||
Validate: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||
if _, err := requireAppID(rctx.Str("app-id")); err != nil {
|
||
return err
|
||
}
|
||
return rejectLegacyEnvFlag(rctx)
|
||
},
|
||
DryRun: func(ctx context.Context, rctx *common.RuntimeContext) *common.DryRunAPI {
|
||
appID, _ := requireAppID(rctx.Str("app-id"))
|
||
return common.NewDryRunAPI().
|
||
POST(appAuditSetPath(appID)).
|
||
Desc("Enable table audit").
|
||
Params(dbEnvParams(rctx, map[string]interface{}{})).
|
||
Body(map[string]interface{}{"table": strings.TrimSpace(rctx.Str("table")), "enabled": true, "retention": rctx.Str("retention")})
|
||
},
|
||
Execute: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||
appID, err := requireAppID(rctx.Str("app-id"))
|
||
if err != nil {
|
||
return err
|
||
}
|
||
table := strings.TrimSpace(rctx.Str("table"))
|
||
retention := rctx.Str("retention")
|
||
stop := rctx.StartSpinner("Enabling audit logging for " + table)
|
||
defer stop()
|
||
data, err := rctx.CallAPITyped("POST", appAuditSetPath(appID),
|
||
dbEnvParams(rctx, map[string]interface{}{}),
|
||
map[string]interface{}{"table": table, "enabled": true, "retention": retention})
|
||
stop()
|
||
if err != nil {
|
||
return withAppsHint(err, dbAuditSetHint)
|
||
}
|
||
st := auditSetStatus(data, table)
|
||
ret := common.GetString(st, "retention")
|
||
if ret == "" {
|
||
ret = retention
|
||
}
|
||
out := map[string]interface{}{"table": common.GetString(st, "table"), "enabled": true, "retention": ret}
|
||
rctx.OutFormat(out, nil, func(w io.Writer) {
|
||
fmt.Fprintf(w, "✓ Audit enabled for table '%s' (retention: %s)\n", common.GetString(out, "table"), ret)
|
||
})
|
||
return nil
|
||
},
|
||
}
|
||
|
||
// AppsDBAuditDisable 关闭某张表的行级审计。
|
||
//
|
||
// POST /apps/{app_id}/db/audit_set,body {table, enabled:false}。
|
||
var AppsDBAuditDisable = common.Shortcut{
|
||
Service: appsService,
|
||
Command: "+db-audit-disable",
|
||
Description: "Disable row-change audit logging for a table",
|
||
Risk: "write",
|
||
Tips: []string{
|
||
"Example: lark-cli apps +db-audit-disable --app-id <app_id> --table orders",
|
||
},
|
||
Scopes: []string{"spark:app:write"},
|
||
AuthTypes: []string{"user"},
|
||
HasFormat: true,
|
||
Flags: append([]common.Flag{
|
||
{Name: "app-id", Desc: "Miaoda app id", Required: true},
|
||
{Name: "table", Desc: "table to disable audit for", Required: true},
|
||
}, dbEnvFlags("", []string{"dev", "online"}, "target db environment; leave unset to auto-select (multi-env app uses dev, single-env uses online), or pass dev/online")...),
|
||
Validate: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||
if _, err := requireAppID(rctx.Str("app-id")); err != nil {
|
||
return err
|
||
}
|
||
return rejectLegacyEnvFlag(rctx)
|
||
},
|
||
DryRun: func(ctx context.Context, rctx *common.RuntimeContext) *common.DryRunAPI {
|
||
appID, _ := requireAppID(rctx.Str("app-id"))
|
||
return common.NewDryRunAPI().
|
||
POST(appAuditSetPath(appID)).
|
||
Desc("Disable table audit").
|
||
Params(dbEnvParams(rctx, map[string]interface{}{})).
|
||
Body(map[string]interface{}{"table": strings.TrimSpace(rctx.Str("table")), "enabled": false})
|
||
},
|
||
Execute: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||
appID, err := requireAppID(rctx.Str("app-id"))
|
||
if err != nil {
|
||
return err
|
||
}
|
||
table := strings.TrimSpace(rctx.Str("table"))
|
||
data, err := rctx.CallAPITyped("POST", appAuditSetPath(appID),
|
||
dbEnvParams(rctx, map[string]interface{}{}),
|
||
map[string]interface{}{"table": table, "enabled": false})
|
||
if err != nil {
|
||
return withAppsHint(err, dbAuditSetHint)
|
||
}
|
||
st := auditSetStatus(data, table)
|
||
out := map[string]interface{}{"table": common.GetString(st, "table"), "enabled": false}
|
||
rctx.OutFormat(out, nil, func(w io.Writer) {
|
||
fmt.Fprintf(w, "✓ Audit disabled for table '%s'\n", common.GetString(out, "table"))
|
||
})
|
||
return nil
|
||
},
|
||
}
|
||
|
||
// auditSetStatus 取响应里的 status 对象(缺失时用入参 table 兜底)。
|
||
func auditSetStatus(data map[string]interface{}, table string) map[string]interface{} {
|
||
if st, ok := data["status"].(map[string]interface{}); ok {
|
||
if common.GetString(st, "table") == "" {
|
||
st["table"] = table
|
||
}
|
||
return st
|
||
}
|
||
return map[string]interface{}{"table": table}
|
||
}
|