From c78671b88fbb7aaddd34309b2eeb954051020564 Mon Sep 17 00:00:00 2001 From: Jenia Peimer Date: Thu, 14 Nov 2024 12:02:31 +0200 Subject: [PATCH 1/2] Handle resource alias not found error --- app/exceptions.py | 14 ++++++++++++++ app/main.py | 24 +++++++++++++++--------- 2 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 app/exceptions.py diff --git a/app/exceptions.py b/app/exceptions.py new file mode 100644 index 0000000..4ebf658 --- /dev/null +++ b/app/exceptions.py @@ -0,0 +1,14 @@ +class MissingResourceKindAliasError(Exception): + def __init__(self, requested_kind: str) -> None: + self.requested_kind = requested_kind + + def __repr__(self) -> str: + return f"Resource kind alias '{self.requested_kind}' not found" + + +class FailToReadJSONFileError(Exception): + def __init__(self, file_name: str) -> None: + self.file_name = file_name + + def __repr__(self) -> str: + return f"Can't read file '{self.file_name}'" diff --git a/app/main.py b/app/main.py index f632f23..f730d69 100644 --- a/app/main.py +++ b/app/main.py @@ -11,6 +11,7 @@ from rich.table import Table from app.constants import ALIASES_FILE_PATH +from app.exceptions import MissingResourceKindAliasError, FailToReadJSONFileError CONSOLE = Console() @@ -149,14 +150,19 @@ def main( continue resource_name = commands_list[0] - resources_raw_data = get_cluster_resources_raw_data( - all_resources=all_resources, kind=resource_kind, name=resource_name, namespace=namespace_name - ) - if not resources_raw_data: - CONSOLE.print(f"No resources found for {resource_kind} {resource_name} {namespace_name}") - continue + try: + resources_raw_data = get_cluster_resources_raw_data( + all_resources=all_resources, kind=resource_kind, name=resource_name, namespace=namespace_name + ) + if not resources_raw_data: + CONSOLE.print(f"No resources found for {resource_kind} {resource_name} {namespace_name}") + continue + actions_dict[action_name](resources_raw_data, print_yaml) - actions_dict[action_name](resources_raw_data, print_yaml) + except FailToReadJSONFileError: + sys.exit(1) + except MissingResourceKindAliasError: + continue def get_resources(resources_raw_data: List[Dict[str, Any]], print_yaml: bool = False, **kwargs: Dict[Any, Any]) -> None: @@ -252,7 +258,7 @@ def get_resource_kind_by_alias(requested_kind: str) -> str: f"Error details: {exp}\n" f"{how_to_update_aliases_message}" ) - sys.exit(1) + raise FailToReadJSONFileError(file_name=ALIASES_FILE_PATH) for kind, aliases in resources_aliases.items(): if kind == kind_lower or kind_lower in aliases: @@ -263,7 +269,7 @@ def get_resource_kind_by_alias(requested_kind: str) -> str: f"please make sure it was typed correctly and alias file is up to date\n" f"{how_to_update_aliases_message}" ) - sys.exit(2) + raise MissingResourceKindAliasError(requested_kind=requested_kind) if __name__ == "__main__": From 54271fda31139de0f17527205038d5650f18132c Mon Sep 17 00:00:00 2001 From: Jenia Peimer Date: Thu, 14 Nov 2024 12:38:17 +0200 Subject: [PATCH 2/2] Move read alias file to utils --- app/constants.py | 9 +++++++++ app/main.py | 34 ++++++++-------------------------- app/resource_aliases.py | 12 +++--------- app/utils.py | 19 +++++++++++++++++++ 4 files changed, 39 insertions(+), 35 deletions(-) create mode 100644 app/utils.py diff --git a/app/constants.py b/app/constants.py index 9734060..2b8942a 100644 --- a/app/constants.py +++ b/app/constants.py @@ -1 +1,10 @@ +from rich.console import Console + +CONSOLE = Console() + ALIASES_FILE_PATH = "app/manifests/aliases.json" + +HOW_TO_UPDATE_ALIASES_MESSAGE = ( + "How to update the resource aliases file: " + "https://github.com/RedHatQE/must-gather-explorer?tab=readme-ov-file#update-cluster-resources-aliases\n" +) diff --git a/app/main.py b/app/main.py index f730d69..897f9be 100644 --- a/app/main.py +++ b/app/main.py @@ -1,4 +1,3 @@ -import json import os import sys from functools import lru_cache @@ -6,16 +5,12 @@ import click import yaml -from rich.console import Console from rich.prompt import Prompt from rich.table import Table -from app.constants import ALIASES_FILE_PATH +from app.constants import HOW_TO_UPDATE_ALIASES_MESSAGE, CONSOLE from app.exceptions import MissingResourceKindAliasError, FailToReadJSONFileError - -CONSOLE = Console() - -NAMESPACE_FLAG = "-n" +from app.utils import read_aliases_file @click.command("must-gather-explorer") @@ -116,11 +111,12 @@ def main( print_help() continue + namespace_flag = "-n" namespace_name = "" - if NAMESPACE_FLAG in commands_list: - namespace_index = commands_list.index(NAMESPACE_FLAG) + if namespace_flag in commands_list: + namespace_index = commands_list.index(namespace_flag) namespace_name = commands_list[namespace_index + 1] - commands_list.remove(NAMESPACE_FLAG) + commands_list.remove(namespace_flag) commands_list.remove(namespace_name) if not commands_list: @@ -244,21 +240,7 @@ def get_cluster_resources_raw_data( def get_resource_kind_by_alias(requested_kind: str) -> str: kind_lower = requested_kind.lower() - how_to_update_aliases_message = ( - "How to update the resource aliases file: " - "https://github.com/RedHatQE/must-gather-explorer?tab=readme-ov-file#update-cluster-resources-aliases\n" - ) - - try: - with open(ALIASES_FILE_PATH) as aliases_file: - resources_aliases = json.load(aliases_file) - except Exception as exp: - CONSOLE.print( - f"[bold red]Error:[/bold red] Can't read the aliases_file\n" - f"Error details: {exp}\n" - f"{how_to_update_aliases_message}" - ) - raise FailToReadJSONFileError(file_name=ALIASES_FILE_PATH) + resources_aliases = read_aliases_file() for kind, aliases in resources_aliases.items(): if kind == kind_lower or kind_lower in aliases: @@ -267,7 +249,7 @@ def get_resource_kind_by_alias(requested_kind: str) -> str: CONSOLE.print( f"[bold red]Error:[/bold red] Not valid resource kind '{kind_lower}', " f"please make sure it was typed correctly and alias file is up to date\n" - f"{how_to_update_aliases_message}" + f"{HOW_TO_UPDATE_ALIASES_MESSAGE}" ) raise MissingResourceKindAliasError(requested_kind=requested_kind) diff --git a/app/resource_aliases.py b/app/resource_aliases.py index 30d52cb..a2b18a3 100644 --- a/app/resource_aliases.py +++ b/app/resource_aliases.py @@ -5,11 +5,9 @@ import click from pyhelper_utils.shell import run_command -from rich.console import Console -from app.constants import ALIASES_FILE_PATH - -CONSOLE = Console() +from app.constants import ALIASES_FILE_PATH, CONSOLE +from app.utils import read_aliases_file @click.command("update-resources-aliases") @@ -22,11 +20,7 @@ def fill_api_resources_aliases() -> None: CONSOLE.print(f"Error message: {err}") sys.exit(1) - try: - with open(ALIASES_FILE_PATH) as aliases_file: - resources_aliases = json.load(aliases_file) - except Exception: - resources_aliases = {} + resources_aliases = read_aliases_file(raise_on_error=False) cli_resource_alias: Dict[str, List[str]] = {} diff --git a/app/utils.py b/app/utils.py new file mode 100644 index 0000000..fcbc733 --- /dev/null +++ b/app/utils.py @@ -0,0 +1,19 @@ +import json + +from app.constants import ALIASES_FILE_PATH, HOW_TO_UPDATE_ALIASES_MESSAGE, CONSOLE +from app.exceptions import FailToReadJSONFileError + + +def read_aliases_file(raise_on_error: bool = True) -> dict[str, list[str]]: + try: + with open(ALIASES_FILE_PATH) as aliases_file: + return json.load(aliases_file) + except (FileNotFoundError, json.JSONDecodeError) as exp: + if raise_on_error: + CONSOLE.print( + f"[bold red]Error:[/bold red] Can't read the aliases_file\n" + f"Error details: {exp}\n" + f"{HOW_TO_UPDATE_ALIASES_MESSAGE}" + ) + raise FailToReadJSONFileError(file_name=ALIASES_FILE_PATH) + return {}