import click

from pycage.helpers import get_interpreter, print_error


@click.group(help="commands to remove specified files")
def clean() -> None:
    pass


def delete_files(
    glob_pattern: str,
) -> None:
    try:
        pth_intp = get_interpreter()
    except RuntimeError:
        click.echo("Base interpreter path could not be found", err=True)
        return

    base_folder = pth_intp.parent
    assert base_folder.is_dir(), "base folder not a directory"
    files_to_delete = base_folder.glob(glob_pattern)
    for file in files_to_delete:
        file.unlink(missing_ok=True)


@click.command(help="removes all pre-compiled byte code files in the distribution")
def precompiled() -> None:
    try:
        delete_files(r"**/*.pyc")
    except Exception as err:
        print_error(err)


@click.command(help="removes all sensitive distribution data")
def dist_info() -> None:
    try:
        delete_files(r"**/*url.json")
    except Exception as err:
        print_error(err)


clean.add_command(precompiled)
clean.add_command(dist_info)
