Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print resource yaml #20

Merged
merged 8 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,15 @@ get <resource_kind:mandatory> <-n namespace_name:optional> <resource_name_starts

```bash
get Node # To get all Nodes
get PersistentVolumeClaim hpp # To get all PVCs that name starts form 'hpp'
get PersistentVolumeClaim -n openshift-storage hpp # To get all PVCs in 'openshift-storage' namespace that name starts form 'hpp'
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 `-oyaml` to the `get` command, for example:

```bash
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:
Expand Down
64 changes: 48 additions & 16 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ def main(
)
sys.exit(1)

GET_ACTION = "get"

actions_dict: Dict[str, Any] = {
"get": get_resources,
GET_ACTION: get_resources,
"logs": get_logs,
"describe": get_describe,
"exit": None,
Expand All @@ -86,6 +88,8 @@ def main(
# Get user prompt
while True:
user_command = Prompt.ask("Enter the command ")
if not user_command:
continue

# get PersistentVolumeClaim -n openshift-cnv
# get PersistentVolumeClaim -n openshift-cnv hpp
Expand All @@ -99,7 +103,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":
Expand All @@ -123,13 +129,24 @@ def main(
resource_kind = commands_list[0]
commands_list.remove(resource_kind)

resource_name = ""
# 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:
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)

resource_name = ""
if commands_list:
if len(commands_list) > 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(
Expand All @@ -138,30 +155,45 @@ 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)
jpeimer marked this conversation as resolved.
Show resolved Hide resolved

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"])
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)


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"]
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("-" * 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


Expand Down