67 lines
1.7 KiB
Python
67 lines
1.7 KiB
Python
"""CLI app."""
|
|
|
|
import logging
|
|
import asyncio
|
|
import sys
|
|
from typing import cast
|
|
|
|
import click
|
|
from pydantic import ValidationError
|
|
from .settings import ServerSettings
|
|
from .ssh_server import start_sshecret_sshd
|
|
|
|
LOG = logging.getLogger()
|
|
|
|
handler = logging.StreamHandler()
|
|
formatter = logging.Formatter(
|
|
"%(created)f:%(levelname)s:%(name)s:%(module)s:%(message)s"
|
|
)
|
|
|
|
handler.setFormatter(formatter)
|
|
LOG.addHandler(handler)
|
|
LOG.setLevel(logging.INFO)
|
|
|
|
|
|
@click.group()
|
|
@click.option("--debug", is_flag=True)
|
|
@click.pass_context
|
|
def cli(ctx: click.Context, debug: bool) -> None:
|
|
"""Sshecret Admin."""
|
|
if debug:
|
|
LOG.setLevel(logging.DEBUG)
|
|
try:
|
|
settings = ServerSettings() # pyright: ignore[reportCallIssue]
|
|
except ValidationError as e:
|
|
raise click.ClickException(
|
|
"Error: One or more required environment options are missing."
|
|
) from e
|
|
ctx.obj = settings
|
|
|
|
|
|
@cli.command("run")
|
|
@click.option("--host")
|
|
@click.option("--port", type=click.INT)
|
|
@click.pass_context
|
|
def cli_run(ctx: click.Context, host: str | None, port: int | None) -> None:
|
|
"""Run the server."""
|
|
settings = cast(ServerSettings, ctx.obj)
|
|
if host:
|
|
settings.listen_address = host
|
|
if port:
|
|
settings.port = port
|
|
|
|
loop = asyncio.new_event_loop()
|
|
loop.run_until_complete(start_sshecret_sshd(settings))
|
|
title = click.style("Sshecret SSH Daemon", fg="red", bold=True)
|
|
click.echo(f"Starting {title}: {settings.listen_address}:{settings.port}")
|
|
try:
|
|
loop.run_forever()
|
|
except KeyboardInterrupt:
|
|
print("\nCtrl-C received. Exiting.")
|
|
sys.exit()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
"""Run CLI app."""
|
|
cli()
|