diff --git a/CHANGELOG.org b/CHANGELOG.org index 8bf46c0..11574bd 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -14,3 +14,6 @@ Added *source update args* function. ** 0.5.0 <2023-03-03 Fri> Added *groups add* function. +** 0.5.2 <2023-03-04 Sat> + Added *sync recover* function. + Fixed label_prompt returning list. diff --git a/tui_rsync/cli/label_prompt.py b/tui_rsync/cli/label_prompt.py index 1f50469..a194aac 100644 --- a/tui_rsync/cli/label_prompt.py +++ b/tui_rsync/cli/label_prompt.py @@ -40,9 +40,9 @@ class LabelPrompt: @staticmethod def get_label_fzf() -> str: fzf = FzfPrompt() - return fzf.prompt(all_labels().iterator()) + return fzf.prompt(all_labels().iterator())[0] @staticmethod def get_label_except_fzf(labels = None) -> str: fzf = FzfPrompt() - return fzf.prompt(all_labels_except(labels).iterator()) + return fzf.prompt(all_labels_except(labels).iterator())[0] diff --git a/tui_rsync/cli/path_prompt.py b/tui_rsync/cli/path_prompt.py new file mode 100644 index 0000000..f33a311 --- /dev/null +++ b/tui_rsync/cli/path_prompt.py @@ -0,0 +1,40 @@ +################################################################################ +# Copyright (C) 2023 Kostiantyn Klochko # +# # +# This file is part of tui-rsync. # +# # +# tui-rsync is free software: you can redistribute it and/or modify it # +# under the terms of the GNU General Public License as published by the Free # +# Software Foundation, either version 3 of the License, or (at your option) # +# any later version. # +# # +# tui-rsync is distributed in the hope that it will be useful, but # +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY # +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # +# more details. # +# # +# You should have received a copy of the GNU General Public License along with # +# tui-rsync. If not, see . # +################################################################################ + +from sys import stderr +from rich.console import Console +from rich.prompt import Prompt +from pyfzf import FzfPrompt +import os +from models.models import Destination + +console = Console() +err_console = Console(stderr=True) + +class PathPrompt: + @staticmethod + def get_backup_fzf(label:str) -> str: + dests_count = len(Destination.get_all(label)) + if dests_count == 0: + err_console.print("[red b]No backups!!![/]") + return "" + if dests_count == 1: + return Destination.get_all(label).get() + fzf = FzfPrompt() + return fzf.prompt(Destination.get_all(label).iterator())[0] diff --git a/tui_rsync/cli/sync.py b/tui_rsync/cli/sync.py index b5d7f5b..c7323d2 100644 --- a/tui_rsync/cli/sync.py +++ b/tui_rsync/cli/sync.py @@ -25,6 +25,7 @@ import typer from models.models import Source, Destination, SyncCommand, Path from models.models import all_labels from cli.label_prompt import LabelPrompt +from cli.path_prompt import PathPrompt from cli.rsync import Rsync console = Console() @@ -55,3 +56,22 @@ def all(): """ for label in all_labels().iterator(): one(label.label) + +@sync.command() +def recover( + label: str = typer.Option( + None, "--label", "-l", + help="[b]The label[/] is a uniq identification of a [b]source[/].", + show_default=False + ), +): + """ + [green b]Sync[/] the [yellow]chosen backup[/] with its source. + """ + if label is None: + label = LabelPrompt.get_label_fzf() + src = Source.get(Source.label == label) + dest = PathPrompt.get_backup_fzf(label) + + rsync = Rsync(str(src.args)) + rsync.run_one(str(dest), str(src.source)) diff --git a/tui_rsync/models/models.py b/tui_rsync/models/models.py index c8e996f..5e138f1 100644 --- a/tui_rsync/models/models.py +++ b/tui_rsync/models/models.py @@ -93,6 +93,19 @@ class Destination(BaseModel): def __str__(self) -> str: return f"{self.path}" + @staticmethod + def get_all(label:str|None = None): + """ + Return all destiantions of the source. + """ + if label is None: + return [] + + src = Source.get_source(label) + if src is None: + return [] + return src.destinations + class Group(BaseModel): label = CharField(unique=True)