1
0
mirror of https://github.com/ycd/manage-fastapi.git synced 2021-11-08 01:34:39 +03:00
Files
manage-fastapi/manage_fastapi/generator.py
Marcelo Trylesinski 4ef522de7e Add startapp command
2020-12-07 14:42:54 -03:00

34 lines
944 B
Python

import os
from typing import TypeVar
import typer
from cookiecutter.exceptions import OutputDirExistsException
from cookiecutter.main import cookiecutter
from pydantic.main import BaseModel
from manage_fastapi.config import TEMPLATES_DIR
from manage_fastapi.context import AppContext, ProjectContext
ContextType = TypeVar("ContextType", bound=BaseModel)
def fill_template(template_name: str, context: ContextType):
try:
cookiecutter(
os.path.join(TEMPLATES_DIR, template_name),
extra_context=context.dict(),
no_input=True,
)
except OutputDirExistsException:
typer.echo(f"Folder '{context.folder_name}' already exists. 😞")
else:
typer.echo(f"FastAPI {template_name} created successfully! 🎉")
def generate_app(context: AppContext):
fill_template("app", context)
def generate_project(context: ProjectContext):
fill_template("project", context)