(Added) source update args function.

This commit is contained in:
2023-03-02 17:22:14 +02:00
parent b99f2a0ffa
commit c063b5a82c
5 changed files with 81 additions and 3 deletions
+3 -1
View File
@@ -24,9 +24,11 @@ import typer
from models.models import Source, Destination, SyncCommand, Path
from cli.label_prompt import LabelPrompt
from cli.source_update import source_update
console = Console()
source = typer.Typer()
source.add_typer(source_update, name="update", help="Update sources")
@source.command()
def add(
@@ -53,7 +55,7 @@ def add(
"""
[green b]Create[/] a [yellow]new source[/] with a [bold]uniq[/] label.
[b]The source[/] will be connected to [b]backup destinations[/].
[yellow i]Optionally, additional[/] arguments for rsync can be added.g
[yellow i]Optionally, additional[/] arguments for rsync can be added.
"""
if label is None:
label = LabelPrompt.ask_uuid()
+54
View File
@@ -0,0 +1,54 @@
################################################################################
# Copyright (C) 2023 Kostiantyn Klochko <kostya_klochko@ukr.net> #
# #
# This file is part of tui-rsync. #
# #
# tui-rsync is free software: you can redistribute it and/or modify it under #
# uthe 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 <https://www.gnu.org/licenses/>. #
################################################################################
from rich.console import Console
from rich.prompt import Prompt
from typing import List, Optional
import typer
from models.models import Source, Destination, SyncCommand, Path
from cli.label_prompt import LabelPrompt
console = Console()
source_update = typer.Typer()
@source_update.command()
def args(
label: str = typer.Option(
None, "--label", "-l",
help="[b]The label[/] is a uniq identification of a [b]source[/].",
show_default=False
),
args: str = typer.Option(
None, "--args", "-a",
help="[b yellow]rsync[/] [b]arguments[/].",
show_default=False
)
):
"""
[green b]Update[/] an [yellow]existing source args[/].
"""
if args is None:
args = console.input("What is the [yellow b]rsync args of source[/]? ")
if Source.is_exist(label):
src = Source.get_source(label)
src.update_args(args)
+20
View File
@@ -37,6 +37,11 @@ class Path(BaseModel):
class SyncCommand(BaseModel):
command = CharField()
@staticmethod
def get_sync_command(args):
sync_cmd, _ = SyncCommand.get_or_create(command=args)
return sync_cmd
def __str__(self) -> str:
return self.command
@@ -45,6 +50,16 @@ class Source(BaseModel):
source = ForeignKeyField(Path)
args = ForeignKeyField(SyncCommand)
@staticmethod
def is_exist(label) -> bool:
return Source.select().where(Source.label == label).exists()
@staticmethod
def get_source(label):
if not Source.is_exist(label):
return None
return Source.select().where(Source.label == label).get()
@staticmethod
def create_save(label:str, source:str, destinations:list[str], args:str):
src_path, _ = Path.get_or_create(path=source)
@@ -61,6 +76,11 @@ class Source(BaseModel):
src.save()
return src
def update_args(self, args):
args_obj = SyncCommand.get_sync_command(args)
self.args = args_obj
self.save()
def __str__(self) -> str:
return f"{self.label}"