|
| 1 | +// Copyright (c) 2026 Lark Technologies Pte. Ltd. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package base |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/larksuite/cli/shortcuts/common" |
| 11 | +) |
| 12 | + |
| 13 | +var baseBlockTypeEnums = []string{"folder", "table", "docx", "dashboard", "workflow"} |
| 14 | + |
| 15 | +func baseBlockIDFlag(required bool) common.Flag { |
| 16 | + return common.Flag{Name: "block-id", Desc: "block id", Required: required} |
| 17 | +} |
| 18 | + |
| 19 | +func dryRunBaseBlockList(_ context.Context, runtime *common.RuntimeContext) *common.DryRunAPI { |
| 20 | + return common.NewDryRunAPI(). |
| 21 | + POST("/open-apis/base/v3/bases/:base_token/blocks/list"). |
| 22 | + Body(buildBaseBlockListBody(runtime)). |
| 23 | + Set("base_token", runtime.Str("base-token")) |
| 24 | +} |
| 25 | + |
| 26 | +func dryRunBaseBlockCreate(_ context.Context, runtime *common.RuntimeContext) *common.DryRunAPI { |
| 27 | + return common.NewDryRunAPI(). |
| 28 | + POST("/open-apis/base/v3/bases/:base_token/blocks"). |
| 29 | + Body(buildBaseBlockCreateBody(runtime)). |
| 30 | + Set("base_token", runtime.Str("base-token")) |
| 31 | +} |
| 32 | + |
| 33 | +func dryRunBaseBlockMove(_ context.Context, runtime *common.RuntimeContext) *common.DryRunAPI { |
| 34 | + return common.NewDryRunAPI(). |
| 35 | + POST("/open-apis/base/v3/bases/:base_token/blocks/:block_id/move"). |
| 36 | + Body(buildBaseBlockMoveBody(runtime)). |
| 37 | + Set("base_token", runtime.Str("base-token")). |
| 38 | + Set("block_id", runtime.Str("block-id")) |
| 39 | +} |
| 40 | + |
| 41 | +func dryRunBaseBlockRename(_ context.Context, runtime *common.RuntimeContext) *common.DryRunAPI { |
| 42 | + return common.NewDryRunAPI(). |
| 43 | + POST("/open-apis/base/v3/bases/:base_token/blocks/:block_id/rename"). |
| 44 | + Body(map[string]interface{}{"name": strings.TrimSpace(runtime.Str("name"))}). |
| 45 | + Set("base_token", runtime.Str("base-token")). |
| 46 | + Set("block_id", runtime.Str("block-id")) |
| 47 | +} |
| 48 | + |
| 49 | +func dryRunBaseBlockDelete(_ context.Context, runtime *common.RuntimeContext) *common.DryRunAPI { |
| 50 | + return common.NewDryRunAPI(). |
| 51 | + DELETE("/open-apis/base/v3/bases/:base_token/blocks/:block_id"). |
| 52 | + Set("base_token", runtime.Str("base-token")). |
| 53 | + Set("block_id", runtime.Str("block-id")) |
| 54 | +} |
| 55 | + |
| 56 | +func validateBaseBlockCreate(runtime *common.RuntimeContext) error { |
| 57 | + if strings.TrimSpace(runtime.Str("name")) == "" { |
| 58 | + return common.FlagErrorf("--name must not be blank") |
| 59 | + } |
| 60 | + if strings.TrimSpace(runtime.Str("type")) == "" { |
| 61 | + return common.FlagErrorf("--type must not be blank") |
| 62 | + } |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +func validateBaseBlockMove(runtime *common.RuntimeContext) error { |
| 67 | + if strings.TrimSpace(runtime.Str("before-id")) != "" && strings.TrimSpace(runtime.Str("after-id")) != "" { |
| 68 | + return common.FlagErrorf("--before-id and --after-id are mutually exclusive") |
| 69 | + } |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +func validateBaseBlockRename(runtime *common.RuntimeContext) error { |
| 74 | + if strings.TrimSpace(runtime.Str("name")) == "" { |
| 75 | + return common.FlagErrorf("--name must not be blank") |
| 76 | + } |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +func executeBaseBlockList(runtime *common.RuntimeContext) error { |
| 81 | + data, err := baseV3Call(runtime, "POST", baseV3Path("bases", runtime.Str("base-token"), "blocks", "list"), nil, buildBaseBlockListBody(runtime)) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + filterBaseBlockListData(data, strings.TrimSpace(runtime.Str("type"))) |
| 86 | + runtime.Out(data, nil) |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +func executeBaseBlockCreate(runtime *common.RuntimeContext) error { |
| 91 | + data, err := baseV3Call(runtime, "POST", baseV3Path("bases", runtime.Str("base-token"), "blocks"), nil, buildBaseBlockCreateBody(runtime)) |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + runtime.Out(map[string]interface{}{"block": data, "created": true}, nil) |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +func executeBaseBlockMove(runtime *common.RuntimeContext) error { |
| 100 | + data, err := baseV3Call(runtime, "POST", baseV3Path("bases", runtime.Str("base-token"), "blocks", runtime.Str("block-id"), "move"), nil, buildBaseBlockMoveBody(runtime)) |
| 101 | + if err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + runtime.Out(map[string]interface{}{"block": data, "moved": true}, nil) |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +func executeBaseBlockRename(runtime *common.RuntimeContext) error { |
| 109 | + data, err := baseV3Call(runtime, "POST", baseV3Path("bases", runtime.Str("base-token"), "blocks", runtime.Str("block-id"), "rename"), nil, map[string]interface{}{ |
| 110 | + "name": strings.TrimSpace(runtime.Str("name")), |
| 111 | + }) |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + runtime.Out(map[string]interface{}{"block": data, "renamed": true}, nil) |
| 116 | + return nil |
| 117 | +} |
| 118 | + |
| 119 | +func executeBaseBlockDelete(runtime *common.RuntimeContext) error { |
| 120 | + data, err := baseV3Call(runtime, "DELETE", baseV3Path("bases", runtime.Str("base-token"), "blocks", runtime.Str("block-id")), nil, nil) |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + runtime.Out(map[string]interface{}{"block": data, "deleted": true}, nil) |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +func buildBaseBlockListBody(runtime *common.RuntimeContext) map[string]interface{} { |
| 129 | + body := map[string]interface{}{} |
| 130 | + if parentID := strings.TrimSpace(runtime.Str("parent-id")); parentID != "" { |
| 131 | + body["parent_id"] = parentID |
| 132 | + } |
| 133 | + return body |
| 134 | +} |
| 135 | + |
| 136 | +func filterBaseBlockListData(data map[string]interface{}, blockType string) { |
| 137 | + if blockType == "" { |
| 138 | + return |
| 139 | + } |
| 140 | + blocks, ok := data["blocks"].([]interface{}) |
| 141 | + if !ok { |
| 142 | + return |
| 143 | + } |
| 144 | + filtered := make([]interface{}, 0, len(blocks)) |
| 145 | + for _, block := range blocks { |
| 146 | + blockMap, ok := block.(map[string]interface{}) |
| 147 | + if !ok || blockMap["type"] != blockType { |
| 148 | + continue |
| 149 | + } |
| 150 | + filtered = append(filtered, block) |
| 151 | + } |
| 152 | + data["blocks"] = filtered |
| 153 | + data["total"] = len(filtered) |
| 154 | +} |
| 155 | + |
| 156 | +func buildBaseBlockCreateBody(runtime *common.RuntimeContext) map[string]interface{} { |
| 157 | + body := map[string]interface{}{ |
| 158 | + "type": strings.TrimSpace(runtime.Str("type")), |
| 159 | + "name": strings.TrimSpace(runtime.Str("name")), |
| 160 | + } |
| 161 | + if parentID := strings.TrimSpace(runtime.Str("parent-id")); parentID != "" { |
| 162 | + body["parent_id"] = parentID |
| 163 | + } |
| 164 | + return body |
| 165 | +} |
| 166 | + |
| 167 | +func buildBaseBlockMoveBody(runtime *common.RuntimeContext) map[string]interface{} { |
| 168 | + body := map[string]interface{}{"parent_id": nil} |
| 169 | + if parentID := strings.TrimSpace(runtime.Str("parent-id")); parentID != "" { |
| 170 | + body["parent_id"] = parentID |
| 171 | + } |
| 172 | + if beforeID := strings.TrimSpace(runtime.Str("before-id")); beforeID != "" { |
| 173 | + body["before_id"] = beforeID |
| 174 | + } |
| 175 | + if afterID := strings.TrimSpace(runtime.Str("after-id")); afterID != "" { |
| 176 | + body["after_id"] = afterID |
| 177 | + } |
| 178 | + return body |
| 179 | +} |
0 commit comments