mirror of
https://github.com/ycd/manage-fastapi.git
synced 2021-11-08 01:34:39 +03:00
✨ Add single line command support and version command
This commit is contained in:
@@ -2,9 +2,6 @@ from enum import Enum, EnumMeta
|
||||
|
||||
|
||||
class BaseMetadataEnum(EnumMeta):
|
||||
def __iter__(self):
|
||||
return (member[1].value for member in self.__members__.items())
|
||||
|
||||
def __contains__(self, other):
|
||||
try:
|
||||
self(other)
|
||||
@@ -31,11 +28,10 @@ class PythonVersion(BaseEnum):
|
||||
|
||||
class License(BaseEnum):
|
||||
MIT = "MIT"
|
||||
BSD = "BSD-3"
|
||||
GNU = "GNU GPL v3.0"
|
||||
APACHE = "Apache Software License 2.0"
|
||||
BSD = "BSD"
|
||||
GNU = "GNU"
|
||||
APACHE = "Apache"
|
||||
|
||||
|
||||
class Database(BaseEnum):
|
||||
POSTGRES = "Postgres"
|
||||
NONE = "None"
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import os
|
||||
import subprocess
|
||||
from typing import Optional
|
||||
|
||||
import pkg_resources
|
||||
import typer
|
||||
|
||||
from manage_fastapi.constants import Database, License, PackageManager, PythonVersion
|
||||
@@ -12,18 +14,17 @@ app = typer.Typer(help="Managing FastAPI projects made easy!", name="Manage Fast
|
||||
|
||||
|
||||
@app.command(help="Creates a FastAPI project.")
|
||||
def startproject(name: str, default: bool = typer.Option(False)):
|
||||
if default:
|
||||
context = Context(
|
||||
name=name,
|
||||
packaging=PackageManager.PIP,
|
||||
python=PythonVersion.THREE_DOT_EIG,
|
||||
license=License.MIT,
|
||||
pre_commit=True,
|
||||
docker=True,
|
||||
database=Database.NONE,
|
||||
)
|
||||
else:
|
||||
def startproject(
|
||||
name: str,
|
||||
interact: bool = typer.Option(False, "--potato", is_flag=True),
|
||||
database: Optional[Database] = typer.Option(None),
|
||||
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 interact:
|
||||
result = launch_cli(
|
||||
("packaging", bullet(PackageManager)),
|
||||
("python", bullet(PythonVersion)),
|
||||
@@ -33,6 +34,16 @@ def startproject(name: str, default: bool = typer.Option(False)):
|
||||
("database", bullet(Database)),
|
||||
)
|
||||
context = Context(name=name, **result)
|
||||
else:
|
||||
context = Context(
|
||||
name=name,
|
||||
packaging=packaging,
|
||||
python=python,
|
||||
license=license_,
|
||||
pre_commit=pre_commit,
|
||||
docker=docker,
|
||||
database=database,
|
||||
)
|
||||
generate_project(context)
|
||||
|
||||
|
||||
@@ -48,3 +59,23 @@ def run(prod: bool = typer.Option(False)):
|
||||
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.",
|
||||
)
|
||||
):
|
||||
...
|
||||
|
||||
@@ -19,13 +19,13 @@ class Context(BaseModel):
|
||||
python: PythonVersion
|
||||
fastapi: str = FASTAPI_VERSION
|
||||
|
||||
license: License
|
||||
license: Optional[License]
|
||||
year: int
|
||||
|
||||
pre_commit: bool
|
||||
docker: bool
|
||||
|
||||
database: Database
|
||||
database: Optional[Database]
|
||||
|
||||
@root_validator(pre=True)
|
||||
def git_info(cls, values: dict):
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import os
|
||||
|
||||
from manage_fastapi.constants import Database, PackageManager
|
||||
from manage_fastapi.constants import PackageManager
|
||||
|
||||
|
||||
def remove_paths(paths: list):
|
||||
@@ -41,18 +41,22 @@ def set_docker():
|
||||
|
||||
def set_database():
|
||||
database = "{{ cookiecutter.database }}"
|
||||
paths = []
|
||||
if database == "None":
|
||||
remove_paths(["app/database.py"])
|
||||
|
||||
if database == Database.NONE:
|
||||
paths = ["app/database.py"]
|
||||
|
||||
remove_paths(paths)
|
||||
def set_license():
|
||||
license_ = "{{ cookiecutter.license }}"
|
||||
if license_ == "None":
|
||||
remove_paths(["LICENSE"])
|
||||
|
||||
|
||||
def main():
|
||||
set_database()
|
||||
set_docker()
|
||||
set_license()
|
||||
set_packaging()
|
||||
set_pre_commit()
|
||||
set_docker()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user