feat: rework completion message for webclient remove process

Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
dw-0
2024-10-27 18:41:49 +01:00
parent 12127efa21
commit 1ca1e8ff6f
7 changed files with 128 additions and 69 deletions

View File

@@ -78,7 +78,10 @@ def add_config_section_at_top(section: str, instances: List[InstanceType]) -> No
Logger.print_ok("OK!")
def remove_config_section(section: str, instances: List[InstanceType]) -> None:
def remove_config_section(
section: str, instances: List[InstanceType]
) -> List[InstanceType]:
removed_from: List[instances] = []
for instance in instances:
cfg_file = instance.cfg_file
Logger.print_status(f"Remove section '[{section}]' from '{cfg_file}' ...")
@@ -96,4 +99,7 @@ def remove_config_section(section: str, instances: List[InstanceType]) -> None:
scp.remove_section(section)
scp.write_file(cfg_file)
removed_from.append(instance)
Logger.print_ok("OK!")
return removed_from

View File

@@ -13,7 +13,7 @@ from __future__ import annotations
import re
import shutil
from pathlib import Path
from subprocess import DEVNULL, PIPE, CalledProcessError, check_output, run
from subprocess import DEVNULL, PIPE, CalledProcessError, call, check_output, run
from typing import List
from zipfile import ZipFile
@@ -53,13 +53,28 @@ def create_symlink(source: Path, target: Path, sudo=False) -> None:
raise
def remove_with_sudo(file: Path) -> None:
try:
cmd = ["sudo", "rm", "-rf", file.as_posix()]
run(cmd, stderr=PIPE, check=True)
except CalledProcessError as e:
Logger.print_error(f"Failed to remove {file}: {e}")
raise
def remove_with_sudo(files: Path | List[Path]) -> bool:
_files = []
_removed = []
if isinstance(files, list):
_files = files
else:
_files.append(files)
for f in _files:
try:
cmd = ["sudo", "find", f.as_posix()]
if call(cmd, stderr=DEVNULL, stdout=DEVNULL) == 1:
Logger.print_info(f"File '{f}' does not exist. Skipped ...")
continue
cmd = ["sudo", "rm", "-rf", f.as_posix()]
run(cmd, stderr=PIPE, check=True)
Logger.print_ok(f"File '{f}' was successfully removed!")
_removed.append(f)
except CalledProcessError as e:
Logger.print_error(f"Error removing file '{f}': {e}")
return len(_removed) > 0
@deprecated(info="Use remove_with_sudo instead", replaced_by=remove_with_sudo)
@@ -84,16 +99,17 @@ def run_remove_routines(file: Path) -> bool:
elif file.is_file() or file.is_symlink():
file.unlink()
else:
raise OSError(f"File '{file}' is neither a file nor a directory!")
Logger.print_error(f"File '{file}' is neither a file nor a directory!")
return False
Logger.print_ok(f"File '{file}' was successfully removed!")
return True
except OSError as e:
Logger.print_error(f"Unable to delete '{file}':\n{e}")
try:
Logger.print_info("Trying to remove with sudo ...")
remove_with_sudo(file)
Logger.print_ok(f"File '{file}' was successfully removed!")
return True
if remove_with_sudo(file):
Logger.print_ok(f"File '{file}' was successfully removed!")
return True
except CalledProcessError as e:
Logger.print_error(f"Error deleting '{file}' with sudo:\n{e}")
Logger.print_error("Remove this directory manually!")