Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent regeneration without passing --regen #17

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions clientele/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 17 additions & 2 deletions clientele/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,6 +19,8 @@
)
from clientele.writer import write_to_manifest, write_to_http

console = Console()


class Generator:
"""
Expand All @@ -26,6 +29,7 @@ class Generator:

spec: Spec
asyncio: bool
regen: bool
schemas_generator: SchemasGenerator
clients_generator: ClientsGenerator
http_generator: HTTPGenerator
Expand All @@ -38,6 +42,7 @@ def __init__(
spec: Spec,
output_dir: str,
asyncio: bool,
regen: bool,
url: Optional[str],
file: Optional[str],
) -> None:
Expand All @@ -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
Expand All @@ -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"):
Expand Down
4 changes: 2 additions & 2 deletions tests/async_test_client/MANIFEST.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
4 changes: 2 additions & 2 deletions tests/test_client/MANIFEST.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```