Skip to content

Add 'routes' command #57

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions manage_fastapi/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from importlib import import_module
from typing import TypeVar

import questionary
Expand All @@ -17,3 +18,16 @@ def question(choices: EnumType) -> questionary.Question:

def binary_question(option: str) -> questionary.Question:
return questionary.confirm(f"Do you want {option}?", default=False)


def import_from_string(dotted_path: str) -> object:
try:
module_path, app_name = dotted_path.rsplit(":", 1)
except ValueError:
raise SystemExit(f'"{dotted_path}" is not formatted as "<module>:<app>".')

module = import_module(module_path)
try:
return getattr(module, app_name)
except AttributeError:
raise SystemExit(f'"{module_path}" does not contain an app named "{app_name}".')
31 changes: 29 additions & 2 deletions manage_fastapi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
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
from manage_fastapi.helpers import binary_question, import_from_string, question

app = typer.Typer(
add_completion=False,
Expand Down Expand Up @@ -67,8 +67,35 @@ def run(prod: bool = typer.Option(False)):
subprocess.call(["uvicorn", f"{app_file}:app", *args])


@app.command(help="Show list of endpoints.")
def routes(dotted_path: str):
try:
from rich.console import Console
from rich.table import Table
except ModuleNotFoundError: # pragma: no cover
typer.echo("rich is not installed. Install it with `pip install rich`.")
raise typer.Exit()

app = import_from_string(dotted_path)
headers = ("name", "path", "methods")
routes = sorted(
[
tuple(str(getattr(route, header)) for header in headers)
for route in app.routes
],
key=lambda x: x[1],
)
console = Console()
table = Table(show_header=True, header_style="bold magenta")
for column in headers:
table.add_column(column)
for route in routes:
table.add_row(*route)
console.print(table)


def version_callback(value: bool):
if value:
if value: # pragma: no cover
version = pkg_resources.get_distribution("manage-fastapi").version
typer.echo(f"manage-fastapi, version {version}")
raise typer.Exit()
Expand Down
Loading