mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-21 00:19:42 +02:00
timestamp working on standard and streaming parsers. updates existing _jc_meta if exists
This commit is contained in:
27
jc/cli.py
27
jc/cli.py
@ -442,21 +442,21 @@ def combined_exit_code(program_exit=0, jc_exit=0):
|
|||||||
|
|
||||||
def add_timestamp_to(list_or_dict, runtime, magic_exit_code):
|
def add_timestamp_to(list_or_dict, runtime, magic_exit_code):
|
||||||
"""This function mutates a list or dict in place"""
|
"""This function mutates a list or dict in place"""
|
||||||
run_iso = runtime.isoformat()
|
|
||||||
run_timestamp = runtime.timestamp()
|
run_timestamp = runtime.timestamp()
|
||||||
timestamp_obj = {
|
timestamp_obj = {'timestamp': run_timestamp}
|
||||||
'_jc_meta': {
|
|
||||||
'run_timestamp': run_timestamp,
|
|
||||||
'run_iso': run_iso
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if isinstance(list_or_dict, dict):
|
if isinstance(list_or_dict, dict):
|
||||||
list_or_dict.update(timestamp_obj)
|
if '_jc_meta' in list_or_dict:
|
||||||
|
list_or_dict['_jc_meta'].update(timestamp_obj)
|
||||||
|
else:
|
||||||
|
list_or_dict['_jc_meta'] = timestamp_obj
|
||||||
|
|
||||||
elif isinstance(list_or_dict, list):
|
elif isinstance(list_or_dict, list):
|
||||||
for item in list_or_dict:
|
for item in list_or_dict:
|
||||||
item.update(timestamp_obj)
|
if '_jc_meta' in item:
|
||||||
|
item['_jc_meta'].update(timestamp_obj)
|
||||||
|
else:
|
||||||
|
item['_jc_meta'] = timestamp_obj
|
||||||
|
|
||||||
else:
|
else:
|
||||||
utils.error_message(['Parser returned an unsupported object type.'])
|
utils.error_message(['Parser returned an unsupported object type.'])
|
||||||
@ -545,9 +545,6 @@ def main():
|
|||||||
utils._safe_print(zsh_completion())
|
utils._safe_print(zsh_completion())
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
if timestamp:
|
|
||||||
run_dt_utc = datetime.now(timezone.utc)
|
|
||||||
|
|
||||||
# if magic syntax used, try to run the command and error if it's not found, etc.
|
# if magic syntax used, try to run the command and error if it's not found, etc.
|
||||||
magic_stdout, magic_stderr, magic_exit_code = None, None, 0
|
magic_stdout, magic_stderr, magic_exit_code = None, None, 0
|
||||||
if run_command:
|
if run_command:
|
||||||
@ -623,7 +620,12 @@ def main():
|
|||||||
raw=raw,
|
raw=raw,
|
||||||
quiet=quiet,
|
quiet=quiet,
|
||||||
ignore_exceptions=ignore_exceptions)
|
ignore_exceptions=ignore_exceptions)
|
||||||
|
|
||||||
for line in result:
|
for line in result:
|
||||||
|
if timestamp:
|
||||||
|
run_dt_utc = datetime.now(timezone.utc)
|
||||||
|
add_timestamp_to(line, run_dt_utc, magic_exit_code)
|
||||||
|
|
||||||
safe_print_out(line,
|
safe_print_out(line,
|
||||||
pretty=pretty,
|
pretty=pretty,
|
||||||
env_colors=jc_colors,
|
env_colors=jc_colors,
|
||||||
@ -650,6 +652,7 @@ def main():
|
|||||||
quiet=quiet)
|
quiet=quiet)
|
||||||
|
|
||||||
if timestamp:
|
if timestamp:
|
||||||
|
run_dt_utc = datetime.now(timezone.utc)
|
||||||
add_timestamp_to(result, run_dt_utc, magic_exit_code)
|
add_timestamp_to(result, run_dt_utc, magic_exit_code)
|
||||||
|
|
||||||
safe_print_out(result,
|
safe_print_out(result,
|
||||||
|
@ -10,7 +10,7 @@ long_options_map: Dict[str, List[str]] = {
|
|||||||
'--pretty': ['p', 'pretty print output'],
|
'--pretty': ['p', 'pretty print output'],
|
||||||
'--quiet': ['q', 'suppress warnings (double to ignore streaming errors)'],
|
'--quiet': ['q', 'suppress warnings (double to ignore streaming errors)'],
|
||||||
'--raw': ['r', 'raw output'],
|
'--raw': ['r', 'raw output'],
|
||||||
'--timestamp': ['t', 'add timestamp information to output'],
|
'--timestamp': ['t', 'add UTC Unix timestamp information to output'],
|
||||||
'--unbuffer': ['u', 'unbuffer output'],
|
'--unbuffer': ['u', 'unbuffer output'],
|
||||||
'--version': ['v', 'version info'],
|
'--version': ['v', 'version info'],
|
||||||
'--yaml-out': ['y', 'YAML output'],
|
'--yaml-out': ['y', 'YAML output'],
|
||||||
|
Reference in New Issue
Block a user