From 57460ab8b3cd3ebef13a3f30c26519a10b20ec80 Mon Sep 17 00:00:00 2001 From: Paul Hallett Date: Tue, 3 Oct 2023 14:33:12 +1300 Subject: [PATCH] Prevent regeneration without passing --regen --- clientele/cli.py | 25 +++++++++++++++---------- clientele/generator.py | 19 +++++++++++++++++-- tests/async_test_client/MANIFEST.md | 4 ++-- tests/test_client/MANIFEST.md | 4 ++-- 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/clientele/cli.py b/clientele/cli.py index 9e114ee..74155bb 100644 --- a/clientele/cli.py +++ b/clientele/cli.py @@ -62,13 +62,16 @@ def validate(url, file): @click.command() -@click.option("-u", "--url", help="URL to openapi schema (json file)", required=False) -@click.option("-f", "--file", help="Path to openapi schema (json file)", required=False) +@click.option("-u", "--url", help="URL to openapi schema (URL)", required=False) +@click.option( + "-f", "--file", help="Path to openapi schema (json or yaml file)", required=False +) @click.option( "-o", "--output", help="Directory for the generated client", required=True ) -@click.option("-a", "--asyncio", help="Use Async.IO", required=False) -def generate(url, file, output, asyncio): +@click.option("-a", "--asyncio", help="Generate async client", required=False) +@click.option("-r", "--regen", help="Regenerate client", required=False) +def generate(url, file, output, asyncio, regen): """ Generate a new client from an OpenAPI schema """ @@ -106,13 +109,15 @@ def generate(url, file, output, asyncio): f"[red]Clientele only supports OpenAPI version 3.0.0 and up, and you have {spec['openapi']}" ) return - Generator( - spec=spec, asyncio=asyncio, output_dir=output, url=url, file=file - ).generate() - console.log("\n[green]⚜️ Client generated! ⚜️ \n") - console.log( - "[yellow]REMEMBER: install `httpx` `pydantic`, and `respx` to use your new client" + generator = Generator( + spec=spec, asyncio=asyncio, regen=regen, output_dir=output, url=url, file=file ) + if generator.prevent_accidental_regens(): + generator.generate() + console.log("\n[green]⚜️ Client generated! ⚜️ \n") + console.log( + "[yellow]REMEMBER: install `httpx` `pydantic`, and `respx` to use your new client" + ) cli_group.add_command(generate) diff --git a/clientele/generator.py b/clientele/generator.py index 542a70f..332637f 100644 --- a/clientele/generator.py +++ b/clientele/generator.py @@ -5,6 +5,7 @@ from typing import Optional from openapi_core import Spec +from rich.console import Console from clientele.generators.clients import ClientsGenerator from clientele.generators.http import HTTPGenerator @@ -18,6 +19,8 @@ ) from clientele.writer import write_to_manifest, write_to_http +console = Console() + class Generator: """ @@ -26,6 +29,7 @@ class Generator: spec: Spec asyncio: bool + regen: bool schemas_generator: SchemasGenerator clients_generator: ClientsGenerator http_generator: HTTPGenerator @@ -38,6 +42,7 @@ def __init__( spec: Spec, output_dir: str, asyncio: bool, + regen: bool, url: Optional[str], file: Optional[str], ) -> None: @@ -54,6 +59,7 @@ def __init__( ) self.spec = spec self.asyncio = asyncio + self.regen = regen self.output_dir = output_dir self.file = file self.url = url @@ -80,15 +86,24 @@ def generate_manifest(self): # ruff: noqa write_to_manifest( f""" -Generated using this command: +Regnerate using this command: ```sh -clientele generate {f"-u {self.url}" if self.url else ""}{f"-f {self.file}" if self.file else ""} -o {self.output_dir} {"--asyncio t" if self.asyncio else ""} +clientele generate {f"-u {self.url}" if self.url else ""}{f"-f {self.file}" if self.file else ""} -o {self.output_dir} {"--asyncio t" if self.asyncio else ""} --regen t ``` """, self.output_dir, ) + def prevent_accidental_regens(self) -> bool: + if exists(self.output_dir): + if not self.regen: + console.log( + "[red]WARNING! If you want to regenerate, please pass --regen t" + ) + return False + return True + def generate(self) -> None: copy_tree(src=CLIENT_TEMPLATE_ROOT, dst=self.output_dir) if not exists(f"{self.output_dir}/config.py"): diff --git a/tests/async_test_client/MANIFEST.md b/tests/async_test_client/MANIFEST.md index f69842a..2ab58e8 100644 --- a/tests/async_test_client/MANIFEST.md +++ b/tests/async_test_client/MANIFEST.md @@ -11,8 +11,8 @@ API VERSION: 0.1.0 OPENAPI VERSION: 3.0.2 CLIENTELE VERSION: 0.7.0 -Generated using this command: +Regnerate using this command: ```sh -clientele generate -f example_openapi_specs/best.json -o tests/async_test_client/ --asyncio t +clientele generate -f example_openapi_specs/best.json -o tests/async_test_client/ --asyncio t --regen t ``` diff --git a/tests/test_client/MANIFEST.md b/tests/test_client/MANIFEST.md index 7c0e437..a4126ca 100644 --- a/tests/test_client/MANIFEST.md +++ b/tests/test_client/MANIFEST.md @@ -11,8 +11,8 @@ API VERSION: 0.1.0 OPENAPI VERSION: 3.0.2 CLIENTELE VERSION: 0.7.0 -Generated using this command: +Regnerate using this command: ```sh -clientele generate -f example_openapi_specs/best.json -o tests/test_client/ +clientele generate -f example_openapi_specs/best.json -o tests/test_client/ --regen t ```