Skip to content

Commit

Permalink
refactor: replace existing code with core_helpers utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
YisusChrist committed Sep 16, 2024
1 parent 18b283b commit 61ea43b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 50 deletions.
8 changes: 5 additions & 3 deletions pybacker/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
from shutil import copytree, rmtree
from tkinter import Tk, filedialog

from core_helpers.updates import check_updates
from rich import print
from rich.traceback import install

from .cli import check_updates, get_parsed_args
from .consts import DEBUG, EXIT_FAILURE, EXIT_SUCCESS
from .cli import get_parsed_args
from .consts import DEBUG, EXIT_FAILURE, EXIT_SUCCESS, GITHUB
from .consts import __version__ as VERSION


def check_dir_list(dirs: list) -> bool:
Expand Down Expand Up @@ -184,7 +186,7 @@ def cleanup_and_exit(exit_code: int, backup_dir: Path) -> None:

def main():
args = get_parsed_args()
check_updates()
check_updates(GITHUB, VERSION)

print("1. Creating backup directory...")
backup_dir = create_backup_dir(output=args.output, base_path=args.path)
Expand Down
55 changes: 8 additions & 47 deletions pybacker/cli.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"""Command-line interface for the project."""

from argparse import ArgumentParser, Namespace
from argparse import Namespace
from pathlib import Path

import requests
from rich import print
from rich_argparse_plus import RichHelpFormatterPlus
from core_helpers.cli import ArgparseColorThemes, setup_parser

from .consts import GITHUB, MAX_TIMEOUT, PACKAGE
from .consts import PACKAGE
from .consts import __desc__ as DESC
from .consts import __version__ as VERSION

Expand All @@ -19,14 +17,13 @@ def get_parsed_args() -> Namespace:
Returns:
The parsed arguments as a Namespace object.
"""
parser = ArgumentParser(
description=DESC, # Program description
formatter_class=RichHelpFormatterPlus, # Disable line wrapping
allow_abbrev=False, # Disable abbreviations
add_help=False, # Disable default help
parser, g_main = setup_parser(
PACKAGE,
DESC,
VERSION,
ArgparseColorThemes.DEFAULT,
)

g_main = parser.add_argument_group("Main Options")
# Source path argument
g_main.add_argument(
"-p",
Expand Down Expand Up @@ -61,40 +58,4 @@ def get_parsed_args() -> Namespace:
help="The output directory for the backup. Default is [i]backup_<timestamp>.[/]",
)

g_misc = parser.add_argument_group("Miscellaneous Options")
# Help
g_misc.add_argument(
"-h", "--help", action="help", help="Show this help message and exit."
)
# Version
g_misc.add_argument(
"-V",
"--version",
action="version",
help="Show version number and exit.",
version=f"[argparse.prog]{PACKAGE}[/] version [i]{VERSION}[/]",
)

return parser.parse_args()


def check_updates() -> None:
"""
Check if there is a newer version of the script available in the GitHub repository.
"""
project = GITHUB.split("https://github.com/")[1]
repo_url = f"https://api.github.com/repos/{project}/releases/latest"

try:
response = requests.get(repo_url, timeout=MAX_TIMEOUT)
response.raise_for_status()

latest_version = response.json()["tag_name"]
if latest_version != VERSION:
print(
f"\n[yellow]Newer version of the script available: {latest_version}.\n"
"Please consider updating your version.[/yellow]"
)

except requests.exceptions.RequestException as e:
print(f"[red]ERROR[/]: Could not check for updates: {e}")

0 comments on commit 61ea43b

Please sign in to comment.