Files
claude-timemachine 9074121898
CI / test (3.10) (push) Successful in 7s
CI / test (3.11) (push) Successful in 7s
CI / test (3.12) (push) Successful in 6s
CI / build-pyz (push) Successful in 4s
CI / release (push) Has been skipped
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.
2026-06-05 09:58:56 +02:00

135 lines
4.2 KiB
Python

"""Interactive (``setup``) + non-interactive (``init``) opt-in flow.
Both end up doing the same thing — write a fresh sync.json + token to
``<pack>/.cloud-sync/``. Difference is just how the URL + token get
collected:
setup Qt login dialog for token; URL prompted at stdin if not
supplied via ``--url`` flag. Falls back to all-stdin when
headless / Qt missing.
init Both URL + token come from flags. No prompting. Suitable
for scripted CI runs or external launcher hooks (frazclient,
discord-bot ``/cloud register`` integration).
This is the only path that creates sync.json; pull/push never auto-mint.
That makes opt-in explicit — a player intentionally enables sync rather
than being silently signed up the first time they launch an instance.
"""
from __future__ import annotations
import sys
from pathlib import Path
from . import config as cfgmod
from .cli import Args
def run(
args: Args,
url: str | None,
token: str | None,
interactive: bool,
) -> int:
"""Drive the opt-in flow. Returns CLI exit code (0/1/2)."""
pack = args.pack_folder
cfg_path = cfgmod.config_path(pack)
if cfgmod.exists(pack):
existing = cfgmod.read(pack)
if existing is not None:
print(
f"instance-sync: this instance is already sync-enabled\n"
f" sync.json: {cfg_path}\n"
f" url: {existing.url}\n"
f" instance_id: {existing.instance_id}\n"
f" created_at: {existing.created_at}\n"
"Run with `disable` first if you want to re-enroll.",
file=sys.stderr,
)
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(
"instance-sync: --url is required for `init`",
file=sys.stderr,
)
return 2
url = _prompt_url()
if not url:
return 1
if token is None:
token = _prompt_token(headless=args.headless or not interactive)
if token is None:
print("instance-sync: setup cancelled (no token)", file=sys.stderr)
return 1
if ":" not in token:
print(
"instance-sync: token must be discord_id:password",
file=sys.stderr,
)
return 2
head, _, tail = token.partition(":")
if not head.strip().isdigit() or not tail.strip():
print(
"instance-sync: token malformed (discord_id must be numeric, "
"password must be non-empty)",
file=sys.stderr,
)
return 2
cfg = cfgmod.mint(url=url.strip())
cfgmod.write(pack, cfg)
args.token_file.parent.mkdir(parents=True, exist_ok=True)
args.token_file.write_text(token.strip() + "\n", encoding="utf-8")
args.token_file.chmod(0o600)
print(
f"instance-sync: enabled\n"
f" sync.json: {cfg_path}\n"
f" token: {args.token_file}\n"
f" url: {cfg.url}\n"
f" instance_id: {cfg.instance_id}"
)
return 0
def _prompt_url() -> str:
"""stdin prompt for the network endpoint."""
try:
v = input("Timemachine Network URL (e.g. https://cloud.tm.center): ").strip()
return v
except (EOFError, KeyboardInterrupt):
return ""
def _prompt_token(headless: bool) -> str | None:
"""Returns the discord_id:password token string, or None if user cancelled."""
if not headless:
try:
from .ui_qt import prompt_login_qt
except ImportError:
pass
else:
return prompt_login_qt()
# Headless fallback.
try:
return input("Paste token (discord_id:password): ").strip()
except (EOFError, KeyboardInterrupt):
return None