Files
BackItUp/app/setup_wizard.py
T

73 lines
2.3 KiB
Python
Raw Normal View History

2026-07-29 17:36:22 +02:00
import getpass
from app.config_model import AppConfig, InitialSetupData
from app.security import hash_password
class SetupWizard:
@staticmethod
def _ask_non_empty(prompt: str) -> str:
while True:
value = input(prompt).strip()
if value:
return value
print("Eingabe darf nicht leer sein.")
@staticmethod
def _ask_port(prompt: str) -> int:
while True:
raw = input(prompt).strip()
try:
port = int(raw)
if 1 <= port <= 65535:
return port
except ValueError:
pass
print("Bitte eine gueltige Portnummer zwischen 1 und 65535 eingeben.")
@staticmethod
def _ask_debug(prompt: str) -> bool:
while True:
raw = input(prompt).strip().lower()
if raw in {"true", "false"}:
return raw == "true"
print("Bitte nur true oder false eingeben.")
@staticmethod
def _ask_password() -> str:
while True:
password = getpass.getpass("Adminpassword: ")
confirm = getpass.getpass("Adminpassword bestaetigen: ")
if not password:
print("Passwort darf nicht leer sein.")
continue
if password != confirm:
print("Passwoerter stimmen nicht ueberein.")
continue
return password
def collect_initial(self) -> InitialSetupData:
print("\n[SETUP] Initiale Konfiguration erforderlich.")
ip = self._ask_non_empty("IP-Adresse: ")
port = self._ask_port("Port: ")
debug = self._ask_debug("Debug (true/false): ")
adminuser = self._ask_non_empty("Adminuser: ")
password_hash = hash_password(self._ask_password())
return InitialSetupData(
config=AppConfig(
ip=ip,
port=port,
debug=debug,
),
admin_username=adminuser,
admin_password_hash=password_hash,
)
def collect_admin_user(self) -> tuple[str, str]:
print("\n[SETUP] Es wurde kein Benutzer gefunden. Bitte Admin anlegen.")
adminuser = self._ask_non_empty("Adminuser: ")
password_hash = hash_password(self._ask_password())
return adminuser, password_hash