refactor: implement basic input validation for repo switch feature

Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
dw-0
2024-11-10 20:58:37 +01:00
parent b9c9feef3c
commit 6ff45aab41
4 changed files with 31 additions and 19 deletions

View File

@@ -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"

View File

@@ -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"

View File

@@ -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,

View File

@@ -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():