diff --git a/manage_fastapi/context.py b/manage_fastapi/context.py index 5b8dc20..9b89444 100644 --- a/manage_fastapi/context.py +++ b/manage_fastapi/context.py @@ -25,6 +25,8 @@ class ProjectContext(BaseModel): folder_name: str packaging: PackageManager + output_dir: str + username: Optional[str] = None email: Optional[EmailStr] = None @@ -42,12 +44,8 @@ class ProjectContext(BaseModel): @root_validator(pre=True) def validate_project(cls, values: dict): try: - values["username"] = subprocess.check_output( - ["git", "config", "--get", "user.name"] - ) - values["email"] = subprocess.check_output( - ["git", "config", "--get", "user.email"] - ) + values["username"] = subprocess.check_output(["git", "config", "--get", "user.name"]) + values["email"] = subprocess.check_output(["git", "config", "--get", "user.email"]) except subprocess.CalledProcessError: ... values["folder_name"] = values["name"].lower().replace(" ", "-").strip() diff --git a/manage_fastapi/generator.py b/manage_fastapi/generator.py index 21fb8d9..cfb38c1 100644 --- a/manage_fastapi/generator.py +++ b/manage_fastapi/generator.py @@ -14,10 +14,12 @@ def fill_template(template_name: str, context: ContextType): try: + output_dir = context.output_dir cookiecutter( os.path.join(TEMPLATES_DIR, template_name), - extra_context=context.dict(), + extra_context=context.dict(exclude={"output_dir"}), no_input=True, + output_dir=output_dir, ) except OutputDirExistsException: typer.echo(f"Folder '{context.folder_name}' already exists. 😞") diff --git a/manage_fastapi/main.py b/manage_fastapi/main.py index 24e00bc..5b87379 100644 --- a/manage_fastapi/main.py +++ b/manage_fastapi/main.py @@ -21,6 +21,7 @@ @app.command(help="Creates a FastAPI project.") def startproject( name: str, + output_dir: Optional[str] = typer.Argument(default="."), interactive: bool = typer.Option(False, help="Run in interactive mode."), database: Optional[Database] = typer.Option(None, case_sensitive=False), docker: bool = typer.Option(False), @@ -42,6 +43,7 @@ def startproject( else: context = ProjectContext( name=name, + output_dir=output_dir, packaging=packaging, python=python, license=license_, @@ -83,5 +85,4 @@ def main( is_eager=True, help="Show the Manage FastAPI version information.", ) -): - ... +): ... diff --git a/tests/test_startproject.py b/tests/test_startproject.py index 65348f4..ae7440d 100644 --- a/tests/test_startproject.py +++ b/tests/test_startproject.py @@ -26,3 +26,10 @@ def test_startproject_already_exists(tmp_path: Path): result = runner.invoke(app, ["startproject", "potato"]) assert result.output == ALREADY_EXISTS assert result.exit_code == 0 + + +def test_startproject_with_output_dir(tmp_path: Path): + with runner.isolated_filesystem(temp_dir=tmp_path): + result = runner.invoke(app, ["startproject", "potato", "myproject/potato"]) + assert result.output == CREATED_SUCCESSFULLY + assert result.exit_code == 0