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:
@@ -55,7 +55,7 @@ def print_client_already_installed_dialog(name: str):
|
||||
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}"
|
||||
line1 = f"Please select the port, {name} should be served on."
|
||||
line2 = f"In case you need {name} to be served on a specific"
|
||||
|
||||
@@ -104,20 +104,18 @@ def install_client(client: BaseWebClient) -> None:
|
||||
install_client_cfg = get_confirm(question, allow_go_back=False)
|
||||
|
||||
settings = KiauhSettings()
|
||||
port = settings.get(client.name, "port")
|
||||
ports_in_use = read_ports_from_nginx_configs()
|
||||
port: int = settings.get(client.name, "port")
|
||||
ports_in_use: List[int] = read_ports_from_nginx_configs()
|
||||
|
||||
# check if configured port is a valid number and not in use already
|
||||
valid_port = is_valid_port(port, ports_in_use)
|
||||
while not valid_port:
|
||||
next_port = get_next_free_port(ports_in_use)
|
||||
print_client_port_select_dialog(client.display_name, next_port, ports_in_use)
|
||||
port = str(
|
||||
get_number_input(
|
||||
f"Configure {client.display_name} for port",
|
||||
min_count=int(next_port),
|
||||
default=next_port,
|
||||
)
|
||||
port = get_number_input(
|
||||
f"Configure {client.display_name} for port",
|
||||
min_count=int(next_port),
|
||||
default=next_port,
|
||||
)
|
||||
valid_port = is_valid_port(port, ports_in_use)
|
||||
|
||||
|
||||
@@ -149,7 +149,7 @@ def create_nginx_cfg(name: str, port: int, root_dir: Path) -> None:
|
||||
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
|
||||
: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:
|
||||
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:
|
||||
return port.isdigit() and port not in ports_in_use
|
||||
def is_valid_port(port: int, ports_in_use: List[int]) -> bool:
|
||||
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))
|
||||
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(
|
||||
|
||||
Reference in New Issue
Block a user