feat: 'disable' renames to sync.json.disabled; new 'enable' rename-back
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.
This commit is contained in:
+52
-6
@@ -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
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user