Complete backend
This commit is contained in:
@ -1,24 +1,31 @@
|
||||
"""CLI and main entry point."""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
from dotenv import load_dotenv
|
||||
import click
|
||||
from pydantic import BaseModel, FilePath
|
||||
|
||||
from .db import generate_api_token
|
||||
|
||||
DEFAULT_LISTEN = "127.0.0.1"
|
||||
DEFAULT_PORT = 8022
|
||||
|
||||
WORKDIR = Path(os.getcwd())
|
||||
|
||||
class BackendSettings(BaseModel):
|
||||
"""Backend Settings."""
|
||||
|
||||
db_file: FilePath
|
||||
regenerate_tokens: bool = False
|
||||
|
||||
load_dotenv()
|
||||
|
||||
@click.group()
|
||||
@click.option("--db-file", envvar="sshecret_db_file", type=click.Path(path_type=Path))
|
||||
@click.option("--regenerate-tokens", is_flag=True, default=False)
|
||||
@click.pass_context
|
||||
def cli(ctx: click.Context, db_file: Path, regenerate_tokens: bool) -> None:
|
||||
"""Sshecret database handler."""
|
||||
if not isinstance(ctx.obj, BackendSettings):
|
||||
ctx.obj = BackendSettings(db_file=db_file, regenerate_tokens=regenerate_tokens)
|
||||
@click.option("--database", help="Path to the sqlite database file.")
|
||||
def cli(database: str) -> None:
|
||||
"""CLI group."""
|
||||
if database:
|
||||
# Hopefully it's enough to set the environment variable as so.
|
||||
os.environ["SSHECRET_DB_FILE"] = str(Path(database).absolute())
|
||||
|
||||
|
||||
@cli.command("generate-token")
|
||||
def cli_generate_token() -> None:
|
||||
"""Generate a token."""
|
||||
token = generate_api_token()
|
||||
click.echo("Generated api token:")
|
||||
click.echo(token)
|
||||
|
||||
Reference in New Issue
Block a user