refactor: handle ports as ints as they are coming as ints from the KiauhSettings

Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
dw-0
2024-04-28 19:42:40 +02:00
parent 5225e70e83
commit 1a29324e6a
3 changed files with 14 additions and 15 deletions

View File

@@ -55,7 +55,7 @@ def print_client_already_installed_dialog(name: str):
print_back_footer() print_back_footer()
def print_client_port_select_dialog(name: str, port: str, ports_in_use: List[str]): def print_client_port_select_dialog(name: str, port: int, ports_in_use: List[int]):
port = f"{COLOR_CYAN}{port}{RESET_FORMAT}" port = f"{COLOR_CYAN}{port}{RESET_FORMAT}"
line1 = f"Please select the port, {name} should be served on." line1 = f"Please select the port, {name} should be served on."
line2 = f"In case you need {name} to be served on a specific" line2 = f"In case you need {name} to be served on a specific"

View File

@@ -104,21 +104,19 @@ def install_client(client: BaseWebClient) -> None:
install_client_cfg = get_confirm(question, allow_go_back=False) install_client_cfg = get_confirm(question, allow_go_back=False)
settings = KiauhSettings() settings = KiauhSettings()
port = settings.get(client.name, "port") port: int = settings.get(client.name, "port")
ports_in_use = read_ports_from_nginx_configs() ports_in_use: List[int] = read_ports_from_nginx_configs()
# check if configured port is a valid number and not in use already # check if configured port is a valid number and not in use already
valid_port = is_valid_port(port, ports_in_use) valid_port = is_valid_port(port, ports_in_use)
while not valid_port: while not valid_port:
next_port = get_next_free_port(ports_in_use) next_port = get_next_free_port(ports_in_use)
print_client_port_select_dialog(client.display_name, next_port, ports_in_use) print_client_port_select_dialog(client.display_name, next_port, ports_in_use)
port = str( port = get_number_input(
get_number_input(
f"Configure {client.display_name} for port", f"Configure {client.display_name} for port",
min_count=int(next_port), min_count=int(next_port),
default=next_port, default=next_port,
) )
)
valid_port = is_valid_port(port, ports_in_use) valid_port = is_valid_port(port, ports_in_use)
check_install_dependencies(["nginx"]) check_install_dependencies(["nginx"])

View File

@@ -149,7 +149,7 @@ def create_nginx_cfg(name: str, port: int, root_dir: Path) -> None:
raise raise
def read_ports_from_nginx_configs() -> List[str]: def read_ports_from_nginx_configs() -> List[int]:
""" """
Helper function to iterate over all NGINX configs and read all ports defined for listen Helper function to iterate over all NGINX configs and read all ports defined for listen
:return: A sorted list of listen ports :return: A sorted list of listen ports
@@ -168,18 +168,19 @@ def read_ports_from_nginx_configs() -> List[str]:
if line.startswith("listen") and line.split()[-1] not in port_list: if line.startswith("listen") and line.split()[-1] not in port_list:
port_list.append(line.split()[-1]) port_list.append(line.split()[-1])
return sorted(port_list, key=lambda x: int(x)) ports_to_ints_list = [int(port) for port in port_list]
return sorted(ports_to_ints_list, key=lambda x: int(x))
def is_valid_port(port: str, ports_in_use: List[str]) -> bool: def is_valid_port(port: int, ports_in_use: List[int]) -> bool:
return port.isdigit() and port not in ports_in_use return port not in ports_in_use
def get_next_free_port(ports_in_use: List[str]) -> str: def get_next_free_port(ports_in_use: List[int]) -> int:
valid_ports = set(range(80, 7125)) valid_ports = set(range(80, 7125))
used_ports = set(map(int, ports_in_use)) used_ports = set(map(int, ports_in_use))
return str(min(valid_ports - used_ports)) return min(valid_ports - used_ports)
def add_config_section( def add_config_section(