Build and Push to Registry / build (push) Successful in 8s
Co-authored-by: gabdevele <[email protected]>
66 lines
1.8 KiB
Python
66 lines
1.8 KiB
Python
from collections.abc import Mapping
|
|
|
|
|
|
def _get_service_port(mapping: str | int | Mapping[str, object]) -> int | None:
|
|
if isinstance(mapping, int):
|
|
return mapping
|
|
|
|
if isinstance(mapping, Mapping):
|
|
published = mapping.get("published")
|
|
if published is None:
|
|
return None
|
|
try:
|
|
return int(published)
|
|
except (TypeError, ValueError):
|
|
return None
|
|
|
|
parts = str(mapping).split(":")
|
|
match parts:
|
|
case [""]:
|
|
return None
|
|
case [port] | [port, _]:
|
|
try:
|
|
return int(port)
|
|
except ValueError:
|
|
return None
|
|
case ["localhost" | "127.0.0.1", _, _]:
|
|
return None
|
|
case [_, port, _]:
|
|
try:
|
|
return int(port)
|
|
except ValueError:
|
|
return None
|
|
case _:
|
|
return None
|
|
|
|
|
|
def _replace_port_in_mapping(
|
|
mapping: str | int | Mapping[str, object],
|
|
port_from: int,
|
|
port_to: int,
|
|
) -> str | dict[str, object]:
|
|
if isinstance(mapping, Mapping):
|
|
updated = dict(mapping)
|
|
if _get_service_port(updated) == port_from:
|
|
updated["published"] = str(port_to)
|
|
return updated
|
|
|
|
parts = str(mapping).split(":")
|
|
match parts:
|
|
case [_]:
|
|
return str(port_to)
|
|
case [_, container_port]:
|
|
return f"{port_to}:{container_port}"
|
|
case [ip, _, container_port]:
|
|
return f"{ip}:{port_to}:{container_port}"
|
|
case _:
|
|
return str(port_to)
|
|
|
|
|
|
def _get_port_index(mappings: list[str | int | Mapping[str, object]], port: int) -> int | None:
|
|
for i, mapping in enumerate(mappings):
|
|
if _get_service_port(mapping) == port:
|
|
return i
|
|
return None
|
|
|