Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion shortcuts/base/base_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
43 changes: 42 additions & 1 deletion shortcuts/base/base_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Loading