Skip to content
Snippets Groups Projects
Verified Commit e62ea5fc authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Support looping through pages in ls command

parent 19c4b751
Branches
No related tags found
1 merge request!3Ci
...@@ -267,6 +267,7 @@ def get_ls_command(group, endpoint, output_conf): ...@@ -267,6 +267,7 @@ def get_ls_command(group, endpoint, output_conf):
@click.option("--ordering", "-o", default=None) @click.option("--ordering", "-o", default=None)
@click.option("--filter", "-f", multiple=True) @click.option("--filter", "-f", multiple=True)
@click.option("--ids", "-i", is_flag=True) @click.option("--ids", "-i", is_flag=True)
@click.option("--limit", "-l", type=click.INT, default=1)
@click.option( @click.option(
"--column", "--column",
"-c", "-c",
...@@ -289,12 +290,20 @@ def get_ls_command(group, endpoint, output_conf): ...@@ -289,12 +290,20 @@ def get_ls_command(group, endpoint, output_conf):
format, format,
no_headers, no_headers,
ids, ids,
limit,
): ):
if ids: if ids:
no_headers = True no_headers = True
column = [output_conf.get("id_field", "UUID")] column = [output_conf.get("id_field", "UUID")]
format = "plain" format = "plain"
next_page_url = None
page_count = 0
while True:
if limit and page_count >= limit:
break
async with ctx.obj["remote"]: async with ctx.obj["remote"]:
if page_count == 0:
url = endpoint
params = {"page": page} params = {"page": page}
if page_size: if page_size:
params["page_size"] = page_size params["page_size"] = page_size
...@@ -307,10 +316,16 @@ def get_ls_command(group, endpoint, output_conf): ...@@ -307,10 +316,16 @@ def get_ls_command(group, endpoint, output_conf):
query = urllib.parse.parse_qs(f) query = urllib.parse.parse_qs(f)
for k, v in query.items(): for k, v in query.items():
params[k] = v[0] params[k] = v[0]
else:
result = await ctx.obj["remote"].request("get", endpoint, params=params) params = {}
url = next_page_url
if not url:
break
result = await ctx.obj["remote"].request("get", url, params=params)
result.raise_for_status() result.raise_for_status()
payload = await result.json() payload = await result.json()
next_page_url = payload['next']
page_count += 1
if raw: if raw:
click.echo(json.dumps(payload, sort_keys=True, indent=4)) click.echo(json.dumps(payload, sort_keys=True, indent=4))
else: else:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment