From 0da04ac3e9d45fc7e2ac72c674cab703b8f063ca Mon Sep 17 00:00:00 2001 From: Leo Vasanko Date: Wed, 9 Sep 2026 17:23:42 +0000 Subject: [PATCH] Restore --save option to persist CLI setting --listen as the default --- paskia/__main__.py | 23 +++++++++++++++++++++++ tests/test_cli.py | 16 ++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/paskia/__main__.py b/paskia/__main__.py index 1cec473..733ba9c 100644 --- a/paskia/__main__.py +++ b/paskia/__main__.py @@ -24,6 +24,7 @@ EPILOG = """\ Examples: paskia init example.com "Example Corporation" paskia migrate example.com + paskia --listen 4402 --save paskia """ @@ -185,6 +186,18 @@ def cmd_migrate(args: argparse.Namespace) -> None: print(f"✅ {action} {db_file_path()} (domains: {', '.join(rp_ids)})") +def _save_listen(db_path: Path, listen: list[str] | None) -> None: + """Persist the listen endpoints to the stored configuration.""" + kanta = Kanta(str(db_path), DB()) + + async def _write() -> None: + async with kanta: + with kanta.transaction("serve:save_listen"): + kanta.data.config.listen = listen + + asyncio.run(_write()) + + def cmd_serve(args: argparse.Namespace) -> None: """Open the combined database and serve all configured domains.""" db_path = db_file_path() @@ -197,6 +210,10 @@ def cmd_serve(args: argparse.Namespace) -> None: ) raise SystemExit(f"Database {db_path} not found — run 'paskia init' first.") + if args.save and args.listen is not None: + # '--listen ""' clears the stored endpoints (back to the default) + _save_listen(db_path, _split_multi(args.listen) or None) + config = _load_stored_config(db_path) listen = _split_multi(args.listen) or config.listen @@ -239,6 +256,12 @@ def main(): epilog=EPILOG, ) _add_listen_option(parser) + parser.add_argument( + "--save", + action="store_true", + help="Save --listen to the database for future runs. " + "Use --listen \"\" to clear the stored endpoints.", + ) init_parser = argparse.ArgumentParser( prog="paskia init", diff --git a/tests/test_cli.py b/tests/test_cli.py index f73f489..190be6a 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -185,6 +185,22 @@ def test_serve_listen_override_not_persisted(run_cli, tmp_path): assert stored_config(tmp_path).listen == ["4402"] +def test_serve_listen_save_persists(run_cli, tmp_path): + run_cli("init", "--listen", "4402") + calls = run_cli("--listen", "4403", "--save") + + assert calls["listen"] == ["4403"] + assert stored_config(tmp_path).listen == ["4403"] + + +def test_serve_listen_save_clear(run_cli, tmp_path): + """--listen "" --save clears the stored endpoints (back to default).""" + run_cli("init", "--listen", "4402") + run_cli("--listen", "", "--save") + + assert stored_config(tmp_path).listen is None + + def test_serve_suggests_migrate_when_legacy_present(run_cli, tmp_path): write_legacy_db(tmp_path, legacy.LegacyConfig(rp_id="example.com")) with pytest.raises(SystemExit, match="paskia migrate"):