Skip to content
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

Added --ids-only option #940

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
23 changes: 18 additions & 5 deletions planet/cli/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,9 @@ def filter(ctx,
default=SEARCH_SORT_DEFAULT,
show_default=True,
help='Field and direction to order results by.')
@click.option('--ids-only', is_flag=True, help='Returns only the item IDs.')
@pretty
async def search(ctx, item_types, filter, limit, name, sort, pretty):
async def search(ctx, item_types, filter, limit, name, sort, ids_only, pretty):
"""Execute a structured item search.

This function outputs a series of GeoJSON descriptions, one for each of the
Expand All @@ -309,13 +310,18 @@ async def search(ctx, item_types, filter, limit, name, sort, pretty):
parameter will be applied to the stored quick search.
"""
async with data_client(ctx) as cl:

item_ids = []
Copy link
Contributor

@sgillies sgillies May 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In #645 we've recognized that search results may cover multiple item_types and that mixing their ids together sets a user up for failure. @kevinlacaille what would you think about this?

Suggested change
item_ids = []
if ids_only and len(item_types) > 1:
raise ClickException("item id output is not allowed when multiple item types have been searched.")
item_ids = []

async for item in cl.search(item_types,
search_filter=filter,
name=name,
sort=sort,
limit=limit):
echo_json(item, pretty)
if ids_only:
item_ids.append(item['id'])
else:
echo_json(item, pretty)
Copy link
Contributor

@sgillies sgillies Apr 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevinlacaille In #645 I'm pushing back a little on this feature request, but if we did it, this is completely sound. Append to list or print to terminal, this is easy to read and understand. It's symmetry in action.

if ids_only:
click.echo(','.join(item_ids))


@data.command(epilog=valid_item_string)
Expand Down Expand Up @@ -395,16 +401,23 @@ async def search_list(ctx, sort, search_type, limit, pretty):
show_default=True,
help='Field and direction to order results by.')
@limit
@click.option('--ids-only', is_flag=True, help='Returns only the item IDs.')
@pretty
async def search_run(ctx, search_id, sort, limit, pretty):
async def search_run(ctx, search_id, sort, limit, ids_only, pretty):
"""Execute a saved structured item search.

This function outputs a series of GeoJSON descriptions, one for each of the
returned items, optionally pretty-printed.
"""
async with data_client(ctx) as cl:
item_ids = []
async for item in cl.run_search(search_id, sort=sort, limit=limit):
echo_json(item, pretty)
if ids_only:
item_ids.append(item['id'])
else:
echo_json(item, pretty)
if ids_only:
click.echo(','.join(item_ids))


@data.command(epilog=valid_item_string)
Expand Down
13 changes: 8 additions & 5 deletions planet/cli/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,20 @@ async def list(ctx, state, limit, pretty):
@translate_exceptions
@coro
@click.argument('order_id', type=click.UUID)
@click.option('--ids-only', is_flag=True, help='Returns only the item IDs.')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevinlacaille #645 is only about getting a list of ids from search. Let's not extend to the orders CLI here. Can you remove the changes to planet/cli/orders.py?

@pretty
async def get(ctx, order_id, pretty):
async def get(ctx, order_id, ids_only, pretty):
"""Get order

This command outputs the order description, optionally pretty-printed.
"""
async with orders_client(ctx) as cl:
order = await cl.get_order(str(order_id))

echo_json(order, pretty)
if ids_only:
item_ids = order['products'][0]['item_ids']
click.echo(','.join(item_ids))
else:
echo_json(order, pretty)


@orders.command()
Expand Down Expand Up @@ -217,8 +221,7 @@ async def create(ctx, request: str, pretty):
'''
async with orders_client(ctx) as cl:
order = await cl.create_order(request)

echo_json(order, pretty)
echo_json(order, pretty)


@orders.command()
Expand Down