|
| 1 | +"""Setup wizard — first-run configuration for studyctl. |
| 2 | +
|
| 3 | +Guides users through the essential configuration questions one at a time. |
| 4 | +Designed to be AuDHD-friendly: clear steps, no overwhelming walls of text, |
| 5 | +sensible defaults so pressing Enter always works. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import contextlib |
| 11 | +import subprocess |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +import click |
| 15 | +import yaml |
| 16 | + |
| 17 | +from studyctl.cli._shared import console |
| 18 | +from studyctl.settings import CONFIG_DIR |
| 19 | + |
| 20 | + |
| 21 | +def _validate_path(value: str) -> Path: |
| 22 | + """Expand user and return a Path; raise UsageError if obviously invalid.""" |
| 23 | + p = Path(value).expanduser() |
| 24 | + return p |
| 25 | + |
| 26 | + |
| 27 | +@click.command(name="setup") |
| 28 | +def setup() -> None: |
| 29 | + """First-run setup wizard — configure studyctl in under 2 minutes.""" |
| 30 | + |
| 31 | + # ------------------------------------------------------------------ |
| 32 | + # Banner |
| 33 | + # ------------------------------------------------------------------ |
| 34 | + console.print() |
| 35 | + console.print("[bold cyan]studyctl setup[/bold cyan]") |
| 36 | + console.print( |
| 37 | + "studyctl is your AuDHD study pipeline: it syncs Obsidian notes to " |
| 38 | + "NotebookLM, tracks spaced-repetition reviews, and schedules focused " |
| 39 | + "study sessions.\n" |
| 40 | + ) |
| 41 | + console.print("[dim]Press Enter to accept the default shown in brackets.[/dim]\n") |
| 42 | + |
| 43 | + config: dict = {} |
| 44 | + |
| 45 | + # ------------------------------------------------------------------ |
| 46 | + # Step 1 — Study materials directory |
| 47 | + # ------------------------------------------------------------------ |
| 48 | + console.print("[bold]Step 1 of 5[/bold] Where do you store course materials (PDFs, slides)?") |
| 49 | + materials_raw = click.prompt( |
| 50 | + "Study materials path", |
| 51 | + default="~/study-materials", |
| 52 | + ) |
| 53 | + materials_path = _validate_path(materials_raw) |
| 54 | + config["content"] = {"base_path": str(materials_raw)} |
| 55 | + console.print(f" [dim]Will use: {materials_path}[/dim]\n") |
| 56 | + |
| 57 | + # ------------------------------------------------------------------ |
| 58 | + # Step 2 — AI coding assistant / MCP registration |
| 59 | + # ------------------------------------------------------------------ |
| 60 | + console.print("[bold]Step 2 of 5[/bold] Do you use an AI coding assistant?") |
| 61 | + console.print(" [dim](Claude Code, Kiro, Gemini CLI, etc.)[/dim]") |
| 62 | + has_ai = click.confirm(" Use an AI assistant?", default=True) |
| 63 | + if has_ai: |
| 64 | + assistant = click.prompt( |
| 65 | + " Which one", |
| 66 | + default="claude-code", |
| 67 | + type=click.Choice( |
| 68 | + ["claude-code", "kiro", "gemini-cli", "other"], |
| 69 | + case_sensitive=False, |
| 70 | + ), |
| 71 | + show_choices=True, |
| 72 | + ) |
| 73 | + config["ai_assistant"] = assistant |
| 74 | + console.print( |
| 75 | + f" [dim]Noted. Run 'studyctl config init' to install agent definitions " |
| 76 | + f"for {assistant}.[/dim]\n" |
| 77 | + ) |
| 78 | + else: |
| 79 | + console.print(" [dim]No problem — studyctl works fine as a standalone CLI.[/dim]\n") |
| 80 | + |
| 81 | + # ------------------------------------------------------------------ |
| 82 | + # Step 3 — NotebookLM |
| 83 | + # ------------------------------------------------------------------ |
| 84 | + console.print("[bold]Step 3 of 5[/bold] Do you use Google NotebookLM?") |
| 85 | + console.print(" [dim](Optional — enables audio overview generation from your notes)[/dim]") |
| 86 | + use_nlm = click.confirm(" Enable NotebookLM integration?", default=False) |
| 87 | + config["notebooklm"] = {"enabled": use_nlm} |
| 88 | + if use_nlm: |
| 89 | + console.print( |
| 90 | + " [dim]NotebookLM enabled. You'll need a Google account" |
| 91 | + " with NotebookLM access.[/dim]\n" |
| 92 | + ) |
| 93 | + else: |
| 94 | + console.print(" [dim]Skipped — you can enable this later in config.yaml.[/dim]\n") |
| 95 | + |
| 96 | + # ------------------------------------------------------------------ |
| 97 | + # Step 4 — Obsidian vault |
| 98 | + # ------------------------------------------------------------------ |
| 99 | + console.print("[bold]Step 4 of 5[/bold] Where is your Obsidian vault?") |
| 100 | + console.print(" [dim](Optional — enables note sync and spaced repetition)[/dim]") |
| 101 | + use_obsidian = click.confirm(" Configure Obsidian integration?", default=True) |
| 102 | + if use_obsidian: |
| 103 | + obsidian_raw = click.prompt(" Obsidian vault path", default="~/Obsidian") |
| 104 | + obsidian_path = _validate_path(obsidian_raw) |
| 105 | + config["obsidian_base"] = str(obsidian_raw) |
| 106 | + if not obsidian_path.exists(): |
| 107 | + console.print( |
| 108 | + f" [yellow]Note: {obsidian_path} does not exist yet. " |
| 109 | + f"Create it before running sync.[/yellow]\n" |
| 110 | + ) |
| 111 | + else: |
| 112 | + console.print(f" [green]Found vault at {obsidian_path}[/green]\n") |
| 113 | + else: |
| 114 | + console.print(" [dim]Skipped — you can set obsidian_base in config.yaml later.[/dim]\n") |
| 115 | + |
| 116 | + # ------------------------------------------------------------------ |
| 117 | + # Step 5 — Write config |
| 118 | + # ------------------------------------------------------------------ |
| 119 | + console.print("[bold]Step 5 of 5[/bold] Writing configuration...") |
| 120 | + |
| 121 | + CONFIG_DIR.mkdir(parents=True, exist_ok=True) |
| 122 | + config_path = CONFIG_DIR / "config.yaml" |
| 123 | + |
| 124 | + # Preserve any existing keys by loading first, then merging |
| 125 | + existing: dict = {} |
| 126 | + if config_path.exists(): |
| 127 | + with contextlib.suppress(yaml.YAMLError): |
| 128 | + existing = yaml.safe_load(config_path.read_text()) or {} |
| 129 | + |
| 130 | + # New values take precedence over existing |
| 131 | + merged = {**existing, **config} |
| 132 | + # Nested dicts: merge content and notebooklm sub-keys |
| 133 | + for key in ("content", "notebooklm"): |
| 134 | + if key in existing and key in config and isinstance(existing[key], dict): |
| 135 | + merged[key] = {**existing[key], **config[key]} |
| 136 | + |
| 137 | + config_path.write_text(yaml.dump(merged, default_flow_style=False, sort_keys=False)) |
| 138 | + |
| 139 | + console.print(f"\n[bold green]Configuration saved to {config_path}[/bold green]") |
| 140 | + |
| 141 | + # ------------------------------------------------------------------ |
| 142 | + # Offer to launch web UI |
| 143 | + # ------------------------------------------------------------------ |
| 144 | + console.print() |
| 145 | + launch = click.confirm("Launch the studyctl web UI now?", default=False) |
| 146 | + if launch: |
| 147 | + console.print("[dim]Starting web UI on http://localhost:8000 ...[/dim]") |
| 148 | + try: |
| 149 | + subprocess.Popen( |
| 150 | + ["studyctl", "web"], |
| 151 | + stdout=subprocess.DEVNULL, |
| 152 | + stderr=subprocess.DEVNULL, |
| 153 | + ) |
| 154 | + console.print( |
| 155 | + "[green]Web UI started. Open http://localhost:8000 in your browser.[/green]" |
| 156 | + ) |
| 157 | + except FileNotFoundError: |
| 158 | + console.print("[yellow]Could not launch web UI. Run 'studyctl web' manually.[/yellow]") |
| 159 | + else: |
| 160 | + console.print("\n[bold]You're all set.[/bold] Next steps:") |
| 161 | + console.print(" studyctl config show — review your configuration") |
| 162 | + console.print(" studyctl review — see what topics are due for review") |
| 163 | + console.print(" studyctl web — open the web UI") |
| 164 | + console.print(" studyctl --help — explore all commands") |
0 commit comments