from src.cmd.daemon import run_daemon
from src.cmd.web import run_web
from src.cmd.webpack import run_webpack

from src.config import Configuration, parse_config
from src.internals.tracing.custom_psycopg_instrumentor import instrument_psycopg


def __try(f: callable):
    import sys
    try:
        f()
    except KeyboardInterrupt:
        sys.exit()


def main(args: list[str]):
    match args:
        case ["run"]:
            if Configuration().open_telemetry_endpoint:
                instrument_psycopg()
            if Configuration().development_mode:
                __try(run_web())
            else:
                __try(run_daemon())

        case ["web"]:
            if Configuration().open_telemetry_endpoint:
                instrument_psycopg()
            __try(run_web())

        case ["webpack"]:
            run_webpack()

        case ["daemon"]:
            if Configuration().open_telemetry_endpoint:
                instrument_psycopg()
            __try(run_daemon())

        case ["validate"]:
            __try(Configuration)

        case _:
            print(f"usage: python -m src [run|web|webpack|daemon]")
