Skip to content

Commit 0040474

Browse files
author
Juliya Smith
authored
Output formats (#149)
1 parent 9e636f6 commit 0040474

9 files changed

Lines changed: 28 additions & 54 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ how a consumer would use the library (e.g. adding unit tests, updating documenta
3333
- The `path` positional argument for bulk `generate-template` commands is now an option (`--p/-p`).
3434
- Below `search` subcommands accept argument `--format/-f` to display result in formats `csv`, `table`, `json`, `raw-json`:
3535
- Default output format is changed to `table` format from `raw-json`, returns a paginated response.
36-
A predefined properties would be displayed by default, pass `--include-all` to view all non-nested top-level properties.
36+
All properties would be displayed by default except when using `-f table`.
37+
Pass `--include-all` when using `table` to view all non-nested top-level properties.
3738
- `code42 alerts search`
3839
- `code42 security-data search`
40+
- `code42 security-data saved-search list`
41+
- `code42 legal-hold list`
42+
- `code42 alert-rules list`
3943

4044
### Added
4145

src/code42cli/cmds/alert_rules.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ def list_alert_rules(state, format=None):
7979
formatter = OutputFormatter(format, _HEADER_KEYS_MAP)
8080
selected_rules = _get_all_rules_metadata(state.sdk)
8181
if selected_rules:
82-
for output in formatter.get_formatted_output(selected_rules):
83-
echo(output)
82+
formatter.echo_formatted_list(selected_rules)
8483

8584

8685
@alert_rules.command()

src/code42cli/cmds/legal_hold.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from code42cli.options import format_option
1515
from code42cli.options import OrderedGroup
1616
from code42cli.options import sdk_options
17+
from code42cli.output_formats import OutputFormat
1718
from code42cli.output_formats import OutputFormatter
1819
from code42cli.util import format_string_list_to_columns
1920

@@ -75,8 +76,7 @@ def _list(state, format=None):
7576
formatter = OutputFormatter(format, _MATTER_KEYS_MAP)
7677
matters = _get_all_active_matters(state.sdk)
7778
if matters:
78-
for output in formatter.get_formatted_output(matters):
79-
echo(output)
79+
formatter.echo_formatted_list(matters)
8080

8181

8282
@legal_hold.command()
@@ -92,11 +92,9 @@ def _list(state, format=None):
9292
is_flag=True,
9393
help="View details of the preservation policy associated with the legal hold matter.",
9494
)
95-
@format_option
9695
@sdk_options()
97-
def show(state, matter_id, include_inactive=False, include_policy=False, format=None):
96+
def show(state, matter_id, include_inactive=False, include_policy=False):
9897
"""Display details of a given legal hold matter."""
99-
formatter = OutputFormatter(format, _MATTER_KEYS_MAP)
10098
matter = _check_matter_is_accessible(state.sdk, matter_id)
10199
matter["creator_username"] = matter["creator"]["username"]
102100
matter = json.loads(matter.text)
@@ -114,9 +112,8 @@ def show(state, matter_id, include_inactive=False, include_policy=False, format=
114112
member["user"]["username"] for member in memberships if not member["active"]
115113
]
116114

117-
for output in formatter.get_formatted_output([matter]):
118-
echo(output)
119-
115+
formatter = OutputFormatter(OutputFormat.TABLE, _MATTER_KEYS_MAP)
116+
formatter.echo_formatted_list([matter])
120117
_print_matter_members(active_usernames, member_type="active")
121118

122119
if include_inactive:

src/code42cli/cmds/search/extraction.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,10 @@ def handle_response(response):
9494
total_events = len(events)
9595
handlers.TOTAL_EVENTS += total_events
9696

97-
def _format_output():
98-
return formatter.get_formatted_output(events)
99-
10097
if total_events > 10 or force_pager:
101-
click.echo_via_pager(_format_output())
98+
click.echo_via_pager(formatter.get_formatted_output(events))
10299
else:
103-
for page in _format_output():
104-
click.echo(page, nl=False)
105-
if formatter.output_format == OutputFormat.TABLE:
106-
click.echo()
100+
formatter.echo_formatted_list(events)
107101

108102
# To make sure the extractor records correct timestamp event when `CTRL-C` is pressed.
109103
if total_events:

src/code42cli/cmds/securitydata.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def _get_saved_search_query(ctx, param, arg):
166166
"--format",
167167
type=click.Choice(SendToFileEventsOutputFormat(), case_sensitive=False),
168168
help="The output format of the result. Defaults to json format.",
169-
default=SendToFileEventsOutputFormat.JSON,
169+
default=SendToFileEventsOutputFormat.RAW,
170170
)
171171

