aides-spec/aides_spec/commands/create.py

74 lines
2.2 KiB
Python
Raw Permalink Normal View History

2025-01-02 21:26:20 +00:00
import sys
from typing import Annotated, Optional
import typer
2025-01-05 08:37:11 +00:00
from aides_spec.utils.empty_template import create_from_empty_template
2025-01-02 21:26:20 +00:00
from aides_spec.utils.from_pkgbuild import (
2025-01-07 10:30:50 +00:00
PkgbuildDownloader,
2025-01-02 21:26:20 +00:00
create_from_pkgbuild,
)
app = typer.Typer()
2025-01-07 10:30:50 +00:00
def process_empty_template(output_file: str):
"""Handles creation from an empty template."""
typer.echo("Creating spec from an empty template...")
create_from_empty_template(output_file)
def process_from_aur(package_name: str, output_file: str):
"""Handles creation from an AUR package."""
typer.echo(
f"Downloading PKGBUILD for package '{package_name}' from AUR..."
)
try:
content = PkgbuildDownloader.download_and_extract(package_name)
create_from_pkgbuild(content, output_file)
except Exception as e:
typer.echo(
f"Error downloading PKGBUILD for '{package_name}': {e}", err=True
)
sys.exit(1)
def process_from_pkgbuild(file_path: str, output_file: str):
"""Handles creation from a local PKGBUILD file."""
typer.echo(f"Reading PKGBUILD from local file '{file_path}'...")
try:
with open(file_path, "rb") as f:
content = f.read()
create_from_pkgbuild(content, output_file)
except IOError as e:
typer.echo(f"Error reading file '{file_path}': {e}", err=True)
sys.exit(1)
@app.command(help="Create spec (empty, from PKGBUILD or AUR)")
2025-01-02 21:26:20 +00:00
def create(
2025-01-07 10:30:50 +00:00
from_aur: Annotated[
Optional[str], typer.Option(help="Package name to fetch from AUR")
] = None,
from_pkgbuild: Annotated[
Optional[str], typer.Option(help="Path to local PKGBUILD file")
] = None,
empty_template: Annotated[
Optional[bool], typer.Option(help="Create spec from an empty template")
] = None,
2025-01-02 21:26:20 +00:00
):
2025-01-07 10:30:50 +00:00
"""Main function to handle spec creation."""
2025-01-05 08:37:11 +00:00
output_file = "alr.sh"
if empty_template:
2025-01-07 10:30:50 +00:00
process_empty_template(output_file)
2025-01-05 08:37:11 +00:00
elif from_aur:
2025-01-07 10:30:50 +00:00
process_from_aur(from_aur, output_file)
2025-01-02 21:26:20 +00:00
elif from_pkgbuild:
2025-01-07 10:30:50 +00:00
process_from_pkgbuild(from_pkgbuild, output_file)
2025-01-02 21:26:20 +00:00
else:
2025-01-07 10:30:50 +00:00
typer.echo(
"No valid option provided. Use --help for usage details.", err=True
)
2025-01-02 21:26:20 +00:00
sys.exit(1)