From aaca4b12153106e693a24b553db590784f560867 Mon Sep 17 00:00:00 2001 From: Jenia Peimer Date: Sun, 6 Oct 2024 21:29:05 +0300 Subject: [PATCH 1/7] Print resource yaml --- README.md | 9 +++++++-- app/main.py | 25 +++++++++++++++++++++---- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 2afffae..ab6c0fb 100644 --- a/README.md +++ b/README.md @@ -37,8 +37,13 @@ get <-n namespace_name:optional> 1: CONSOLE.print("[red]Too many params passed in, run 'help' for help\n") continue - resource_name = commands_list[0] resources_raw_data = get_cluster_resources_raw_data( @@ -146,13 +156,20 @@ def get_resources(resources_raw_data: List[Dict[str, Any]]) -> None: table = Table() table.add_column("NAMESPACE") table.add_column("NAME") - for raw_data in resources_raw_data: table.add_row(raw_data["namespace"], raw_data["name"]) - CONSOLE.print(table) +def print_resource_yaml(resources_raw_data: List[Dict[str, Any]]) -> None: + for raw_data in resources_raw_data: + # Read resource yaml file from path in raw_data["yaml_file"] + with open(raw_data["yaml_file"]) as fd: + resource_yaml_content = fd.read() + CONSOLE.print(resource_yaml_content) + CONSOLE.print("--------------------------------------------------") + + def get_logs() -> None: pass From ee390bb782eab119d9e94ddfd6742e3ea7ab12b0 Mon Sep 17 00:00:00 2001 From: Jenia Peimer Date: Thu, 10 Oct 2024 10:41:21 +0300 Subject: [PATCH 2/7] Wrap yaml read in try-except --- app/main.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 38d3784..28c79c8 100644 --- a/app/main.py +++ b/app/main.py @@ -164,10 +164,14 @@ def get_resources(resources_raw_data: List[Dict[str, Any]]) -> None: def print_resource_yaml(resources_raw_data: List[Dict[str, Any]]) -> None: for raw_data in resources_raw_data: # Read resource yaml file from path in raw_data["yaml_file"] - with open(raw_data["yaml_file"]) as fd: - resource_yaml_content = fd.read() + try: + with open(raw_data["yaml_file"]) as fd: + resource_yaml_content = fd.read() + except (FileNotFoundError, IOError) as e: + CONSOLE.print(f"[red]Error opening file {raw_data['yaml_file']}: {e}") + continue CONSOLE.print(resource_yaml_content) - CONSOLE.print("--------------------------------------------------") + CONSOLE.print("-" * os.get_terminal_size().columns) def get_logs() -> None: From 7765f503b9452510363ccfc4e0d2613c3014358d Mon Sep 17 00:00:00 2001 From: Jenia Peimer Date: Thu, 10 Oct 2024 11:46:48 +0300 Subject: [PATCH 3/7] Change yaml print handling --- app/main.py | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/app/main.py b/app/main.py index 28c79c8..b79094e 100644 --- a/app/main.py +++ b/app/main.py @@ -130,10 +130,14 @@ def main( # get pvc -n openshift-cnv yaml # get pvc -n openshift-cnv hpp yaml # get pvc yaml - yaml_str = "yaml" - if yaml_str in commands_list: - action_name = yaml_str - commands_list.remove(yaml_str) + print_yaml = False + yaml_flag = "-oyaml" + if yaml_flag in commands_list: + if action_name != "get": + CONSOLE.print(f"{yaml_flag} is only supported with 'get' action") + continue + print_yaml = True + commands_list.remove(yaml_flag) resource_name = "" if commands_list: @@ -151,14 +155,17 @@ def main( actions_dict[action_name](resources_raw_data) -def get_resources(resources_raw_data: List[Dict[str, Any]]) -> None: - # Print table of Namespace, Name - table = Table() - table.add_column("NAMESPACE") - table.add_column("NAME") - for raw_data in resources_raw_data: - table.add_row(raw_data["namespace"], raw_data["name"]) - CONSOLE.print(table) +def get_resources(resources_raw_data: List[Dict[str, Any]], print_yaml: bool = False, **kwargs: Dict[Any, Any]) -> None: + if print_yaml: + print_resource_yaml(resources_raw_data=resources_raw_data) + else: + # Print table of Namespace, Name + table = Table() + table.add_column("NAMESPACE") + table.add_column("NAME") + for raw_data in resources_raw_data: + table.add_row(raw_data["namespace"], raw_data["name"]) + CONSOLE.print(table) def print_resource_yaml(resources_raw_data: List[Dict[str, Any]]) -> None: @@ -174,15 +181,15 @@ def print_resource_yaml(resources_raw_data: List[Dict[str, Any]]) -> None: CONSOLE.print("-" * os.get_terminal_size().columns) -def get_logs() -> None: +def get_logs(**kwargs: Dict[Any, Any]) -> None: pass -def get_describe() -> None: +def get_describe(**kwargs: Dict[Any, Any]) -> None: pass -def print_help() -> None: +def print_help(**kwargs: Dict[Any, Any]) -> None: pass From c841dc1070eedf9787736a7202e5fdf30c049bd5 Mon Sep 17 00:00:00 2001 From: Jenia Peimer Date: Thu, 10 Oct 2024 11:56:25 +0300 Subject: [PATCH 4/7] Add 'get' constant --- app/main.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index b79094e..58c91d0 100644 --- a/app/main.py +++ b/app/main.py @@ -75,8 +75,10 @@ def main( ) sys.exit(1) + GET_ACTION = "get" + actions_dict: Dict[str, Any] = { - "get": get_resources, + GET_ACTION: get_resources, "yaml": print_resource_yaml, "logs": get_logs, "describe": get_describe, @@ -133,8 +135,8 @@ def main( print_yaml = False yaml_flag = "-oyaml" if yaml_flag in commands_list: - if action_name != "get": - CONSOLE.print(f"{yaml_flag} is only supported with 'get' action") + if action_name != GET_ACTION: + CONSOLE.print(f"'{yaml_flag}' is only supported with '{GET_ACTION}' action") continue print_yaml = True commands_list.remove(yaml_flag) From bd90b3feed0428089119a0fc03f7e3ef3d6ce2b8 Mon Sep 17 00:00:00 2001 From: Jenia Peimer Date: Thu, 10 Oct 2024 14:07:21 +0300 Subject: [PATCH 5/7] Print supported actions as a tuple --- app/main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 58c91d0..a2e5a86 100644 --- a/app/main.py +++ b/app/main.py @@ -104,7 +104,9 @@ def main( supported_actions = actions_dict.keys() if action_name not in supported_actions: - CONSOLE.print(f"Action '{action_name}' is not supported, please use a supported action {supported_actions}") + CONSOLE.print( + f"Action '{action_name}' is not supported, please use a supported action {tuple(supported_actions)}" + ) continue if action_name == "exit": From 2f9a3e272c0f7b7b70b2dd3dbceb75d30741874c Mon Sep 17 00:00:00 2001 From: Jenia Peimer Date: Thu, 10 Oct 2024 14:17:54 +0300 Subject: [PATCH 6/7] Fix review comments --- README.md | 8 +++++--- app/main.py | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ab6c0fb..1d3b211 100644 --- a/README.md +++ b/README.md @@ -40,10 +40,12 @@ get Node # To get all Nodes get PersistentVolumeClaim hpp # To get all PVCs when name starts from 'hpp' get pvc -n openshift-storage hpp # To get all PVCs in 'openshift-storage' namespace when name starts from 'hpp' ``` -- To print the resource YAML, add `yaml` to the `get` command, for example: + +- To print the resource YAML, add `-oyaml` to the `get` command, for example: + ```bash -get pvc hpp yaml # To print yamls of all PVCs when name starts from 'hpp' -get pvc -n openshift-storage hpp yaml # To print yamls of all PVCs in 'openshift-storage' namespace when name starts from 'hpp' +get pvc hpp -oyaml # To print yamls of all PVCs when name starts from 'hpp' +get pvc -n openshift-storage hpp -oyaml # To print yamls of all PVCs in 'openshift-storage' namespace when name starts from 'hpp' ``` - Help: diff --git a/app/main.py b/app/main.py index a2e5a86..a37ae48 100644 --- a/app/main.py +++ b/app/main.py @@ -79,7 +79,6 @@ def main( actions_dict: Dict[str, Any] = { GET_ACTION: get_resources, - "yaml": print_resource_yaml, "logs": get_logs, "describe": get_describe, "exit": None, @@ -156,7 +155,8 @@ def main( 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) + + actions_dict[action_name](resources_raw_data, print_yaml) def get_resources(resources_raw_data: List[Dict[str, Any]], print_yaml: bool = False, **kwargs: Dict[Any, Any]) -> None: From 5c6a0e33694006f31add2318435b636fff48331a Mon Sep 17 00:00:00 2001 From: Jenia Peimer Date: Thu, 10 Oct 2024 14:22:47 +0300 Subject: [PATCH 7/7] Fix yaml flag comment --- app/main.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index a37ae48..bfe75b3 100644 --- a/app/main.py +++ b/app/main.py @@ -129,10 +129,10 @@ def main( resource_kind = commands_list[0] commands_list.remove(resource_kind) - # if "yaml" passed - change the action_name to "yaml" - # get pvc -n openshift-cnv yaml - # get pvc -n openshift-cnv hpp yaml - # get pvc yaml + # if "-oyaml" passed, change print_yaml to True + # get pvc -n openshift-cnv -oyaml + # get pvc -n openshift-cnv hpp -oyaml + # get pvc -oyaml print_yaml = False yaml_flag = "-oyaml" if yaml_flag in commands_list: