Skip to content

Commit

Permalink
🎨 improve cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Wytamma committed Apr 26, 2024
1 parent c031d90 commit a4f0762
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions backend/beastiary/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@

app = typer.Typer()

def address_is_available(host: str, port: str) -> bool:
import socket

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex((host, int(port))) != 0

@app.command(context_settings={"help_option_names": ["-h", "--help"]})
def main(
Expand Down Expand Up @@ -80,12 +85,18 @@ def main(
if debug:
log_level = "debug"
if testing:
typer.Exit()
return typer.Exit()
if not address_is_available(host, port):
typer.secho(
f"Address {host}:{port} is already in use. Please choose another address.",
fg=typer.colors.RED,
)
return typer.Exit(1)
if share:
typer.echo("Creating public shareable link...")
with cloudflared(port=port) as cloudflared_url:
url_with_token = typer.style(
f"{cloudflared_url}", fg=typer.colors.GREEN, bold=False
f"{cloudflared_url}/login?token={token}", fg=typer.colors.GREEN, bold=False
)
typer.echo(f"\nBeastiary is now publicly accessible at: {url_with_token}")
uvicorn.run(api, host=host, port=port, log_level=log_level)
Expand Down

0 comments on commit a4f0762

Please sign in to comment.