From 7c8c71dff7436e5a138a4a428ba2f1591ea858fa Mon Sep 17 00:00:00 2001 From: "Joshua M. Boniface" Date: Thu, 17 Oct 2024 09:48:57 -0400 Subject: [PATCH] Improve handling of local connections in CLI 1. Ensure the local connection is actually always present if it exists, and stored in the store file. 2. Remove any invalid "local" store entries if present (i.e. pvcapid.yaml entries from legacy versions). 3. Order the connection lists such that "local" is always first. 4. Improve pretty list output format such that all fields are wider if needed --- client-cli/pvc/cli/formatters.py | 4 ++-- client-cli/pvc/cli/helpers.py | 12 ++++++++++-- client-cli/pvc/cli/parsers.py | 3 ++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/client-cli/pvc/cli/formatters.py b/client-cli/pvc/cli/formatters.py index 518bd583..951a22e1 100644 --- a/client-cli/pvc/cli/formatters.py +++ b/client-cli/pvc/cli/formatters.py @@ -905,7 +905,7 @@ def cli_connection_list_format_pretty(CLI_CONFIG, data): # Parse each connection and adjust field lengths for connection in data: for field, length in [(f, fields[f]["length"]) for f in fields]: - _length = len(str(connection[field])) + _length = len(str(connection[field])) + 1 if _length > length: length = len(str(connection[field])) + 1 @@ -1005,7 +1005,7 @@ def cli_connection_detail_format_pretty(CLI_CONFIG, data): # Parse each connection and adjust field lengths for connection in data: for field, length in [(f, fields[f]["length"]) for f in fields]: - _length = len(str(connection[field])) + _length = len(str(connection[field])) + 1 if _length > length: length = len(str(connection[field])) + 1 diff --git a/client-cli/pvc/cli/helpers.py b/client-cli/pvc/cli/helpers.py index 9c72f623..3a9226a4 100644 --- a/client-cli/pvc/cli/helpers.py +++ b/client-cli/pvc/cli/helpers.py @@ -167,9 +167,17 @@ def get_store(store_path): with open(store_file) as fh: try: store_data = jload(fh) - return store_data except Exception: - return dict() + store_data = dict() + + if path.exists(DEFAULT_STORE_DATA["cfgfile"]): + if store_data.get("local", None) != DEFAULT_STORE_DATA: + del store_data["local"] + if "local" not in store_data.keys(): + store_data["local"] = DEFAULT_STORE_DATA + update_store(store_path, store_data) + + return store_data def update_store(store_path, store_data): diff --git a/client-cli/pvc/cli/parsers.py b/client-cli/pvc/cli/parsers.py index 267f064a..4e75eda2 100644 --- a/client-cli/pvc/cli/parsers.py +++ b/client-cli/pvc/cli/parsers.py @@ -68,7 +68,8 @@ def cli_connection_list_parser(connections_config, show_keys_flag): } ) - return connections_data + # Return, ensuring local is always first + return sorted(connections_data, key=lambda x: (x.get("name") != "local")) def cli_connection_detail_parser(connections_config):