From 71b1167d8ae981fa0033c7996a5918fd190cdb93 Mon Sep 17 00:00:00 2001 From: Filip Christiansen <22807962+filipchristiansen@users.noreply.github.com> Date: Fri, 17 Jan 2025 17:09:58 +0100 Subject: [PATCH] Refactor CLI to wrap async logic with a sync command function (#136) - Change main() to a synchronous Click command - Introduce _async_main() for async ingest logic - Use asyncio.run(...) to properly await the async function --- src/gitingest/cli.py | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/src/gitingest/cli.py b/src/gitingest/cli.py index ef7761b..a21a453 100644 --- a/src/gitingest/cli.py +++ b/src/gitingest/cli.py @@ -2,6 +2,8 @@ # pylint: disable=no-value-for-parameter +import asyncio + import click from config import MAX_FILE_SIZE @@ -9,12 +11,42 @@ @click.command() -@click.argument("source", type=str, required=True) +@click.argument("source", type=str, default=".") @click.option("--output", "-o", default=None, help="Output file path (default: .txt in current directory)") @click.option("--max-size", "-s", default=MAX_FILE_SIZE, help="Maximum file size to process in bytes") @click.option("--exclude-pattern", "-e", multiple=True, help="Patterns to exclude") @click.option("--include-pattern", "-i", multiple=True, help="Patterns to include") -async def main( +def main( + source: str, + output: str | None, + max_size: int, + exclude_pattern: tuple[str, ...], + include_pattern: tuple[str, ...], +): + """ + Main entry point for the CLI. This function is called when the CLI is run as a script. + + It calls the async main function to run the command. + + Parameters + ---------- + source : str + The source directory or repository to analyze. + output : str | None + The path where the output file will be written. If not specified, the output will be written + to a file named `.txt` in the current directory. + max_size : int + The maximum file size to process, in bytes. Files larger than this size will be ignored. + exclude_pattern : tuple[str, ...] + A tuple of patterns to exclude during the analysis. Files matching these patterns will be ignored. + include_pattern : tuple[str, ...] + A tuple of patterns to include during the analysis. Only files matching these patterns will be processed. + """ + # Main entry point for the CLI. This function is called when the CLI is run as a script. + asyncio.run(_async_main(source, output, max_size, exclude_pattern, include_pattern)) + + +async def _async_main( source: str, output: str | None, max_size: int, @@ -24,9 +56,8 @@ async def main( """ Analyze a directory or repository and create a text dump of its contents. - This command analyzes the contents of a specified source directory or repository, - applies custom include and exclude patterns, and generates a text summary of the analysis - which is then written to an output file. + This command analyzes the contents of a specified source directory or repository, applies custom include and + exclude patterns, and generates a text summary of the analysis which is then written to an output file. Parameters ----------