From 03a2b35846da2903cddead358b265aed53738c4c Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Tue, 27 Sep 2022 11:11:16 -0700 Subject: [PATCH] Add metadata object to empty results list --- CHANGELOG | 2 ++ jc/cli.py | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 6cdc5284..88ae1a4a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -51,10 +51,12 @@ jc changelog `/proc//stat` `/proc//statm` `/proc//status` +- Magic syntax support for `/proc` files - Enhance `free` parser to support `-w` option integer conversions - Fix `ini` and `kv` parsers so they don't change keynames to lower case NOTE: This can be a breaking change in your scripts - Fix `id` command parser to allow usernames and groupnames with spaces +- Enhance metadata output to output metadata even when results are empty - Optimize tests - Optimize documentation build script diff --git a/jc/cli.py b/jc/cli.py index 9c35ee95..fb2ecbb2 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -475,6 +475,10 @@ def add_metadata_to(list_or_dict, does not already exist, it will be created with the metadata fields. If the _jc_meta field already exists, the metadata fields will be added to the existing object. + + In the case of an empty list (no data), a dictionary with a _jc_meta + object will be added to the list. This way you always get metadata, + even if there are no results. """ run_timestamp = runtime.timestamp() @@ -494,6 +498,9 @@ def add_metadata_to(list_or_dict, list_or_dict['_jc_meta'].update(meta_obj) elif isinstance(list_or_dict, list): + if not list_or_dict: + list_or_dict.append({}) + for item in list_or_dict: if '_jc_meta' not in item: item['_jc_meta'] = {}