Add the restore command.

dev
KKlochko 4 months ago
parent 87682a056f
commit b32e5d32b0

@ -0,0 +1,22 @@
from tui_rsync.core.components.backup.application.backup_commands import BackupSyncCommand, BackupSyncCommandDryRun
from tui_rsync.core.components.backup_plan.application.repository import BackupPlanRepositoryPort
from tui_rsync.core.shared_kernel.components.common import UUID
from tui_rsync.user_interface.cli.shared_kernel.components.prompts.applications.prompts import ChoosePromptPort
class BackupRestoreService:
def __init__(self, backup_plan_repository: BackupPlanRepositoryPort):
self.backup_plan_repository = backup_plan_repository
def sync_by_plan_id(self, uuid: UUID, choose_prompt: ChoosePromptPort, args: str = '', dry_run: bool = False):
backup_plan = self.backup_plan_repository.get_by_id(uuid)
destinations = (destination.path for destination in backup_plan.destinations)
destination = choose_prompt.choose(destinations)
if dry_run:
backup_command = BackupSyncCommandDryRun(destination, backup_plan.source.path, args)
backup_command.run()
else:
backup_command = BackupSyncCommand(destination, backup_plan.source.path, args)
backup_command.run()

@ -8,8 +8,11 @@ from tui_rsync.core.components.backup_plan.application.services.backup_plan_serv
from tui_rsync.core.ports.configuration import UserDataPathsPort
from tui_rsync.core.ports.orm import DatabaseManagerPort
from tui_rsync.core.shared_kernel.components.backup.application.service import BackupSyncService
from tui_rsync.core.shared_kernel.components.backup.application.service.backup_restore_service import \
BackupRestoreService
from tui_rsync.infrastructure.configuration import UserDataPaths
from tui_rsync.infrastructure.orm import SqliteDatabaseManager
from tui_rsync.user_interface.cli.shared_kernel.components.prompts.applications.prompts import ChoosePromptFzf
class Configuration(Module):
@ -47,3 +50,13 @@ class Configuration(Module):
@singleton
def provide_backup_sync_service(self, backup_plan_repository: BackupPlanRepositoryPort) -> BackupSyncService:
return BackupSyncService(backup_plan_repository)
@provider
@singleton
def provide_backup_restore_service(self, backup_plan_repository: BackupPlanRepositoryPort) -> BackupRestoreService:
return BackupRestoreService(backup_plan_repository)
@provider
@singleton
def provide_choose_prompt_fzf(self) -> ChoosePromptFzf:
return ChoosePromptFzf()

@ -22,8 +22,11 @@ from rich.console import Console
import typer
from tui_rsync.core.shared_kernel.components.backup.application.service import BackupSyncService
from tui_rsync.core.shared_kernel.components.backup.application.service.backup_restore_service import \
BackupRestoreService
from tui_rsync.core.shared_kernel.components.common import UUID
from tui_rsync.infrastructure.configuration import CurrentConfiguration
from tui_rsync.user_interface.cli.shared_kernel.components.prompts.applications.prompts import ChoosePromptFzf
console = Console()
sync = typer.Typer()
@ -42,7 +45,7 @@ def one(
)
):
"""
[green b]Sync[/] a [yellow]source[/] with the [yellow b]label[/] and its backups.
[green b]Sync[/] a [yellow]source[/] to [yellow]destination[/] for the [yellow b]backup plan id[/].
[yellow b]Skips[/] if not available.
"""
@ -54,3 +57,31 @@ def one(
# TODO add args
service.sync_by_plan_id(UUID(id), '-avuP', dry)
@sync.command()
def restore(
id: str = typer.Option(
None, "--id", "-i",
help="[b]The id[/] is an uniq identification of a [b]backup plan[/].",
show_default=False
),
dry: bool = typer.Option(
False, "-d", "--dry-run",
help="The command will [b]show[/] information about what will be changed.",
)
):
"""
[green b]Sync[/] a [yellow]destination[/] to the [yellow]source[/] for the [yellow b]backup plan id[/].
[yellow b]Skips[/] if not available.
"""
service: BackupRestoreService = CurrentConfiguration.get(BackupRestoreService)
choose_prompt: ChoosePromptFzf = CurrentConfiguration.get(ChoosePromptFzf)
if id is None:
console.print("[red b][ERROR][/] Backup plan does not exists!!!")
return
# TODO add args
service.sync_by_plan_id(UUID(id), choose_prompt, '-avuP', dry)

@ -0,0 +1,4 @@
from .choose_prompt_port import ChoosePromptPort
from .fzf_prompt import ChoosePromptFzf
__all__ = ['ChoosePromptPort', 'ChoosePromptFzf']

@ -0,0 +1,8 @@
from abc import ABC, abstractmethod
class ChoosePromptPort(ABC):
@staticmethod
@abstractmethod
def choose(iterator) -> str:
pass

@ -0,0 +1,9 @@
from .choose_prompt_port import ChoosePromptPort
from pyfzf import FzfPrompt
class ChoosePromptFzf(ChoosePromptPort):
@staticmethod
def choose(iterator) -> str:
fzf = FzfPrompt()
return fzf.prompt(iterator)[0]
Loading…
Cancel
Save