diff --git a/shortcuts/base/base_create.go b/shortcuts/base/base_create.go index b5f69a1e..41fe8d9b 100644 --- a/shortcuts/base/base_create.go +++ b/shortcuts/base/base_create.go @@ -14,12 +14,13 @@ var BaseBaseCreate = common.Shortcut{ Command: "+base-create", Description: "Create a new base resource", Risk: "write", - Scopes: []string{"base:app:create"}, + Scopes: []string{"base:app:create", "bitable:app.table:read", "bitable:app.table.record:delete"}, AuthTypes: authTypes(), Flags: []common.Flag{ {Name: "name", Desc: "base name", Required: true}, {Name: "folder-token", Desc: "folder token for destination"}, {Name: "time-zone", Desc: "time zone, e.g. Asia/Shanghai"}, + {Name: "keep-empty-rows", Type: "bool", Default: "false", Desc: "retain the default 5 empty rows in the newly created base"}, }, DryRun: dryRunBaseCreate, Execute: func(ctx context.Context, runtime *common.RuntimeContext) error { diff --git a/shortcuts/base/base_ops.go b/shortcuts/base/base_ops.go index 8a70cba7..f2759a0b 100644 --- a/shortcuts/base/base_ops.go +++ b/shortcuts/base/base_ops.go @@ -92,6 +92,47 @@ func executeBaseCreate(runtime *common.RuntimeContext) error { if err != nil { return err } - runtime.Out(map[string]interface{}{"base": data, "created": true}, nil) + + deletedTotal := 0 + if !runtime.Bool("keep-empty-rows") { + var baseToken string + if m, ok := data["base"].(map[string]interface{}); ok { + baseToken, _ = m["base_token"].(string) + } + if baseToken == "" { + baseToken, _ = data["base_token"].(string) + } + if baseToken != "" { + tables, _, err := listAllTables(runtime, baseToken, 0, 10) + if err == nil && len(tables) > 0 { + defaultTableID := tableID(tables[0]) + recordsData, err := baseV3Call(runtime, "GET", baseV3Path("bases", baseToken, "tables", defaultTableID, "records"), map[string]interface{}{"limit": 10}, nil) + if err == nil { + if rawItems, ok := recordsData["items"].([]interface{}); ok { + for _, item := range rawItems { + if recMap, ok := item.(map[string]interface{}); ok { + // Emptiness guard: only delete if 'fields' is strictly empty (no values filled yet) + fieldsMap, _ := recMap["fields"].(map[string]interface{}) + if len(fieldsMap) == 0 { + if recID, ok := recMap["record_id"].(string); ok { + _, delErr := baseV3Call(runtime, "DELETE", baseV3Path("bases", baseToken, "tables", defaultTableID, "records", recID), nil, nil) + if delErr == nil { + deletedTotal++ + } + } + } + } + } + } + } + } + } + } + + result := map[string]interface{}{"base": data, "created": true} + if deletedTotal > 0 { + result["_pruned_records"] = deletedTotal + } + runtime.Out(result, nil) return nil }