|
14 | 14 | from contextlib import suppress |
15 | 15 | from datetime import datetime |
16 | 16 | from math import ceil |
17 | | -from pathlib import Path |
18 | | -from typing import List |
| 17 | +from typing import Iterable, List |
19 | 18 |
|
20 | 19 | from rich import print as rprint |
21 | 20 | from rich.table import Table |
22 | 21 |
|
23 | 22 | from linodecli.cli import CLI |
24 | 23 | from linodecli.configuration import _do_get_request |
25 | 24 | from linodecli.configuration.helpers import _default_thing_input |
26 | | -from linodecli.helpers import expand_globs |
| 25 | +from linodecli.helpers import expand_globs, pagination_args_shared |
27 | 26 | from linodecli.plugins import PluginContext, inherit_plugin_args |
28 | 27 | from linodecli.plugins.obj.buckets import create_bucket, delete_bucket |
29 | 28 | from linodecli.plugins.obj.config import ( |
|
70 | 69 | except ImportError: |
71 | 70 | HAS_BOTO = False |
72 | 71 |
|
| 72 | +TRUNCATED_MSG = ( |
| 73 | + "Notice: Not all results were shown. If your would " |
| 74 | + "like to get more results, you can add the '--all-row' " |
| 75 | + "flag to the command or use the built-in pagination flags." |
| 76 | +) |
| 77 | + |
| 78 | +INVALID_PAGE_MSG = "No result to show in this page." |
| 79 | + |
| 80 | + |
| 81 | +def flip_to_page(iterable: Iterable, page: int = 1): |
| 82 | + """Given a iterable object and return a specific iteration (page)""" |
| 83 | + iterable = iter(iterable) |
| 84 | + for _ in range(page - 1): |
| 85 | + try: |
| 86 | + next(iterable) |
| 87 | + except StopIteration: |
| 88 | + print(INVALID_PAGE_MSG) |
| 89 | + sys.exit(2) |
| 90 | + |
| 91 | + return next(iterable) |
| 92 | + |
73 | 93 |
|
74 | 94 | def list_objects_or_buckets( |
75 | 95 | get_client, args, **kwargs |
76 | | -): # pylint: disable=too-many-locals,unused-argument |
| 96 | +): # pylint: disable=too-many-locals,unused-argument,too-many-branches |
77 | 97 | """ |
78 | 98 | Lists buckets or objects |
79 | 99 | """ |
80 | 100 | parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " ls")) |
| 101 | + pagination_args_shared(parser) |
81 | 102 |
|
82 | 103 | parser.add_argument( |
83 | 104 | "bucket", |
@@ -106,16 +127,30 @@ def list_objects_or_buckets( |
106 | 127 | prefix = "" |
107 | 128 |
|
108 | 129 | data = [] |
| 130 | + objects = [] |
| 131 | + sub_directories = [] |
| 132 | + pages = client.get_paginator("list_objects_v2").paginate( |
| 133 | + Prefix=prefix, |
| 134 | + Bucket=bucket_name, |
| 135 | + Delimiter="/", |
| 136 | + PaginationConfig={"PageSize": parsed.page_size}, |
| 137 | + ) |
109 | 138 | try: |
110 | | - response = client.list_objects_v2( |
111 | | - Prefix=prefix, Bucket=bucket_name, Delimiter="/" |
112 | | - ) |
| 139 | + if parsed.all_rows: |
| 140 | + results = pages |
| 141 | + else: |
| 142 | + page = flip_to_page(pages, parsed.page) |
| 143 | + if page.get("IsTruncated", False): |
| 144 | + print(TRUNCATED_MSG) |
| 145 | + |
| 146 | + results = [page] |
113 | 147 | except client.exceptions.NoSuchBucket: |
114 | 148 | print("No bucket named " + bucket_name) |
115 | 149 | sys.exit(2) |
116 | 150 |
|
117 | | - objects = response.get("Contents", []) |
118 | | - sub_directories = response.get("CommonPrefixes", []) |
| 151 | + for item in results: |
| 152 | + objects.extend(item.get("Contents", [])) |
| 153 | + sub_directories.extend(item.get("CommonPrefixes", [])) |
119 | 154 |
|
120 | 155 | for d in sub_directories: |
121 | 156 | data.append((" " * 16, "DIR", d.get("Prefix"))) |
@@ -329,17 +364,31 @@ def list_all_objects( |
329 | 364 | """ |
330 | 365 | # this is for printing help when --help is in the args |
331 | 366 | parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " la")) |
| 367 | + pagination_args_shared(parser) |
332 | 368 |
|
333 | | - parser.parse_args(args) |
| 369 | + parsed = parser.parse_args(args) |
334 | 370 |
|
335 | 371 | client = get_client() |
336 | 372 |
|
337 | | - # all buckets |
338 | 373 | buckets = [b["Name"] for b in client.list_buckets().get("Buckets", [])] |
339 | 374 |
|
340 | 375 | for b in buckets: |
341 | 376 | print() |
342 | | - objects = client.list_objects_v2(Bucket=b).get("Contents", []) |
| 377 | + objects = [] |
| 378 | + pages = client.get_paginator("list_objects_v2").paginate( |
| 379 | + Bucket=b, PaginationConfig={"PageSize": parsed.page_size} |
| 380 | + ) |
| 381 | + if parsed.all_rows: |
| 382 | + results = pages |
| 383 | + else: |
| 384 | + page = flip_to_page(pages, parsed.page) |
| 385 | + if page.get("IsTruncated", False): |
| 386 | + print(TRUNCATED_MSG) |
| 387 | + |
| 388 | + results = [page] |
| 389 | + |
| 390 | + for page in results: |
| 391 | + objects.extend(page.get("Contents", [])) |
343 | 392 |
|
344 | 393 | for obj in objects: |
345 | 394 | size = obj.get("Size", 0) |
|
0 commit comments