|
| 1 | +"""talonctl migrate — rewrap v1 templates to v2 and reconcile state to v4. |
| 2 | +
|
| 3 | +Dry-run by default. `--write` is the only flag that mutates disk. Idempotent. |
| 4 | +Orphans/unmanaged/conflicts are reported, never deleted or created. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import json |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +import click |
| 13 | +from rich.table import Table |
| 14 | + |
| 15 | +from talonctl.commands._common import console, get_state_file_path, state_options |
| 16 | +from talonctl.core.migrate import ( |
| 17 | + MigrationReport, |
| 18 | + build_template_index, |
| 19 | + reconcile_state, |
| 20 | + scan_templates, |
| 21 | +) |
| 22 | +from talonctl.core.state_manager import StateManager |
| 23 | +from talonctl.core.template_discovery import TemplateDiscovery |
| 24 | +from talonctl.project import find_project_root |
| 25 | + |
| 26 | + |
| 27 | +@click.command() |
| 28 | +@state_options |
| 29 | +@click.option("--write", is_flag=True, help="Apply changes (default: dry-run, writes nothing).") |
| 30 | +@click.option("--templates-only", is_flag=True, help="Only rewrap templates; skip state reconciliation.") |
| 31 | +@click.option("--state-only", is_flag=True, help="Only reconcile state; skip template rewrap.") |
| 32 | +@click.option("--format", "fmt", type=click.Choice(["text", "json"]), default="text") |
| 33 | +@click.option("--output", "-o", type=click.Path(), help="Write JSON report to file.") |
| 34 | +def migrate(state_file, write, templates_only, state_only, fmt, output): |
| 35 | + """Migrate v1 templates -> v2 and v3 state -> v4 (dry-run by default).""" |
| 36 | + if templates_only and state_only: |
| 37 | + raise click.UsageError("--templates-only and --state-only are mutually exclusive.") |
| 38 | + |
| 39 | + do_templates = not state_only |
| 40 | + do_state = not templates_only |
| 41 | + |
| 42 | + report = MigrationReport(dry_run=not write) |
| 43 | + |
| 44 | + if do_templates: |
| 45 | + resources_dir = find_project_root() / TemplateDiscovery.DEFAULT_RESOURCES_DIR |
| 46 | + report.rewraps = scan_templates(resources_dir) |
| 47 | + if write: |
| 48 | + for fr in report.rewraps: |
| 49 | + if fr.status == "rewrap" and fr.new_text is not None: |
| 50 | + fr.path.write_text(fr.new_text) |
| 51 | + |
| 52 | + if do_state: |
| 53 | + state_path = get_state_file_path(state_file) |
| 54 | + sm = StateManager(state_file_path=state_path) if state_path.exists() else None |
| 55 | + resources = sm.export_to_dict().get("resources", {}) if sm else {} |
| 56 | + index = build_template_index(TemplateDiscovery().discover_all()) |
| 57 | + report.state = reconcile_state(resources, index) |
| 58 | + if write and sm is not None and report.state.rekeyed: |
| 59 | + for rtype, old, new in report.state.rekeyed: |
| 60 | + rs = sm.get_resource(rtype, old) |
| 61 | + if rs is not None: |
| 62 | + sm.set_resource(rtype, new, rs) |
| 63 | + sm.delete_resource(rtype, old) |
| 64 | + sm.save() |
| 65 | + |
| 66 | + if fmt == "json" or output: |
| 67 | + data = json.dumps(report.to_dict(), indent=2) |
| 68 | + if output: |
| 69 | + Path(output).write_text(data) |
| 70 | + console.print(f"[green]Report written to {output}[/green]") |
| 71 | + else: |
| 72 | + console.print_json(data) |
| 73 | + else: |
| 74 | + _render_text(report) |
| 75 | + |
| 76 | + |
| 77 | +def _render_text(report: MigrationReport) -> None: |
| 78 | + mode = "DRY-RUN (no changes written — pass --write to apply)" if report.dry_run else "WRITE" |
| 79 | + console.print(f"[bold blue]talonctl migrate[/bold blue] [dim]{mode}[/dim]\n") |
| 80 | + |
| 81 | + if report.rewraps: |
| 82 | + t = Table(title="Templates") |
| 83 | + t.add_column("status") |
| 84 | + t.add_column("file") |
| 85 | + t.add_column("details") |
| 86 | + for fr in report.rewraps: |
| 87 | + detail = "" |
| 88 | + if fr.status == "rewrap": |
| 89 | + detail = f"{', '.join(fr.kinds)} · {fr.comments_dropped} comment(s) dropped" |
| 90 | + elif fr.status == "error": |
| 91 | + detail = "; ".join(fr.errors) |
| 92 | + t.add_row(fr.status, str(fr.path), detail) |
| 93 | + console.print(t) |
| 94 | + if any(fr.status == "rewrap" and fr.comments_dropped for fr in report.rewraps): |
| 95 | + console.print("[yellow]Comments dropped — originals preserved in git history.[/yellow]") |
| 96 | + |
| 97 | + s = report.state |
| 98 | + if s.rekeyed or s.orphans or s.unmanaged or s.conflicts: |
| 99 | + t = Table(title="State") |
| 100 | + t.add_column("category") |
| 101 | + t.add_column("detail") |
| 102 | + for rtype, old, new in s.rekeyed: |
| 103 | + t.add_row("rekey", f"{rtype}: {old} -> {new}") |
| 104 | + for rtype, key in s.orphans: |
| 105 | + t.add_row("orphan", f"{rtype}.{key} (state has no template)") |
| 106 | + for rtype, rid in s.unmanaged: |
| 107 | + t.add_row("unmanaged", f"{rtype}.{rid} (template has no state)") |
| 108 | + for rtype, key, target, reason in s.conflicts: |
| 109 | + t.add_row("conflict", f"{rtype}.{key} -> {target}: {reason}") |
| 110 | + console.print(t) |
| 111 | + if s.orphans or s.unmanaged or s.conflicts: |
| 112 | + console.print("[dim]Orphans/unmanaged/conflicts are reported only — never deleted or created.[/dim]") |
| 113 | + |
| 114 | + actionable_templates = any(fr.status in ("rewrap", "error") for fr in report.rewraps) |
| 115 | + actionable_state = bool(s.rekeyed or s.orphans or s.unmanaged or s.conflicts) |
| 116 | + if not actionable_templates and not actionable_state: |
| 117 | + console.print("[green]Nothing to migrate — templates are v2 and state is reconciled.[/green]") |
0 commit comments