gws-cli 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
gws/commands/drive.py ADDED
@@ -0,0 +1,580 @@
1
+ """Drive CLI commands."""
2
+
3
+ import typer
4
+ from typing import Annotated, Optional
5
+
6
+ from gws.commands._account import account_callback
7
+ from gws.services.drive import DriveService
8
+
9
+ app = typer.Typer(
10
+ name="drive",
11
+ help="Google Drive file operations.",
12
+ no_args_is_help=True,
13
+ callback=account_callback,
14
+ )
15
+
16
+
17
+ @app.command("list")
18
+ def list_files(
19
+ folder_id: Annotated[
20
+ Optional[str],
21
+ typer.Option("--folder", "-f", help="Folder ID to list contents of."),
22
+ ] = None,
23
+ max_results: Annotated[
24
+ int,
25
+ typer.Option("--max", "-m", help="Maximum number of files to return."),
26
+ ] = 100,
27
+ page_token: Annotated[
28
+ Optional[str],
29
+ typer.Option("--page-token", help="Token for pagination."),
30
+ ] = None,
31
+ ) -> None:
32
+ """List files in Drive or a specific folder."""
33
+ service = DriveService()
34
+ service.list_files(folder_id=folder_id, max_results=max_results, page_token=page_token)
35
+
36
+
37
+ @app.command("search")
38
+ def search_files(
39
+ query: Annotated[str, typer.Argument(help="Search query (Drive query syntax).")],
40
+ max_results: Annotated[
41
+ int,
42
+ typer.Option("--max", "-m", help="Maximum number of files to return."),
43
+ ] = 100,
44
+ page_token: Annotated[
45
+ Optional[str],
46
+ typer.Option("--page-token", help="Token for pagination."),
47
+ ] = None,
48
+ ) -> None:
49
+ """Search files with a query.
50
+
51
+ Query syntax examples:
52
+ - name contains 'report'
53
+ - mimeType = 'application/pdf'
54
+ - modifiedTime > '2024-01-01'
55
+ - 'folder_id' in parents
56
+ """
57
+ service = DriveService()
58
+ service.search(query=query, max_results=max_results, page_token=page_token)
59
+
60
+
61
+ @app.command("get")
62
+ def get_metadata(
63
+ file_id: Annotated[str, typer.Argument(help="File ID to get metadata for.")],
64
+ ) -> None:
65
+ """Get detailed file metadata."""
66
+ service = DriveService()
67
+ service.get_metadata(file_id=file_id)
68
+
69
+
70
+ @app.command("upload")
71
+ def upload_file(
72
+ file_path: Annotated[str, typer.Argument(help="Local file path to upload.")],
73
+ folder_id: Annotated[
74
+ Optional[str],
75
+ typer.Option("--folder", "-f", help="Destination folder ID."),
76
+ ] = None,
77
+ name: Annotated[
78
+ Optional[str],
79
+ typer.Option("--name", "-n", help="Name for the uploaded file."),
80
+ ] = None,
81
+ mime_type: Annotated[
82
+ Optional[str],
83
+ typer.Option("--mime-type", help="MIME type (auto-detected if not specified)."),
84
+ ] = None,
85
+ ) -> None:
86
+ """Upload a file to Google Drive."""
87
+ service = DriveService()
88
+ service.upload(file_path=file_path, folder_id=folder_id, name=name, mime_type=mime_type)
89
+
90
+
91
+ @app.command("download")
92
+ def download_file(
93
+ file_id: Annotated[str, typer.Argument(help="File ID to download.")],
94
+ output_path: Annotated[str, typer.Argument(help="Local path to save the file.")],
95
+ ) -> None:
96
+ """Download a file from Google Drive."""
97
+ service = DriveService()
98
+ service.download(file_id=file_id, output_path=output_path)
99
+
100
+
101
+ @app.command("export")
102
+ def export_file(
103
+ file_id: Annotated[str, typer.Argument(help="File ID to export.")],
104
+ output_path: Annotated[str, typer.Argument(help="Local path to save the file.")],
105
+ mime_type: Annotated[
106
+ str,
107
+ typer.Option("--format", "-f", help="Export MIME type (e.g., application/pdf)."),
108
+ ] = "application/pdf",
109
+ ) -> None:
110
+ """Export a Google native file (Docs, Sheets, Slides) to another format."""
111
+ service = DriveService()
112
+ service.export(file_id=file_id, output_path=output_path, export_mime_type=mime_type)
113
+
114
+
115
+ @app.command("create-folder")
116
+ def create_folder(
117
+ name: Annotated[str, typer.Argument(help="Folder name.")],
118
+ parent_id: Annotated[
119
+ Optional[str],
120
+ typer.Option("--parent", "-p", help="Parent folder ID."),
121
+ ] = None,
122
+ ) -> None:
123
+ """Create a new folder."""
124
+ service = DriveService()
125
+ service.create_folder(name=name, parent_id=parent_id)
126
+
127
+
128
+ @app.command("move")
129
+ def move_file(
130
+ file_id: Annotated[str, typer.Argument(help="File ID to move.")],
131
+ folder_id: Annotated[str, typer.Argument(help="Destination folder ID.")],
132
+ ) -> None:
133
+ """Move a file to a different folder."""
134
+ service = DriveService()
135
+ service.move(file_id=file_id, folder_id=folder_id)
136
+
137
+
138
+ @app.command("copy")
139
+ def copy_file(
140
+ file_id: Annotated[str, typer.Argument(help="File ID to copy.")],
141
+ name: Annotated[
142
+ Optional[str],
143
+ typer.Option("--name", "-n", help="Name for the copy."),
144
+ ] = None,
145
+ folder_id: Annotated[
146
+ Optional[str],
147
+ typer.Option("--folder", "-f", help="Destination folder ID."),
148
+ ] = None,
149
+ ) -> None:
150
+ """Copy a file."""
151
+ service = DriveService()
152
+ service.copy(file_id=file_id, name=name, folder_id=folder_id)
153
+
154
+
155
+ @app.command("share")
156
+ def share_file(
157
+ file_id: Annotated[str, typer.Argument(help="File ID to share.")],
158
+ email: Annotated[
159
+ Optional[str],
160
+ typer.Option("--email", "-e", help="Email address to share with."),
161
+ ] = None,
162
+ role: Annotated[
163
+ str,
164
+ typer.Option("--role", "-r", help="Permission role: reader, writer, commenter."),
165
+ ] = "reader",
166
+ share_type: Annotated[
167
+ Optional[str],
168
+ typer.Option("--type", "-t", help="Share type: user, group, domain, anyone."),
169
+ ] = None,
170
+ domain: Annotated[
171
+ Optional[str],
172
+ typer.Option("--domain", "-d", help="Domain for domain-wide sharing (e.g., company.com)."),
173
+ ] = None,
174
+ ) -> None:
175
+ """Share a file with a user, domain, or make public.
176
+
177
+ Examples:
178
+ Share with user: --email user@example.com
179
+ Share with domain: --domain company.com (anyone at company.com)
180
+ Make public: (no options, creates 'anyone' link)
181
+ """
182
+ service = DriveService()
183
+ service.share(file_id=file_id, email=email, role=role, share_type=share_type, domain=domain)
184
+
185
+
186
+ @app.command("update")
187
+ def update_file(
188
+ file_id: Annotated[str, typer.Argument(help="File ID to update.")],
189
+ file_path: Annotated[str, typer.Argument(help="Local file with new content.")],
190
+ name: Annotated[
191
+ Optional[str],
192
+ typer.Option("--name", "-n", help="New name for the file."),
193
+ ] = None,
194
+ ) -> None:
195
+ """Update a file's content."""
196
+ service = DriveService()
197
+ service.update(file_id=file_id, file_path=file_path, name=name)
198
+
199
+
200
+ @app.command("delete")
201
+ def delete_file(
202
+ file_id: Annotated[str, typer.Argument(help="File ID to delete.")],
203
+ permanent: Annotated[
204
+ bool,
205
+ typer.Option("--permanent", "-p", help="Permanently delete (skip trash)."),
206
+ ] = False,
207
+ ) -> None:
208
+ """Delete a file (moves to trash by default)."""
209
+ service = DriveService()
210
+ service.delete(file_id=file_id, permanent=permanent)
211
+
212
+
213
+ # ===== Comments =====
214
+
215
+
216
+ @app.command("list-comments")
217
+ def list_comments(
218
+ file_id: Annotated[str, typer.Argument(help="File ID to list comments for.")],
219
+ include_deleted: Annotated[
220
+ bool,
221
+ typer.Option("--include-deleted", help="Include deleted comments."),
222
+ ] = False,
223
+ max_results: Annotated[
224
+ int,
225
+ typer.Option("--max", "-m", help="Maximum number of comments to return."),
226
+ ] = 20,
227
+ ) -> None:
228
+ """List comments on a file."""
229
+ service = DriveService()
230
+ service.list_comments(
231
+ file_id=file_id, include_deleted=include_deleted, max_results=max_results
232
+ )
233
+
234
+
235
+ @app.command("add-comment")
236
+ def add_comment(
237
+ file_id: Annotated[str, typer.Argument(help="File ID to comment on.")],
238
+ content: Annotated[str, typer.Argument(help="Comment text.")],
239
+ ) -> None:
240
+ """Add a comment to a file."""
241
+ service = DriveService()
242
+ service.add_comment(file_id=file_id, content=content)
243
+
244
+
245
+ @app.command("resolve-comment")
246
+ def resolve_comment(
247
+ file_id: Annotated[str, typer.Argument(help="File ID.")],
248
+ comment_id: Annotated[str, typer.Argument(help="Comment ID to resolve.")],
249
+ ) -> None:
250
+ """Mark a comment as resolved."""
251
+ service = DriveService()
252
+ service.resolve_comment(file_id=file_id, comment_id=comment_id)
253
+
254
+
255
+ @app.command("delete-comment")
256
+ def delete_comment(
257
+ file_id: Annotated[str, typer.Argument(help="File ID.")],
258
+ comment_id: Annotated[str, typer.Argument(help="Comment ID to delete.")],
259
+ ) -> None:
260
+ """Delete a comment."""
261
+ service = DriveService()
262
+ service.delete_comment(file_id=file_id, comment_id=comment_id)
263
+
264
+
265
+ @app.command("reply-to-comment")
266
+ def reply_to_comment(
267
+ file_id: Annotated[str, typer.Argument(help="File ID.")],
268
+ comment_id: Annotated[str, typer.Argument(help="Comment ID to reply to.")],
269
+ content: Annotated[str, typer.Argument(help="Reply text.")],
270
+ ) -> None:
271
+ """Reply to a comment."""
272
+ service = DriveService()
273
+ service.reply_to_comment(file_id=file_id, comment_id=comment_id, content=content)
274
+
275
+
276
+ # ===== Revisions =====
277
+
278
+
279
+ @app.command("list-revisions")
280
+ def list_revisions(
281
+ file_id: Annotated[str, typer.Argument(help="File ID to list revisions for.")],
282
+ max_results: Annotated[
283
+ int,
284
+ typer.Option("--max", "-m", help="Maximum number of revisions to return."),
285
+ ] = 20,
286
+ ) -> None:
287
+ """List revisions of a file."""
288
+ service = DriveService()
289
+ service.list_revisions(file_id=file_id, max_results=max_results)
290
+
291
+
292
+ @app.command("get-revision")
293
+ def get_revision(
294
+ file_id: Annotated[str, typer.Argument(help="File ID.")],
295
+ revision_id: Annotated[str, typer.Argument(help="Revision ID.")],
296
+ ) -> None:
297
+ """Get metadata for a specific revision."""
298
+ service = DriveService()
299
+ service.get_revision(file_id=file_id, revision_id=revision_id)
300
+
301
+
302
+ @app.command("delete-revision")
303
+ def delete_revision(
304
+ file_id: Annotated[str, typer.Argument(help="File ID.")],
305
+ revision_id: Annotated[str, typer.Argument(help="Revision ID to delete.")],
306
+ ) -> None:
307
+ """Delete a revision (cannot delete the last remaining revision)."""
308
+ service = DriveService()
309
+ service.delete_revision(file_id=file_id, revision_id=revision_id)
310
+
311
+
312
+ # ===== Trash =====
313
+
314
+
315
+ @app.command("list-trash")
316
+ def list_trash(
317
+ max_results: Annotated[
318
+ int,
319
+ typer.Option("--max", "-m", help="Maximum number of files to return."),
320
+ ] = 20,
321
+ ) -> None:
322
+ """List files in trash."""
323
+ service = DriveService()
324
+ service.list_trash(max_results=max_results)
325
+
326
+
327
+ @app.command("restore")
328
+ def restore_from_trash(
329
+ file_id: Annotated[str, typer.Argument(help="File ID to restore from trash.")],
330
+ ) -> None:
331
+ """Restore a file from trash."""
332
+ service = DriveService()
333
+ service.restore_from_trash(file_id=file_id)
334
+
335
+
336
+ @app.command("empty-trash")
337
+ def empty_trash() -> None:
338
+ """Permanently delete all files in trash."""
339
+ service = DriveService()
340
+ service.empty_trash()
341
+
342
+
343
+ # ===== Permissions =====
344
+
345
+
346
+ @app.command("list-permissions")
347
+ def list_permissions(
348
+ file_id: Annotated[str, typer.Argument(help="File ID to list permissions for.")],
349
+ ) -> None:
350
+ """List all permissions on a file (who has access)."""
351
+ service = DriveService()
352
+ service.list_permissions(file_id=file_id)
353
+
354
+
355
+ @app.command("get-permission")
356
+ def get_permission(
357
+ file_id: Annotated[str, typer.Argument(help="File ID.")],
358
+ permission_id: Annotated[str, typer.Argument(help="Permission ID.")],
359
+ ) -> None:
360
+ """Get details of a specific permission."""
361
+ service = DriveService()
362
+ service.get_permission(file_id=file_id, permission_id=permission_id)
363
+
364
+
365
+ @app.command("update-permission")
366
+ def update_permission(
367
+ file_id: Annotated[str, typer.Argument(help="File ID.")],
368
+ permission_id: Annotated[str, typer.Argument(help="Permission ID to update.")],
369
+ role: Annotated[str, typer.Argument(help="New role (reader, commenter, writer, organizer).")],
370
+ expiration: Annotated[
371
+ Optional[str],
372
+ typer.Option("--expiration", "-e", help="Expiration time (ISO 8601 format)."),
373
+ ] = None,
374
+ ) -> None:
375
+ """Update a permission's role."""
376
+ service = DriveService()
377
+ service.update_permission(
378
+ file_id=file_id,
379
+ permission_id=permission_id,
380
+ role=role,
381
+ expiration_time=expiration,
382
+ )
383
+
384
+
385
+ @app.command("delete-permission")
386
+ def delete_permission(
387
+ file_id: Annotated[str, typer.Argument(help="File ID.")],
388
+ permission_id: Annotated[str, typer.Argument(help="Permission ID to remove.")],
389
+ ) -> None:
390
+ """Remove a permission from a file (unshare)."""
391
+ service = DriveService()
392
+ service.delete_permission(file_id=file_id, permission_id=permission_id)
393
+
394
+
395
+ @app.command("transfer-ownership")
396
+ def transfer_ownership(
397
+ file_id: Annotated[str, typer.Argument(help="File ID.")],
398
+ new_owner_email: Annotated[str, typer.Argument(help="Email of the new owner.")],
399
+ ) -> None:
400
+ """Transfer ownership of a file to another user."""
401
+ service = DriveService()
402
+ service.transfer_ownership(file_id=file_id, new_owner_email=new_owner_email)
403
+
404
+
405
+ # ===== Replies (extended) =====
406
+
407
+
408
+ @app.command("list-replies")
409
+ def list_replies(
410
+ file_id: Annotated[str, typer.Argument(help="File ID.")],
411
+ comment_id: Annotated[str, typer.Argument(help="Comment ID.")],
412
+ max_results: Annotated[
413
+ int,
414
+ typer.Option("--max", "-m", help="Maximum number of replies to return."),
415
+ ] = 20,
416
+ ) -> None:
417
+ """List replies to a comment."""
418
+ service = DriveService()
419
+ service.list_replies(file_id=file_id, comment_id=comment_id, max_results=max_results)
420
+
421
+
422
+ @app.command("get-reply")
423
+ def get_reply(
424
+ file_id: Annotated[str, typer.Argument(help="File ID.")],
425
+ comment_id: Annotated[str, typer.Argument(help="Comment ID.")],
426
+ reply_id: Annotated[str, typer.Argument(help="Reply ID.")],
427
+ ) -> None:
428
+ """Get a specific reply."""
429
+ service = DriveService()
430
+ service.get_reply(file_id=file_id, comment_id=comment_id, reply_id=reply_id)
431
+
432
+
433
+ @app.command("update-reply")
434
+ def update_reply(
435
+ file_id: Annotated[str, typer.Argument(help="File ID.")],
436
+ comment_id: Annotated[str, typer.Argument(help="Comment ID.")],
437
+ reply_id: Annotated[str, typer.Argument(help="Reply ID.")],
438
+ content: Annotated[str, typer.Argument(help="New reply content.")],
439
+ ) -> None:
440
+ """Update a reply's content."""
441
+ service = DriveService()
442
+ service.update_reply(file_id=file_id, comment_id=comment_id, reply_id=reply_id, content=content)
443
+
444
+
445
+ @app.command("delete-reply")
446
+ def delete_reply(
447
+ file_id: Annotated[str, typer.Argument(help="File ID.")],
448
+ comment_id: Annotated[str, typer.Argument(help="Comment ID.")],
449
+ reply_id: Annotated[str, typer.Argument(help="Reply ID to delete.")],
450
+ ) -> None:
451
+ """Delete a reply."""
452
+ service = DriveService()
453
+ service.delete_reply(file_id=file_id, comment_id=comment_id, reply_id=reply_id)
454
+
455
+
456
+ # ===== Revisions (extended) =====
457
+
458
+
459
+ @app.command("update-revision")
460
+ def update_revision(
461
+ file_id: Annotated[str, typer.Argument(help="File ID.")],
462
+ revision_id: Annotated[str, typer.Argument(help="Revision ID.")],
463
+ keep_forever: Annotated[
464
+ Optional[bool],
465
+ typer.Option("--keep-forever", help="Keep this revision forever."),
466
+ ] = None,
467
+ published: Annotated[
468
+ Optional[bool],
469
+ typer.Option("--published", help="Publish this revision."),
470
+ ] = None,
471
+ publish_auto: Annotated[
472
+ Optional[bool],
473
+ typer.Option("--publish-auto", help="Auto-publish future revisions."),
474
+ ] = None,
475
+ ) -> None:
476
+ """Update revision metadata (keep forever, publish settings)."""
477
+ service = DriveService()
478
+ service.update_revision(
479
+ file_id=file_id,
480
+ revision_id=revision_id,
481
+ keep_forever=keep_forever,
482
+ published=published,
483
+ publish_auto=publish_auto,
484
+ )
485
+
486
+
487
+ # ===== Changes =====
488
+
489
+
490
+ @app.command("changes-token")
491
+ def get_changes_token() -> None:
492
+ """Get a token for tracking future file changes."""
493
+ service = DriveService()
494
+ service.get_start_page_token()
495
+
496
+
497
+ @app.command("list-changes")
498
+ def list_changes(
499
+ page_token: Annotated[str, typer.Argument(help="Page token from changes-token command.")],
500
+ page_size: Annotated[
501
+ int,
502
+ typer.Option("--max", "-m", help="Maximum number of changes to return."),
503
+ ] = 100,
504
+ include_removed: Annotated[
505
+ bool,
506
+ typer.Option("--include-removed/--no-removed", help="Include removed files."),
507
+ ] = True,
508
+ ) -> None:
509
+ """List changes to files since the given token."""
510
+ service = DriveService()
511
+ service.list_changes(
512
+ page_token=page_token,
513
+ page_size=page_size,
514
+ include_removed=include_removed,
515
+ )
516
+
517
+
518
+ # ===== Shared Drives =====
519
+
520
+
521
+ @app.command("list-shared-drives")
522
+ def list_shared_drives(
523
+ max_results: Annotated[
524
+ int,
525
+ typer.Option("--max", "-m", help="Maximum number of drives to return."),
526
+ ] = 100,
527
+ page_token: Annotated[
528
+ Optional[str],
529
+ typer.Option("--page-token", help="Token for pagination."),
530
+ ] = None,
531
+ ) -> None:
532
+ """List shared drives the user has access to."""
533
+ service = DriveService()
534
+ service.list_shared_drives(max_results=max_results, page_token=page_token)
535
+
536
+
537
+ @app.command("get-shared-drive")
538
+ def get_shared_drive(
539
+ drive_id: Annotated[str, typer.Argument(help="Shared drive ID.")],
540
+ ) -> None:
541
+ """Get metadata for a shared drive."""
542
+ service = DriveService()
543
+ service.get_shared_drive(drive_id=drive_id)
544
+
545
+
546
+ @app.command("create-shared-drive")
547
+ def create_shared_drive(
548
+ name: Annotated[str, typer.Argument(help="Name for the shared drive.")],
549
+ ) -> None:
550
+ """Create a new shared drive."""
551
+ service = DriveService()
552
+ service.create_shared_drive(name=name)
553
+
554
+
555
+ @app.command("delete-shared-drive")
556
+ def delete_shared_drive(
557
+ drive_id: Annotated[str, typer.Argument(help="Shared drive ID to delete.")],
558
+ ) -> None:
559
+ """Delete a shared drive (must be empty)."""
560
+ service = DriveService()
561
+ service.delete_shared_drive(drive_id=drive_id)
562
+
563
+
564
+ # ===== File IDs =====
565
+
566
+
567
+ @app.command("generate-ids")
568
+ def generate_ids(
569
+ count: Annotated[
570
+ int,
571
+ typer.Option("--count", "-c", help="Number of IDs to generate (max 1000)."),
572
+ ] = 10,
573
+ space: Annotated[
574
+ str,
575
+ typer.Option("--space", "-s", help="Space for IDs (drive or appDataFolder)."),
576
+ ] = "drive",
577
+ ) -> None:
578
+ """Generate file IDs for use with create operations."""
579
+ service = DriveService()
580
+ service.generate_ids(count=count, space=space)