Skip to content

Commit 3f3ebb0

Browse files
committed
add record and table --format options
1 parent e9bcf51 commit 3f3ebb0

File tree

7 files changed

+83
-3
lines changed

7 files changed

+83
-3
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
### Enhancements
22

33
* Add Domain field to default output of `globus endpoint search` and `globus endpoint show`
4+
* Add record" and "table" to the options for `--format` to coerce tabular text outputs
5+
into lists of records and vice versa. `--format record` may be especially useful
6+
for displaying information that defaults to wide tables in narrow terminals

src/globus_cli/commands/endpoint/search.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
$ globus endpoint search Tutorial --filter-owner-id go@globusid.org
2828
----
2929
30-
Search for endpoints owned by the current user
30+
Search for endpoints owned by the current user.
3131
3232
[source,bash]
3333
----
@@ -122,6 +122,9 @@ def endpoint_search(
122122
If FILTER_FULLTEXT is given, endpoints which have attributes (display name,
123123
legacy name, description, organization, department, keywords) that match the
124124
search text will be returned. The result size limit is 100 endpoints.
125+
126+
If the default table output is too wide for your terminal consider using
127+
--format record.
125128
"""
126129
from globus_cli.services.transfer import (
127130
ENDPOINT_LIST_FIELDS,

src/globus_cli/parsing/command_state.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
JSON_FORMAT = "json"
1414
TEXT_FORMAT = "text"
1515
UNIX_FORMAT = "unix"
16+
RECORD_FORMAT = "record"
17+
TABLE_FORMAT = "table"
1618

1719
F = t.TypeVar("F", bound=AnyCommand)
1820

@@ -64,6 +66,12 @@ def outformat_is_json(self) -> bool:
6466
def outformat_is_unix(self) -> bool:
6567
return self.output_format == UNIX_FORMAT
6668

69+
def outformat_is_record(self) -> bool:
70+
return self.output_format == RECORD_FORMAT
71+
72+
def outformat_is_table(self) -> bool:
73+
return self.output_format == TABLE_FORMAT
74+
6775
def is_verbose(self) -> bool:
6876
return self.verbosity > 0
6977

@@ -128,7 +136,8 @@ def jmespath_callback(
128136
"-F",
129137
"--format",
130138
type=click.Choice(
131-
[UNIX_FORMAT, JSON_FORMAT, TEXT_FORMAT], case_sensitive=False
139+
[UNIX_FORMAT, JSON_FORMAT, TEXT_FORMAT, RECORD_FORMAT, TABLE_FORMAT],
140+
case_sensitive=False,
132141
),
133142
help="Output format for stdout. Defaults to text.",
134143
expose_value=False,

src/globus_cli/termio/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def print_command_hint(message: str, *, color: str = "yellow") -> None:
3939
"outformat_is_json",
4040
"outformat_is_text",
4141
"outformat_is_unix",
42+
"outformat_is_record",
43+
"outformat_is_table",
4244
"get_jmespath_expression",
4345
"verbosity",
4446
"is_verbose",

src/globus_cli/termio/_display.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
import click
77
import globus_sdk
88

9-
from .context import outformat_is_json, outformat_is_text, outformat_is_unix
9+
from .context import (
10+
outformat_is_json,
11+
outformat_is_record,
12+
outformat_is_table,
13+
outformat_is_text,
14+
outformat_is_unix,
15+
)
1016
from .field import Field
1117
from .printers import (
1218
CustomPrinter,
@@ -94,6 +100,31 @@ def __call__(
94100
(json output only)
95101
"""
96102

103+
# if --format record was used, change table outputs into record_lists.
104+
# no-op if output format is already record or record list. error on
105+
# other output types.
106+
if outformat_is_record():
107+
if text_mode not in (
108+
TextMode.text_table,
109+
TextMode.text_record,
110+
TextMode.text_record_list,
111+
):
112+
raise click.UsageError(
113+
"This command does not support record output formatting"
114+
)
115+
if text_mode == TextMode.text_table:
116+
text_mode = TextMode.text_record_list
117+
118+
# if --format table was used, change record list outputs into table
119+
# outputs. no-op if output format is already table. error on other
120+
# output types.
121+
elif outformat_is_table():
122+
if text_mode not in (TextMode.text_table, TextMode.text_record_list):
123+
raise click.UsageError(
124+
"This command does not support table output formatting"
125+
)
126+
text_mode = TextMode.text_table
127+
97128
if isinstance(response_data, globus_sdk.GlobusHTTPResponse):
98129
maybe_show_server_timing(response_data)
99130

src/globus_cli/termio/context.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ def outformat_is_text() -> bool:
3737
return state.outformat_is_text()
3838

3939

40+
def outformat_is_record() -> bool:
41+
"""
42+
Only safe to call within a click context.
43+
"""
44+
ctx = click.get_current_context()
45+
state = ctx.ensure_object(CommandState)
46+
return state.outformat_is_record()
47+
48+
49+
def outformat_is_table() -> bool:
50+
"""
51+
Only safe to call within a click context.
52+
"""
53+
ctx = click.get_current_context()
54+
state = ctx.ensure_object(CommandState)
55+
return state.outformat_is_table()
56+
57+
4058
def get_jmespath_expression() -> t.Any:
4159
"""
4260
Only safe to call within a click context.

tests/functional/endpoint/test_endpoint_search.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,20 @@ def test_search_shows_collection_id(run_line, singular_search_response):
193193
assert meta["endpoint_id"] not in result.output
194194

195195

196+
def test_search_format_record(run_line, singular_search_response):
197+
singular_search_response.add()
198+
meta = singular_search_response.metadata
199+
200+
result = run_line("globus endpoint search mytestquery --format record")
201+
202+
assert result.output == (
203+
f"ID: {meta['collection_id']}\n"
204+
f"Owner: {meta['owner_string']}\n"
205+
f"Display Name: {meta['display_name']}\n"
206+
f"Domain: {meta['collection_fqdn']}\n"
207+
)
208+
209+
196210
@pytest.mark.parametrize(
197211
"entity_type",
198212
(

0 commit comments

Comments
 (0)