Restore --save option to persist CLI setting --listen as the default

This commit is contained in:
2026-09-09 17:25:51 +00:00
parent ae1928241e
commit 0da04ac3e9
2 changed files with 39 additions and 0 deletions
+23
View File
@@ -24,6 +24,7 @@ EPILOG = """\
Examples: Examples:
paskia init example.com "Example Corporation" paskia init example.com "Example Corporation"
paskia migrate example.com paskia migrate example.com
paskia --listen 4402 --save
paskia paskia
""" """
@@ -185,6 +186,18 @@ def cmd_migrate(args: argparse.Namespace) -> None:
print(f"{action} {db_file_path()} (domains: {', '.join(rp_ids)})") 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: def cmd_serve(args: argparse.Namespace) -> None:
"""Open the combined database and serve all configured domains.""" """Open the combined database and serve all configured domains."""
db_path = db_file_path() 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.") 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) config = _load_stored_config(db_path)
listen = _split_multi(args.listen) or config.listen listen = _split_multi(args.listen) or config.listen
@@ -239,6 +256,12 @@ def main():
epilog=EPILOG, epilog=EPILOG,
) )
_add_listen_option(parser) _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( init_parser = argparse.ArgumentParser(
prog="paskia init", prog="paskia init",
+16
View File
@@ -185,6 +185,22 @@ def test_serve_listen_override_not_persisted(run_cli, tmp_path):
assert stored_config(tmp_path).listen == ["4402"] 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): def test_serve_suggests_migrate_when_legacy_present(run_cli, tmp_path):
write_legacy_db(tmp_path, legacy.LegacyConfig(rp_id="example.com")) write_legacy_db(tmp_path, legacy.LegacyConfig(rp_id="example.com"))
with pytest.raises(SystemExit, match="paskia migrate"): with pytest.raises(SystemExit, match="paskia migrate"):