diff --git a/funkwhale_cli/cli.py b/funkwhale_cli/cli.py index 6d58b9864a5c6c2adff04932d2c4d0d066edb267..12bc25f09459f519405cfcfb96526a9e8560e770 100644 --- a/funkwhale_cli/cli.py +++ b/funkwhale_cli/cli.py @@ -267,6 +267,7 @@ def get_ls_command(group, endpoint, output_conf): @click.option("--ordering", "-o", default=None) @click.option("--filter", "-f", multiple=True) @click.option("--ids", "-i", is_flag=True) + @click.option("--limit", "-l", type=click.INT, default=1) @click.option( "--column", "-c", @@ -289,61 +290,75 @@ def get_ls_command(group, endpoint, output_conf): format, no_headers, ids, + limit, ): if ids: no_headers = True column = [output_conf.get("id_field", "UUID")] format = "plain" - async with ctx.obj["remote"]: - params = {"page": page} - if page_size: - params["page_size"] = page_size - if ordering: - params["ordering"] = ordering - if query: - params["q"] = " ".join(query) - if filter: - for f in filter: - query = urllib.parse.parse_qs(f) - for k, v in query.items(): - params[k] = v[0] - - result = await ctx.obj["remote"].request("get", endpoint, params=params) - result.raise_for_status() - payload = await result.json() - if raw: - click.echo(json.dumps(payload, sort_keys=True, indent=4)) - else: - click.echo( - output.table( - payload["results"], - column or output_conf["labels"], - type=output_conf["type"], - format=format, - headers=not no_headers, - ) - ) - pagination_data = get_pagination_data(payload) - if pagination_data["page_size"]: - start = ( - int( - (pagination_data["current_page"] - 1) - * pagination_data["page_size"] + next_page_url = None + page_count = 0 + while True: + if limit and page_count >= limit: + break + async with ctx.obj["remote"]: + if page_count == 0: + url = endpoint + params = {"page": page} + if page_size: + params["page_size"] = page_size + if ordering: + params["ordering"] = ordering + if query: + params["q"] = " ".join(query) + if filter: + for f in filter: + query = urllib.parse.parse_qs(f) + for k, v in query.items(): + params[k] = v[0] + else: + params = {} + url = next_page_url + if not url: + break + result = await ctx.obj["remote"].request("get", url, params=params) + result.raise_for_status() + payload = await result.json() + next_page_url = payload['next'] + page_count += 1 + if raw: + click.echo(json.dumps(payload, sort_keys=True, indent=4)) + else: + click.echo( + output.table( + payload["results"], + column or output_conf["labels"], + type=output_conf["type"], + format=format, + headers=not no_headers, ) - + 1 ) - else: - start = 1 - end = min(start + len(payload["results"]) - 1, payload["count"]) - logs.logger.info( - "\nObjects {start}-{end} on {total} (page {current_page}/{total_pages})".format( - start=start, - end=end, - total=payload["count"], - current_page=pagination_data["current_page"], - total_pages=pagination_data["total_pages"] or 1, + pagination_data = get_pagination_data(payload) + if pagination_data["page_size"]: + start = ( + int( + (pagination_data["current_page"] - 1) + * pagination_data["page_size"] + ) + + 1 + ) + else: + start = 1 + end = min(start + len(payload["results"]) - 1, payload["count"]) + logs.logger.info( + "\nObjects {start}-{end} on {total} (page {current_page}/{total_pages})".format( + start=start, + end=end, + total=payload["count"], + current_page=pagination_data["current_page"], + total_pages=pagination_data["total_pages"] or 1, + ) ) - ) return ls