Skip to content

Commit

Permalink
Add support for --fields parameter in search command
Browse files Browse the repository at this point in the history
---------

Co-authored-by: carlevison <[email protected]>
  • Loading branch information
const-cloudinary and carlevison authored Dec 3, 2023
1 parent 7821773 commit dce2801
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
25 changes: 18 additions & 7 deletions cloudinary_cli/core/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
e.g. cld search cat AND tags:kitten -s public_id desc -f context -f tags -n 10
""")
@argument("query", nargs=-1)
@option("-f", "--with_field", multiple=True, help="Specify which asset attribute to include in the result.")
@option("-f", "--with_field", multiple=True, help="Specify which non-default asset attributes to include "
"in the result as a comma separated list. ")
@option("-fi", "--fields", multiple=True, help="Specify which asset attributes to include in the result "
"(together with a subset of the default attributes) as a comma separated"
" list. This overrides any value specified for with_field.")
@option("-s", "--sort_by", nargs=2, help="Sort search results by (field, <asc|desc>).")
@option("-a", "--aggregate", nargs=1,
help="Specify the attribute for which an aggregation count should be calculated and returned.")
Expand All @@ -26,28 +30,31 @@
@option("-c", "--next_cursor", nargs=1, help="Continue a search using an existing cursor.")
@option("-A", "--auto_paginate", is_flag=True, help="Return all results. Will call Admin API multiple times.")
@option("-F", "--force", is_flag=True, help="Skip confirmation when running --auto-paginate.")
@option("-ff", "--filter_fields", multiple=True, help="Filter fields to return.")
@option("-ff", "--filter_fields", multiple=True, help="Specify which attributes to show in the response. "
"None of the others will be shown.")
@option("-t", "--ttl", nargs=1, default=300, help="Set the Search URL TTL in seconds. Default: 300.")
@option("-u", "--url", is_flag=True, help="Build a signed search URL.")
@option("-sq", "--search-query", is_flag=True, help="Show the search request query.", hidden=True)
@option("--json", nargs=1, help="Save JSON output to a file. Usage: --json <filename>")
@option("--csv", nargs=1, help="Save CSV output to a file. Usage: --csv <filename>")
@option("-d", "--doc", is_flag=True, help="Open Search API documentation page.")
def search(query, with_field, sort_by, aggregate, max_results, next_cursor,
auto_paginate, force, filter_fields, ttl, url, json, csv, doc):
def search(query, with_field, fields, sort_by, aggregate, max_results, next_cursor,
auto_paginate, force, filter_fields, ttl, url, search_query, json, csv, doc):
if doc:
return launch("https://cloudinary.com/documentation/search_api")

fields_to_keep = []
if filter_fields:
fields_to_keep = tuple(normalize_list_params(filter_fields)) + with_field
fields_to_keep = tuple(normalize_list_params(filter_fields)) + tuple(normalize_list_params(with_field))

search = cloudinary.search.Search().expression(" ".join(query))

if auto_paginate:
max_results = DEFAULT_MAX_RESULTS
if with_field:
for f in with_field:
search.with_field(f)
search.with_field(normalize_list_params(with_field))
if fields:
search.fields(normalize_list_params(fields))
if sort_by:
search.sort_by(*sort_by)
if aggregate:
Expand All @@ -63,6 +70,10 @@ def search(query, with_field, sort_by, aggregate, max_results, next_cursor,
print(search.to_url())
return True

if search_query:
print_json(search.as_dict())
return True

res = execute_single_request(search, fields_to_keep)

if auto_paginate:
Expand Down
6 changes: 6 additions & 0 deletions test/test_cli_search_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ def test_search(self, mocker):
self.assertEqual(0, result.exit_code)
self.assertIn('"foo": "bar"', result.output)

def test_search_fields(self):
result = self.runner.invoke(cli, ['search', 'cat', '-fi', 'url,tags', '-fi', 'context', '--search-query'])

self.assertEqual(0, result.exit_code)
self.assertIn('"fields": [\n "url",\n "tags",\n "context"\n ]', result.output)

def test_search_url(self):
result = self.runner.invoke(cli, ['search', 'cat', '-c', 'NEXT_CURSOR', '--ttl', '1000', '--url'])

Expand Down

0 comments on commit dce2801

Please sign in to comment.