172172

@@ -286,8 +286,7 @@ def _list(state, format=None):
286286
response = state.sdk.securitydata.savedsearches.get()
287287
saved_searches = response["searches"]
288288
if saved_searches:
289-
for output in formatter.get_formatted_output(saved_searches):
290-
echo(output)
289+
formatter.echo_formatted_list(saved_searches)
291290

292291

293292
@saved_search.command()

src/code42cli/output_formats.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import io
33
import json
44

5+
import click
6+
57
from code42cli.util import find_format_width
68
from code42cli.util import format_to_table
79

@@ -62,6 +64,13 @@ def get_formatted_output(self, output):
6264
for item in output:
6365
yield self._format_output(item)
6466

67+
def echo_formatted_list(self, output_list):
68+
formatted_output = self.get_formatted_output(output_list)
69+
for output in formatted_output:
70+
click.echo(output, nl=False)
71+
if self.output_format in [OutputFormat.TABLE]:
72+
click.echo()
73+
6574
@property
6675
def _requires_list_output(self):
6776
return self.output_format in (OutputFormat.TABLE, OutputFormat.CSV)
@@ -90,7 +99,7 @@ def to_table(output, header):
9099

91100
def to_json(output):
92101
"""Output is a single record"""
93-
return "{}".format(json.dumps(output))
102+
return "{}\n".format(json.dumps(output))
94103

95104

96105
def to_formatted_json(output):

tests/cmds/search/test_extraction.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_try_get_default_header_returns_none_when_is_table_and_told_to_include_a
4343
assert actual is None
4444

4545

46-
def test_create_handlers_creates_handlers_that_pass_events_to_output_format(
46+
def test_create_handlers_creates_handlers_that_pass_events_to_output_formatter(
4747
mocker, sdk,
4848
):
4949
class TestExtractor(BaseExtractor):
@@ -64,7 +64,7 @@ def _get_timestamp_from_item(self, item):
6464
http_response.text = '{{"{0}": [{{"property": "bar"}}]}}'.format(key)
6565
py42_response = Py42Response(http_response)
6666
handlers.handle_response(py42_response)
67-
formatter.get_formatted_output.assert_called_once_with(events)
67+
formatter.echo_formatted_list.assert_called_once_with(events)
6868

6969

7070
def test_send_to_handlers_creates_handlers_that_pass_events_to_logger(

tests/cmds/test_legal_hold.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -572,31 +572,3 @@ def test_list_with_csv_format_returns_no_response_when_response_is_empty(
572572
cli_state.sdk.legalhold.get_all_matters.return_value = empty_matters_response
573573
result = runner.invoke(cli, ["legal-hold", "list", "-f", "csv"], obj=cli_state)
574574
assert "Matter ID,Name,Description,Creator,Creation Date" not in result.output
575-
576-
577-
def test_show_with_csv_format_option_returns_expected_format(
578-
runner,
579-
cli_state,
580-
check_matter_accessible_success,
581-
get_user_id_success,
582-
active_and_inactive_legal_hold_memberships_response,
583-
):
584-
cli_state.sdk.legalhold.get_all_matter_custodians.return_value = (
585-
active_and_inactive_legal_hold_memberships_response
586-
)
587-
result = runner.invoke(
588-
cli, ["legal-hold", "show", TEST_MATTER_ID, "-f", "csv"], obj=cli_state
589-
)
590-
assert "legalHoldUid" in result.output
591-
assert "name" in result.output
592-
assert "description" in result.output
593-
assert "active" in result.output
594-
assert "creationDate" in result.output
595-
assert "lastModified" in result.output
596-
assert "creator" in result.output
597-
assert "holdPolicyUid" in result.output
598-
assert "creator_username" in result.output
599-
assert "88888" in result.output
600-
assert "Test_Matter" in result.output
601-
comma_count = [c for c in result.output if c == ","]
602-
assert len(comma_count) >= 13

tests/test_output_formats.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def test_to_table_when_not_given_header_creates_header_dynamically():
136136

137137
def test_to_json():
138138
formatted_output = output_formats_module.to_json(TEST_DATA)
139-
assert formatted_output == json.dumps(TEST_DATA)
139+
assert formatted_output == "{}\n".format(json.dumps(TEST_DATA))
140140

141141

142142
def test_to_formatted_json():

0 commit comments

Comments
 (0)