Complete sshd package with tests
This commit is contained in:
@ -2,8 +2,10 @@
|
||||
import logging
|
||||
import asyncio
|
||||
import sys
|
||||
from pydantic_settings import CliApp
|
||||
from typing import cast
|
||||
|
||||
import click
|
||||
from pydantic import ValidationError
|
||||
from .settings import ServerSettings
|
||||
from .ssh_server import start_server
|
||||
|
||||
@ -17,22 +19,38 @@ LOG.addHandler(handler)
|
||||
LOG.setLevel(logging.INFO)
|
||||
|
||||
|
||||
def cli(args: list[str] | None = None) -> None:
|
||||
"""Run CLI app."""
|
||||
try:
|
||||
settings = ServerSettings()
|
||||
except Exception:
|
||||
print("One or more settings could not be resolved.")
|
||||
CliApp.run(ServerSettings, ["--help"])
|
||||
sys.exit(1)
|
||||
|
||||
if settings.debug:
|
||||
@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_server(settings))
|
||||
|
||||
print(f"Starting SSH server: {settings.listen_address}:{settings.port}")
|
||||
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:
|
||||
@ -40,7 +58,6 @@ def cli(args: list[str] | None = None) -> None:
|
||||
sys.exit()
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
"""Run CLI app."""
|
||||
cli()
|
||||
|
||||
Reference in New Issue
Block a user