diff --git a/kiauh/components/klipper/__init__.py b/kiauh/components/klipper/__init__.py index 2e282e4..908ed4e 100644 --- a/kiauh/components/klipper/__init__.py +++ b/kiauh/components/klipper/__init__.py @@ -13,6 +13,8 @@ from core.backup_manager import BACKUP_ROOT_DIR MODULE_PATH = Path(__file__).resolve().parent +KLIPPER_REPO_URL = "https://github.com/Klipper3d/klipper.git" + # names KLIPPER_LOG_NAME = "klippy.log" KLIPPER_CFG_NAME = "printer.cfg" diff --git a/kiauh/components/moonraker/__init__.py b/kiauh/components/moonraker/__init__.py index 8924129..b48a894 100644 --- a/kiauh/components/moonraker/__init__.py +++ b/kiauh/components/moonraker/__init__.py @@ -13,6 +13,8 @@ from core.backup_manager import BACKUP_ROOT_DIR MODULE_PATH = Path(__file__).resolve().parent +MOONRAKER_REPO_URL = "https://github.com/Arksine/moonraker.git" + # names MOONRAKER_CFG_NAME = "moonraker.conf" MOONRAKER_LOG_NAME = "moonraker.log" diff --git a/kiauh/core/menus/settings_menu.py b/kiauh/core/menus/settings_menu.py index 278301b..1823214 100644 --- a/kiauh/core/menus/settings_menu.py +++ b/kiauh/core/menus/settings_menu.py @@ -12,9 +12,9 @@ import textwrap from pathlib import Path from typing import Literal, Tuple, Type -from components.klipper import KLIPPER_DIR +from components.klipper import KLIPPER_DIR, KLIPPER_REPO_URL from components.klipper.klipper_utils import get_klipper_status -from components.moonraker import MOONRAKER_DIR +from components.moonraker import MOONRAKER_DIR, MOONRAKER_REPO_URL from components.moonraker.moonraker_utils import get_moonraker_status from core.logger import DialogType, Logger from core.menus import Option @@ -124,32 +124,36 @@ class SettingsMenu(BaseMenu): self.moonraker_status.owner = url_parts[-2] self.moonraker_status.branch = self.settings.moonraker.branch - def _gather_input(self) -> Tuple[str, str]: - Logger.print_dialog( - DialogType.ATTENTION, - [ - "There is no input validation in place! Make sure your the input is " - "valid and has no typos or invalid characters! For the change to take " - "effect, the new repository will be cloned. A backup of the old " - "repository will be created.", + def _gather_input(self, repo_name: Literal["klipper", "moonraker"], repo_dir: Path) -> Tuple[str, str]: + warn_msg = [ + "There is only basic input validation in place! " + "Make sure your the input is valid and has no typos or invalid characters!"] + + if repo_dir.exists(): + warn_msg.extend([ + "For the change to take effect, the new repository will be cloned. " + "A backup of the old repository will be created.", "\n\n", "Make sure you don't have any ongoing prints running, as the services " - "will be restarted during this process! You will loose any ongoing print!", - ], - ) + "will be restarted during this process! You will loose any ongoing print!"]) + + Logger.print_dialog(DialogType.ATTENTION, warn_msg) + repo = get_string_input( "Enter new repository URL", - allow_special_chars=True, + regex="^[\w/.:-]+$", + default=KLIPPER_REPO_URL if repo_name == "klipper" else MOONRAKER_REPO_URL, ) branch = get_string_input( "Enter new branch name", - allow_special_chars=True, + regex="^.+$", + default="master" ) return repo, branch def _set_repo(self, repo_name: Literal["klipper", "moonraker"], repo_dir: Path) -> None: - repo_url, branch = self._gather_input() + repo_url, branch = self._gather_input(repo_name, repo_dir) display_name = repo_name.capitalize() Logger.print_dialog( DialogType.CUSTOM, diff --git a/kiauh/utils/input_utils.py b/kiauh/utils/input_utils.py index ef3f785..fecb616 100644 --- a/kiauh/utils/input_utils.py +++ b/kiauh/utils/input_utils.py @@ -86,6 +86,7 @@ def get_string_input( question: str, regex: str | None = None, exclude: List[str] | None = None, + allow_empty: bool = False, allow_special_chars: bool = False, default: str | None = None, ) -> str: @@ -94,6 +95,7 @@ def get_string_input( :param question: The question to display :param regex: An optional regex pattern to validate the input against :param exclude: List of strings which are not allowed + :param allow_empty: Whether to allow empty input :param allow_special_chars: Wheter to allow special characters in the input :param default: Optional default value :return: The validated string value @@ -104,12 +106,14 @@ def get_string_input( while True: _input = input(_question) - if _input.lower() in _exclude: - Logger.print_error("This value is already in use/reserved.") - elif default is not None and _input == "": + if default is not None and _input == "": return default + elif _input == "" and not allow_empty: + Logger.print_error("Input must not be empty!") elif _pattern is not None and _pattern.match(_input): return _input + elif _input.lower() in _exclude: + Logger.print_error("This value is already in use/reserved.") elif allow_special_chars: return _input elif not allow_special_chars and _input.isalnum():