Skip to content

Commit 77f6737

Browse files
author
Juliya Smith
authored
Feature/interactive errors (#9)
1 parent f3fb4a9 commit 77f6737

File tree

6 files changed

+131
-54
lines changed

6 files changed

+131
-54
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ how a consumer would use the library (e.g. adding unit tests, updating documenta
1313
### Added
1414

1515
- Begin and end date now support specifying time: `code42 securitydata print -b 2020-02-02 12:00:00`.
16+
- If running interactively and errors occur, you will be told them at the end of `code42 securitydata` commands.
1617
- New search arguments for `print`, `write-to`, and `send-to`:
1718
- `--c42username`
1819
- `--actor`

src/code42cli/securitydata/extraction.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from py42.sdk.file_event_query.file_query import MD5, SHA256, FileName, FilePath
1313

1414
from code42cli.compat import str
15-
from code42cli.util import print_error, print_bold
15+
from code42cli.util import print_error, print_bold, is_interactive
1616
from code42cli.profile.profile import get_profile
1717
from code42cli.securitydata.options import ExposureType as ExposureTypeOptions
1818
from code42cli.securitydata import date_helper as date_helper
@@ -22,6 +22,9 @@
2222
from code42cli.securitydata.arguments.main import IS_INCREMENTAL_KEY
2323

2424

25+
_EXCEPTIONS_OCCURRED = False
26+
27+
2528
def extract(output_logger, args):
2629
"""Extracts file events using the given command-line arguments.
2730
@@ -38,12 +41,20 @@ def extract(output_logger, args):
3841
sdk = _get_sdk(profile, args.is_debug_mode)
3942
extractor = FileEventExtractor(sdk, handlers)
4043
_call_extract(extractor, args)
44+
_handle_result()
4145

4246

4347
def _create_event_handlers(output_logger, is_incremental):
4448
handlers = FileEventHandlers()
4549
error_logger = get_error_logger()
46-
handlers.handle_error = error_logger.error
50+
51+
def handle_error(exception):
52+
error_logger.error(exception)
53+
global _EXCEPTIONS_OCCURRED
54+
_EXCEPTIONS_OCCURRED = True
55+
56+
handlers.handle_error = handle_error
57+
4758
if is_incremental:
4859
store = AEDCursorStore()
4960
handlers.record_cursor_position = store.replace_stored_insertion_timestamp
@@ -116,7 +127,7 @@ def _verify_begin_date(begin_date):
116127
if not begin_date:
117128
print_error(u"'begin date' is required.")
118129
print(u"")
119-
print(u"Try using '-b' or '--begin'. Use `-h` for more info.")
130+
print_bold(u"Try using '-b' or '--begin'. Use `-h` for more info.")
120131
print(u"")
121132
exit(1)
122133

@@ -131,6 +142,11 @@ def _verify_exposure_types(exposure_types):
131142
exit(1)
132143

133144

145+
def _handle_result():
146+
if is_interactive() and _EXCEPTIONS_OCCURRED:
147+
print_error(u"View exceptions that occurred at [HOME]/.code42cli/log/code42_errors.")
148+
149+
134150
def _create_filters(args):
135151
filters = [_get_event_timestamp_filter(args)]
136152
not args.c42username or filters.append(DeviceUsername.eq(args.c42username))

src/code42cli/securitydata/subcommands/write_to.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def init(subcommand_parser):
1010
Args:
1111
subcommand_parser: The subparsers group created by the parent parser.
1212
"""
13-
parser = subcommand_parser.add_parser("write-to")
13+
parser = subcommand_parser.add_parser(u"write-to")
1414
parser.set_defaults(func=write_to)
1515
_add_filename_subcommand(parser)
1616
search_args.add_arguments_to_parser(parser)
@@ -19,11 +19,11 @@ def init(subcommand_parser):
1919

2020
def write_to(args):
2121
"""Activates 'write-to' command. It gets security events and writes them to the given file."""
22-
logger = get_logger_for_file(args.filename, args.format)
22+
logger = get_logger_for_file(args.output_file, args.format)
2323
extract(logger, args)
2424

2525

2626
def _add_filename_subcommand(parser):
2727
parser.add_argument(
28-
action="store", dest="filename", help="The name of the local file to send output to."
28+
action=u"store", dest=u"output_file", help=u"The name of the local file to send output to."
2929
)

src/code42cli/util.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ def print_error(error_text):
3737

3838
def print_bold(bold_text):
3939
print("\033[1m{}\033[0m".format(bold_text))
40+
41+
42+
def is_interactive():
43+
return sys.stdin.isatty()

tests/securitydata/subcommands/test_write_to.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
@pytest.fixture
1313
def file_namespace(namespace):
14-
namespace.filename = "out.txt"
14+
namespace.output_file = "out.txt"
1515
namespace.format = "CEF"
1616
return namespace
1717

0 commit comments

Comments
 (0)