-
Notifications
You must be signed in to change notification settings - Fork 581
feat: add drive folder delete shortcut with async task polling #415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package drive | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/larksuite/cli/internal/output" | ||
| "github.com/larksuite/cli/internal/validate" | ||
| "github.com/larksuite/cli/shortcuts/common" | ||
| ) | ||
|
|
||
| var driveDeleteAllowedTypes = map[string]bool{ | ||
| "file": true, | ||
| "docx": true, | ||
| "bitable": true, | ||
| "doc": true, | ||
| "sheet": true, | ||
| "mindnote": true, | ||
| "folder": true, | ||
| "shortcut": true, | ||
| "slides": true, | ||
| } | ||
|
|
||
| // driveDeleteSpec contains the normalized input needed to issue a delete | ||
| // request against the Drive files endpoint. | ||
| type driveDeleteSpec struct { | ||
| FileToken string | ||
| FileType string | ||
| } | ||
|
|
||
| // DriveDelete deletes a Drive file or folder and handles the async task | ||
| // polling required by folder deletes. | ||
| var DriveDelete = common.Shortcut{ | ||
| Service: "drive", | ||
| Command: "+delete", | ||
| Description: "Delete a file or folder in Drive", | ||
| Risk: "high-risk-write", | ||
| Scopes: []string{"space:document:delete"}, | ||
| AuthTypes: []string{"user", "bot"}, | ||
| Flags: []common.Flag{ | ||
| {Name: "file-token", Desc: "file or folder token to delete", Required: true}, | ||
| {Name: "type", Desc: "file type (file, docx, bitable, doc, sheet, mindnote, folder, shortcut, slides)", Required: true}, | ||
| }, | ||
| Validate: func(ctx context.Context, runtime *common.RuntimeContext) error { | ||
| return validateDriveDeleteSpec(driveDeleteSpec{ | ||
| FileToken: runtime.Str("file-token"), | ||
| FileType: strings.ToLower(runtime.Str("type")), | ||
| }) | ||
| }, | ||
| DryRun: func(ctx context.Context, runtime *common.RuntimeContext) *common.DryRunAPI { | ||
| spec := driveDeleteSpec{ | ||
| FileToken: runtime.Str("file-token"), | ||
| FileType: strings.ToLower(runtime.Str("type")), | ||
| } | ||
|
|
||
| dry := common.NewDryRunAPI(). | ||
| Desc("Delete file or folder in Drive") | ||
|
|
||
| dry.DELETE("/open-apis/drive/v1/files/:file_token"). | ||
| Desc("[1] Delete file/folder"). | ||
| Set("file_token", spec.FileToken). | ||
| Params(map[string]interface{}{"type": spec.FileType}) | ||
|
|
||
| if spec.FileType == "folder" { | ||
| dry.GET("/open-apis/drive/v1/files/task_check"). | ||
| Desc("[2] Poll async task status (for folder delete)"). | ||
| Params(driveTaskCheckParams("<task_id>")) | ||
| } | ||
|
|
||
| return dry | ||
| }, | ||
| Execute: func(ctx context.Context, runtime *common.RuntimeContext) error { | ||
| spec := driveDeleteSpec{ | ||
| FileToken: runtime.Str("file-token"), | ||
| FileType: strings.ToLower(runtime.Str("type")), | ||
| } | ||
|
|
||
| fmt.Fprintf(runtime.IO().ErrOut, "Deleting %s %s...\n", spec.FileType, common.MaskToken(spec.FileToken)) | ||
|
|
||
| data, err := runtime.CallAPI( | ||
| "DELETE", | ||
| fmt.Sprintf("/open-apis/drive/v1/files/%s", validate.EncodePathSegment(spec.FileToken)), | ||
| map[string]interface{}{"type": spec.FileType}, | ||
| nil, | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if spec.FileType == "folder" { | ||
| taskID := common.GetString(data, "task_id") | ||
| if taskID == "" { | ||
| return output.Errorf(output.ExitAPI, "api_error", "delete folder returned no task_id") | ||
| } | ||
|
|
||
| fmt.Fprintf(runtime.IO().ErrOut, "Folder delete is async, polling task %s...\n", taskID) | ||
|
|
||
| status, ready, err := pollDriveTaskCheck(runtime, taskID) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| out := map[string]interface{}{ | ||
| "task_id": taskID, | ||
| "status": status.StatusLabel(), | ||
| "file_token": spec.FileToken, | ||
| "type": spec.FileType, | ||
| "ready": ready, | ||
| } | ||
| if ready { | ||
| out["deleted"] = true | ||
| } | ||
| if !ready { | ||
| nextCommand := driveTaskCheckResultCommand(taskID, string(runtime.As())) | ||
| fmt.Fprintf(runtime.IO().ErrOut, "Folder delete task is still in progress. Continue with: %s\n", nextCommand) | ||
| out["timed_out"] = true | ||
| out["next_command"] = nextCommand | ||
| } | ||
|
|
||
| runtime.Out(out, nil) | ||
| return nil | ||
| } | ||
|
|
||
| runtime.Out(map[string]interface{}{ | ||
| "deleted": true, | ||
| "file_token": spec.FileToken, | ||
| "type": spec.FileType, | ||
| }, nil) | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| func validateDriveDeleteSpec(spec driveDeleteSpec) error { | ||
| if err := validate.ResourceName(spec.FileToken, "--file-token"); err != nil { | ||
| return output.ErrValidation("%s", err) | ||
| } | ||
| if spec.FileType == "wiki" { | ||
| return output.ErrValidation("unsupported file type: wiki. This shortcut only supports Drive files and folders; wiki documents are not supported") | ||
| } | ||
| if !driveDeleteAllowedTypes[spec.FileType] { | ||
| return output.ErrValidation("unsupported file type: %s. Supported types: file, docx, bitable, doc, sheet, mindnote, folder, shortcut, slides", spec.FileType) | ||
| } | ||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,224 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package drive | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/larksuite/cli/internal/cmdutil" | ||
| "github.com/larksuite/cli/internal/httpmock" | ||
| "github.com/larksuite/cli/shortcuts/common" | ||
| ) | ||
|
|
||
| func TestValidateDriveDeleteSpecRejectsWiki(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| err := validateDriveDeleteSpec(driveDeleteSpec{ | ||
| FileToken: "wiki_token_test", | ||
| FileType: "wiki", | ||
| }) | ||
| if err == nil { | ||
| t.Fatal("expected wiki type error, got nil") | ||
| } | ||
| if !strings.Contains(err.Error(), "wiki documents are not supported") { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestDriveDeleteDryRunFolderIncludesTaskCheckParams(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| cmd := &cobra.Command{Use: "drive +delete"} | ||
| cmd.Flags().String("file-token", "", "") | ||
| cmd.Flags().String("type", "", "") | ||
| if err := cmd.Flags().Set("file-token", "fld_src"); err != nil { | ||
| t.Fatalf("set --file-token: %v", err) | ||
| } | ||
| if err := cmd.Flags().Set("type", "folder"); err != nil { | ||
| t.Fatalf("set --type: %v", err) | ||
| } | ||
|
|
||
| runtime := common.TestNewRuntimeContext(cmd, nil) | ||
| dry := DriveDelete.DryRun(context.Background(), runtime) | ||
| if dry == nil { | ||
| t.Fatal("DryRun returned nil") | ||
| } | ||
|
|
||
| data, err := json.Marshal(dry) | ||
| if err != nil { | ||
| t.Fatalf("marshal dry run: %v", err) | ||
| } | ||
|
|
||
| var got struct { | ||
| API []struct { | ||
| Method string `json:"method"` | ||
| Params map[string]interface{} `json:"params"` | ||
| } `json:"api"` | ||
| } | ||
| if err := json.Unmarshal(data, &got); err != nil { | ||
| t.Fatalf("unmarshal dry run json: %v", err) | ||
| } | ||
| if len(got.API) != 2 { | ||
| t.Fatalf("expected 2 API calls, got %d", len(got.API)) | ||
| } | ||
| if got.API[0].Method != "DELETE" { | ||
| t.Fatalf("first method = %q, want DELETE", got.API[0].Method) | ||
| } | ||
| if got.API[0].Params["type"] != "folder" { | ||
| t.Fatalf("delete params = %#v", got.API[0].Params) | ||
| } | ||
| if got.API[1].Params["task_id"] != "<task_id>" { | ||
| t.Fatalf("task check params = %#v", got.API[1].Params) | ||
| } | ||
| } | ||
|
|
||
| func TestDriveDeleteRequiresYes(t *testing.T) { | ||
| f, _, _, _ := cmdutil.TestFactory(t, driveTestConfig()) | ||
|
|
||
| err := mountAndRunDrive(t, DriveDelete, []string{ | ||
| "+delete", | ||
| "--file-token", "file_token_test", | ||
| "--type", "file", | ||
| "--as", "bot", | ||
| }, f, nil) | ||
| if err == nil { | ||
| t.Fatal("expected confirmation error, got nil") | ||
| } | ||
| if !strings.Contains(err.Error(), "requires confirmation") { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestDriveDeleteFileSuccess(t *testing.T) { | ||
| f, stdout, _, reg := cmdutil.TestFactory(t, driveTestConfig()) | ||
| reg.Register(&httpmock.Stub{ | ||
| Method: "DELETE", | ||
| URL: "/open-apis/drive/v1/files/file_token_test", | ||
| Body: map[string]interface{}{ | ||
| "code": 0, | ||
| "data": map[string]interface{}{}, | ||
| }, | ||
| }) | ||
|
|
||
| err := mountAndRunDrive(t, DriveDelete, []string{ | ||
| "+delete", | ||
| "--file-token", "file_token_test", | ||
| "--type", "file", | ||
| "--yes", | ||
| "--as", "bot", | ||
| }, f, stdout) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if !bytes.Contains(stdout.Bytes(), []byte(`"deleted": true`)) { | ||
| t.Fatalf("stdout missing deleted=true: %s", stdout.String()) | ||
| } | ||
| if !bytes.Contains(stdout.Bytes(), []byte(`"file_token": "file_token_test"`)) { | ||
| t.Fatalf("stdout missing file token: %s", stdout.String()) | ||
| } | ||
| } | ||
|
|
||
| func TestDriveDeleteFolderTaskCheckOutcomes(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| taskCheckBody map[string]interface{} | ||
| wantErrContains string | ||
| wantStdout []string | ||
| }{ | ||
| { | ||
| name: "success", | ||
| taskCheckBody: map[string]interface{}{ | ||
| "code": 0, | ||
| "data": map[string]interface{}{"status": "success"}, | ||
| }, | ||
| wantStdout: []string{ | ||
| `"task_id": "task_123"`, | ||
| `"deleted": true`, | ||
| `"ready": true`, | ||
| }, | ||
| }, | ||
| { | ||
| name: "timeout", | ||
| taskCheckBody: map[string]interface{}{ | ||
| "code": 0, | ||
| "data": map[string]interface{}{"status": "process"}, | ||
| }, | ||
| wantStdout: []string{ | ||
| `"ready": false`, | ||
| `"timed_out": true`, | ||
| `"next_command": "lark-cli drive +task_result --scenario task_check --task-id task_123 --as bot"`, | ||
| }, | ||
| }, | ||
| { | ||
| name: "failed", | ||
| taskCheckBody: map[string]interface{}{ | ||
| "code": 0, | ||
| "data": map[string]interface{}{"status": "fail"}, | ||
| }, | ||
| wantErrContains: "folder task failed", | ||
| }, | ||
| { | ||
| name: "task_check error", | ||
| taskCheckBody: map[string]interface{}{ | ||
| "code": 1061001, | ||
| "msg": "internal error", | ||
| }, | ||
| wantErrContains: "internal error", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| f, stdout, _, reg := cmdutil.TestFactory(t, driveTestConfig()) | ||
| reg.Register(&httpmock.Stub{ | ||
| Method: "DELETE", | ||
| URL: "/open-apis/drive/v1/files/fld_src", | ||
| Body: map[string]interface{}{ | ||
| "code": 0, | ||
| "data": map[string]interface{}{"task_id": "task_123"}, | ||
| }, | ||
| }) | ||
| reg.Register(&httpmock.Stub{ | ||
| Method: "GET", | ||
| URL: "/open-apis/drive/v1/files/task_check", | ||
| Body: tt.taskCheckBody, | ||
| }) | ||
|
|
||
| withSingleDriveTaskCheckPoll(t) | ||
|
|
||
| err := mountAndRunDrive(t, DriveDelete, []string{ | ||
| "+delete", | ||
| "--file-token", "fld_src", | ||
| "--type", "folder", | ||
| "--yes", | ||
| "--as", "bot", | ||
| }, f, stdout) | ||
|
|
||
| if tt.wantErrContains != "" { | ||
| if err == nil { | ||
| t.Fatal("expected delete failure, got nil") | ||
| } | ||
| if !strings.Contains(err.Error(), tt.wantErrContains) { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| for _, needle := range tt.wantStdout { | ||
| if !bytes.Contains(stdout.Bytes(), []byte(needle)) { | ||
| t.Fatalf("stdout missing %q: %s", needle, stdout.String()) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.