-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathmain.py
87 lines (74 loc) · 2.56 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os
import subprocess
from typing import Optional
import pkg_resources
import typer
from questionary.form import form
from manage_fastapi.constants import Database, License, PackageManager, PythonVersion
from manage_fastapi.context import AppContext, ProjectContext
from manage_fastapi.generator import generate_app, generate_project
from manage_fastapi.helpers import binary_question, question
app = typer.Typer(
add_completion=False,
help="Managing FastAPI projects made easy!",
name="Manage FastAPI",
)
@app.command(help="Creates a FastAPI project.")
def startproject(
name: str,
interactive: bool = typer.Option(False, help="Run in interactive mode."),
database: Optional[Database] = typer.Option(None, case_sensitive=False),
docker: bool = typer.Option(False),
license_: Optional[License] = typer.Option(None, "--license", case_sensitive=False),
packaging: PackageManager = typer.Option(PackageManager.PIP),
pre_commit: bool = typer.Option(False, "--pre-commit"),
python: PythonVersion = typer.Option(PythonVersion.THREE_DOT_EIG),
):
if interactive:
result = form(
packaging=question(PackageManager),
python=question(PythonVersion),
license=question(License),
pre_commit=binary_question("pre commit"),
docker=binary_question("docker"),
database=question(Database),
).ask()
context = ProjectContext(name=name, **result)
else:
context = ProjectContext(
name=name,
packaging=packaging,
python=python,
license=license_,
pre_commit=pre_commit,
docker=docker,
database=database,
)
generate_project(context)
@app.command(help="Creates a FastAPI component.")
def startapp(name: str):
context = AppContext(name=name)
generate_app(context)
@app.command(help="Run a FastAPI application.")
def run(prod: bool = typer.Option(False)):
args = []
if not prod:
args.append("--reload")
app_file = os.getenv("FASTAPI_APP", "app.main")
subprocess.call(["uvicorn", f"{app_file}:app", *args])
def version_callback(value: bool):
if value:
version = pkg_resources.get_distribution("manage-fastapi").version
typer.echo(f"manage-fastapi, version {version}")
raise typer.Exit()
@app.callback()
def main(
version: bool = typer.Option(
None,
"--version",
callback=version_callback,
is_eager=True,
help="Show the Manage FastAPI version information.",
)
):
...