"""Common methods to exec helm commands"""

import sys
from typing import NoReturn

import yaml

from common import exec_command, exit_message, get_env_variable_required, is_path_exist


def add_nexus_repo() -> NoReturn:
    nexus_username = get_env_variable_required("NEXUS_USERNAME")
    nexus_password = get_env_variable_required("NEXUS_PASSWORD")

    exec_command(
        "helm repo add nexus-senior https://nexus.senior.com.br/repository/helm-hosted/ "
        f"--username {nexus_username} --password {nexus_password}",
        print_command=False,
    )


def get_version(chart_path: str) -> str or None:
    version = None

    if chart_path and is_path_exist(chart_path):
        with open(f"{chart_path}/Chart.yaml", "r", encoding="utf-8") as stream:
            try:
                chart = yaml.safe_load(stream)

                version = chart["version"]
            except yaml.YAMLError as exc:
                exit_message(exc)

    return version
