From 907412189871e47f2ec0c0b65388d9dce8d46e5c Mon Sep 17 00:00:00 2001 From: claude-timemachine Date: Fri, 5 Jun 2026 09:58:56 +0200 Subject: [PATCH] feat: 'disable' renames to sync.json.disabled; new 'enable' rename-back MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pause/resume sync without losing the instance_id. disable sync.json -> sync.json.disabled enable sync.json.disabled -> sync.json Re-enabling preserves the original ULID + url so the same restic repo continues. No new instance_id minted, no orphaned snapshot history. Tradeoff vs the previous 'disable = delete' semantics: the on-disk artifact survives, so a truly fresh start now needs 'disable && rm sync.json.disabled' before 'setup'. Implementation: config.disable(pack) os.rename(sync.json -> sync.json.disabled). False if no sync.json. config.enable(pack) os.rename(sync.json.disabled -> sync.json). Refuses if sync.json already exists (caller must disable first). config.delete(pack) now sweeps BOTH forms (escape hatch / tests). setup_flow gains a precheck: if sync.json.disabled is present, point the user at 'enable' instead of silently minting a fresh ULID over their existing instance. Opt-in gate (cfgmod.exists) is unchanged — only literal sync.json counts. The .disabled sibling is invisible to pull/push, so the silent-no-op behavior for paused instances Just Works. cli adds 'enable' subcommand alongside 'disable'. _run_disable prints 'already disabled' when called twice; _run_enable refuses to clobber an active config (exits 2 with the FileExistsError message). 7 new tests for disable/enable behavior + edge cases (idempotency, nothing-to-X, refuse-clobber). 80 tests total. --- README.md | 13 ++++++--- cloud_sync/cli.py | 55 ++++++++++++++++++++++++++++++++----- cloud_sync/config.py | 58 +++++++++++++++++++++++++++++++++++----- cloud_sync/setup_flow.py | 11 ++++++++ tests/test_cli.py | 6 +++++ tests/test_config.py | 57 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 185 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 0597f98..8eacc2c 100644 --- a/README.md +++ b/README.md @@ -48,13 +48,19 @@ python /path/to/cloud-sync.pyz init \ That mints a fresh `instance_id` (ULID), writes `.cloud-sync/sync.json` (the opt-in marker) and `.cloud-sync/token` (mode 600). Subsequent launches sync automatically via the global hook. -**Opting out:** +**Opting out / pausing:** ``` python /path/to/cloud-sync.pyz disable --pack-folder=/path/to/instance/minecraft ``` -Removes `sync.json` (cloud data untouched). Instance returns to no-op behavior. +Renames `sync.json` → `sync.json.disabled`. Instance returns to no-op behavior (the opt-in gate only sees a literal `sync.json`). Re-enable later with: + +``` +python /path/to/cloud-sync.pyz enable --pack-folder=/path/to/instance/minecraft +``` + +That rename-back preserves the original `instance_id` so snapshots continue against the same restic repo — no fresh ULID, no orphaned history. For a truly fresh start, `disable` then delete `sync.json.disabled` by hand before re-running `setup`. Player needs Python 3.10+ AND a Qt binding (`pip install PySide6`). Without Qt the pyz falls back to headless mode for status; the conflict + login dialogs are Qt-only — without them, conflict aborts the launch defensively and `setup` prompts for the token via stdin. @@ -70,7 +76,8 @@ instance-sync pull / push [--pack-folder=PATH] instance-sync setup [--url=URL] [--pack-folder=PATH] # interactive instance-sync init --url=URL [--token=ID:PASS] ... # scripted -instance-sync disable [--pack-folder=PATH] +instance-sync disable [--pack-folder=PATH] # sync.json → sync.json.disabled +instance-sync enable [--pack-folder=PATH] # sync.json.disabled → sync.json ``` `pull` and `push` don't take `--url` — it's loaded from `sync.json`. `--instance-label` overrides the UI display name (defaults to `$INST_NAME` from Prism, then `$INST_ID`, then the first 8 chars of `instance_id`). diff --git a/cloud_sync/cli.py b/cloud_sync/cli.py index b3222ad..cca6451 100644 --- a/cloud_sync/cli.py +++ b/cloud_sync/cli.py @@ -80,10 +80,22 @@ def build_parser() -> argparse.ArgumentParser: help="discord_id:password token (read from stdin if omitted)", ) - # --- disable: rm sync.json --- + # --- disable: rename sync.json → sync.json.disabled --- sp = sub.add_parser( "disable", - help="remove sync.json — re-noops the instance. Cloud data untouched.", + help="pause sync on this instance (renames sync.json -> sync.json.disabled). " + "Re-enable with `enable`. Cloud data untouched.", + ) + sp.add_argument( + "--pack-folder", default=".", type=Path, + help="Minecraft instance directory (default: cwd)", + ) + + # --- enable: rename sync.json.disabled → sync.json --- + sp = sub.add_parser( + "enable", + help="resume sync on this instance (renames sync.json.disabled -> sync.json). " + "Preserves the original instance_id + url so snapshots continue.", ) sp.add_argument( "--pack-folder", default=".", type=Path, @@ -159,6 +171,8 @@ def main(argv: list[str] | None = None) -> int: if cmd == "disable": return _run_disable(ns) + if cmd == "enable": + return _run_enable(ns) if cmd in ("setup", "init"): return _run_setup(ns, cmd) @@ -186,13 +200,42 @@ def main(argv: list[str] | None = None) -> int: def _run_disable(ns: argparse.Namespace) -> int: from . import config as cfgmod pack = Path(ns.pack_folder).absolute().resolve() - if cfgmod.delete(pack): - print(f"instance-sync: removed {cfgmod.config_path(pack)}") - else: - print(f"instance-sync: no sync.json at {cfgmod.config_path(pack)} (already disabled)") + if cfgmod.disable(pack): + print( + f"instance-sync: disabled — moved {cfgmod.config_path(pack).name} " + f"to {cfgmod.disabled_path(pack).name} in {cfgmod.config_path(pack).parent}. " + "Re-enable with `instance-sync enable`." + ) + return 0 + if cfgmod.disabled_path(pack).exists(): + print(f"instance-sync: already disabled ({cfgmod.disabled_path(pack)} present)") + return 0 + print(f"instance-sync: not enabled — no sync.json at {cfgmod.config_path(pack)}") return 0 +def _run_enable(ns: argparse.Namespace) -> int: + from . import config as cfgmod + pack = Path(ns.pack_folder).absolute().resolve() + try: + renamed = cfgmod.enable(pack) + except FileExistsError as e: + print(f"instance-sync: {e}", file=sys.stderr) + return 2 + if renamed: + print(f"instance-sync: enabled — restored {cfgmod.config_path(pack)}") + return 0 + if cfgmod.config_path(pack).exists(): + print(f"instance-sync: already enabled ({cfgmod.config_path(pack)} present)") + return 0 + print( + f"instance-sync: no sync.json.disabled at {cfgmod.disabled_path(pack)}. " + "Run `instance-sync setup` to opt in this instance fresh.", + file=sys.stderr, + ) + return 2 + + def _run_setup(ns: argparse.Namespace, cmd: str) -> int: """Wire `setup` (interactive) and `init` (flags-only) through the same flow module so both share the dialog/CLI logic.""" diff --git a/cloud_sync/config.py b/cloud_sync/config.py index d4e1b07..fe8d292 100644 --- a/cloud_sync/config.py +++ b/cloud_sync/config.py @@ -88,16 +88,62 @@ def write(pack_folder: Path, cfg: SyncConfig) -> None: p.chmod(0o644) -def delete(pack_folder: Path) -> bool: - """Remove sync.json. Used by the ``disable`` subcommand. Returns True if a - file was removed, False if nothing was there.""" - p = config_path(pack_folder) - if not p.exists(): +def disabled_path(pack_folder: Path) -> Path: + return pack_folder / ".cloud-sync" / "sync.json.disabled" + + +def disable(pack_folder: Path) -> bool: + """Rename sync.json → sync.json.disabled. Reversible via :func:`enable`. + + Preserves instance_id + url so re-enabling the instance later picks + back up the same restic repo (no new ULID minted, no orphaned + snapshot history). Returns True if a file was renamed; False if + sync.json wasn't there (already off — silent no-op). + + If sync.json.disabled already exists, it's overwritten by the move — + that case means the user disabled, then somehow re-ran setup which + minted a fresh sync.json; the older disabled file is stale. + """ + src = config_path(pack_folder) + if not src.exists(): return False - p.unlink() + dst = disabled_path(pack_folder) + src.replace(dst) return True +def enable(pack_folder: Path) -> bool: + """Rename sync.json.disabled → sync.json. Inverse of :func:`disable`. + + Returns True if a file was renamed; False if .disabled wasn't there + (nothing to re-enable). Refuses if sync.json already exists (would + clobber an active config); caller should ``disable`` first. + """ + src = disabled_path(pack_folder) + if not src.exists(): + return False + dst = config_path(pack_folder) + if dst.exists(): + raise FileExistsError( + f"refusing to enable: sync.json already exists at {dst}. " + "Run 'disable' first if you want to swap configs." + ) + src.replace(dst) + return True + + +def delete(pack_folder: Path) -> bool: + """Hard-delete sync.json (and any .disabled sibling). Used by tests + and as an escape hatch — the ``disable`` subcommand uses the rename + flow instead so re-enabling preserves the same instance_id.""" + removed = False + for p in (config_path(pack_folder), disabled_path(pack_folder)): + if p.exists(): + p.unlink() + removed = True + return removed + + # --------------------------------------------------------------------------- # minting # --------------------------------------------------------------------------- diff --git a/cloud_sync/setup_flow.py b/cloud_sync/setup_flow.py index 8b22c4c..a97a6fd 100644 --- a/cloud_sync/setup_flow.py +++ b/cloud_sync/setup_flow.py @@ -49,6 +49,17 @@ def run( ) return 2 + if cfgmod.disabled_path(pack).exists(): + print( + f"instance-sync: a disabled sync.json is present at " + f"{cfgmod.disabled_path(pack)}\n" + "Use `instance-sync enable` to resume the SAME instance_id " + "(snapshots continue), or `instance-sync disable` again to " + "discard it before a fresh setup.", + file=sys.stderr, + ) + return 2 + if url is None: if not interactive: print( diff --git a/tests/test_cli.py b/tests/test_cli.py index 6c7e3f0..6929eff 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -99,6 +99,12 @@ def test_disable_subcommand_parses() -> None: assert str(ns.pack_folder) == "/tmp/x" +def test_enable_subcommand_parses() -> None: + cmd, ns = parse(["enable", "--pack-folder=/tmp/x"]) + assert cmd == "enable" + assert str(ns.pack_folder) == "/tmp/x" + + def test_missing_subcommand_exits() -> None: with pytest.raises(SystemExit): parse([]) diff --git a/tests/test_config.py b/tests/test_config.py index 3a6175c..405d294 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -72,6 +72,63 @@ def test_delete_returns_false_when_missing(tmp_path: Path): assert cfgmod.delete(tmp_path) is False +def test_delete_also_clears_disabled_sibling(tmp_path: Path): + """Hard delete sweeps both active + disabled forms — used by tests + and as an escape hatch when the user wants a truly clean slate.""" + cfg = cfgmod.mint("https://x") + cfgmod.write(tmp_path, cfg) + cfgmod.disable(tmp_path) # now only .disabled exists + cfgmod.write(tmp_path, cfgmod.mint("https://y")) # also active + assert cfgmod.config_path(tmp_path).exists() + assert cfgmod.disabled_path(tmp_path).exists() + assert cfgmod.delete(tmp_path) is True + assert not cfgmod.config_path(tmp_path).exists() + assert not cfgmod.disabled_path(tmp_path).exists() + + +# ---- disable / enable round-trip ---- + + +def test_disable_renames_to_disabled(tmp_path: Path): + cfg = cfgmod.mint("https://x") + cfgmod.write(tmp_path, cfg) + assert cfgmod.disable(tmp_path) is True + assert not cfgmod.config_path(tmp_path).exists() + assert cfgmod.disabled_path(tmp_path).exists() + # opt-in gate now reads "off" + assert cfgmod.exists(tmp_path) is False + + +def test_disable_returns_false_when_nothing_to_disable(tmp_path: Path): + assert cfgmod.disable(tmp_path) is False + + +def test_enable_restores(tmp_path: Path): + cfg = cfgmod.mint("https://x") + cfgmod.write(tmp_path, cfg) + cfgmod.disable(tmp_path) + assert cfgmod.enable(tmp_path) is True + got = cfgmod.read(tmp_path) + assert got is not None + assert got.instance_id == cfg.instance_id # same id preserved! + assert got.url == cfg.url + assert not cfgmod.disabled_path(tmp_path).exists() + + +def test_enable_returns_false_when_nothing_disabled(tmp_path: Path): + assert cfgmod.enable(tmp_path) is False + + +def test_enable_refuses_to_clobber_active(tmp_path: Path): + """If both sync.json AND sync.json.disabled exist (manual mess), + enable refuses rather than silently overwriting the active one.""" + cfgmod.write(tmp_path, cfgmod.mint("https://x")) + cfgmod.disable(tmp_path) + cfgmod.write(tmp_path, cfgmod.mint("https://y")) # fresh active + with pytest.raises(FileExistsError): + cfgmod.enable(tmp_path) + + def test_corrupt_json_returns_none(tmp_path: Path): cfg_path = cfgmod.config_path(tmp_path) cfg_path.parent.mkdir(parents=True)