|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import sys |
5 | 6 | from pathlib import Path |
6 | 7 |
|
7 | 8 | import click |
|
60 | 61 | " left alone." |
61 | 62 | ), |
62 | 63 | ) |
| 64 | +@click.option( |
| 65 | + "--yes", |
| 66 | + "-y", |
| 67 | + "yes", |
| 68 | + is_flag=True, |
| 69 | + help=( |
| 70 | + "Accept all defaults without prompting. Also implied whenever" |
| 71 | + " stdin is not a TTY (pipes, CI, the git hooks). Writes a" |
| 72 | + " default whygraph.toml if none exists and never clobbers an" |
| 73 | + " existing one." |
| 74 | + ), |
| 75 | +) |
63 | 76 | def init_cmd( |
64 | 77 | agent_name: str | None, |
65 | 78 | print_only: bool, |
66 | 79 | list_agents: bool, |
67 | 80 | install_assets: bool, |
68 | 81 | skip_preflight: bool, |
69 | 82 | force: bool, |
| 83 | + yes: bool, |
70 | 84 | ) -> None: |
71 | 85 | """Initialize the WhyGraph database under ``.whygraph/whygraph.db``. |
72 | 86 |
|
73 | | - Also writes a committable ``whygraph.example.toml`` at the project root |
74 | | - (it documents every tunable and ships the built-in defaults — copy it |
75 | | - to ``whygraph.toml`` and edit to customize) and ensures the project's |
76 | | - ``.gitignore`` keeps the user-owned config and generated caches out of |
77 | | - git (``whygraph.toml``, ``.whygraph/``, ``.codegraph/``). |
| 87 | + On a terminal this runs a guided, arrow-key setup — pick the agent, |
| 88 | + the analyze/rationale LLMs (+ API keys), and the source-control |
| 89 | + provider (+ token), review a summary that masks every secret, then |
| 90 | + confirm. It writes a committable ``whygraph.example.toml`` (never any |
| 91 | + secrets) and, once confirmed, a ready-to-run ``whygraph.toml`` (with |
| 92 | + the secrets you entered). Every prompt is defaulted, so a bare Enter |
| 93 | + accepts it. |
| 94 | +
|
| 95 | + ``--yes`` (and any non-TTY invocation: pipes, CI, the git hooks) |
| 96 | + skips all prompts and uses defaults — writing a default |
| 97 | + ``whygraph.toml`` only if none exists and never clobbering an existing |
| 98 | + one. A bare non-TTY ``init`` refreshes only the example, as before. |
| 99 | +
|
| 100 | + Either way it ensures the project's ``.gitignore`` keeps the |
| 101 | + user-owned config and generated caches out of git (``whygraph.toml``, |
| 102 | + ``.whygraph/``, ``.codegraph/``). |
78 | 103 |
|
79 | 104 | Does **not** index CodeGraph — that happens on ``whygraph scan``, |
80 | 105 | which populates ``.codegraph/codegraph.db`` (and refreshes it on every |
@@ -103,20 +128,28 @@ def init_cmd( |
103 | 128 | if not skip_preflight: |
104 | 129 | _run_preflight() |
105 | 130 |
|
| 131 | + # Prompt only on a real terminal and only when not told to accept |
| 132 | + # defaults. Pipes / CI / the git hooks have no TTY and fall straight |
| 133 | + # through to defaults — identical to the pre-interactive behaviour. |
| 134 | + interactive = sys.stdin.isatty() and not yes |
| 135 | + answers = _gather_answers(project_root, agent_name, interactive=interactive) |
| 136 | + |
106 | 137 | db_path = _ensure_db_initialized() |
107 | 138 | click.echo(f"Initialized WhyGraph database at {db_path}") |
108 | 139 |
|
109 | | - _scaffold_example_config(project_root) |
| 140 | + _scaffold_example_config(project_root, answers) |
| 141 | + _maybe_write_user_config(project_root, answers, write_user=interactive or yes) |
110 | 142 | _ensure_gitignore(project_root) |
111 | 143 |
|
112 | | - if agent_name is None: |
| 144 | + resolved_agent = answers.agent or agent_name |
| 145 | + if resolved_agent is None: |
113 | 146 | click.echo( |
114 | 147 | "Tip: run `whygraph init --list-agents` to see supported agents," |
115 | 148 | " then `whygraph init --agent <name>` to wire it up." |
116 | 149 | ) |
117 | 150 | return |
118 | 151 |
|
119 | | - target = agents.resolve_agent(agent_name) |
| 152 | + target = agents.resolve_agent(resolved_agent) |
120 | 153 | snippet = agents.render_snippet(target) |
121 | 154 |
|
122 | 155 | if print_only or not agents.is_write_supported(target): |
@@ -144,21 +177,82 @@ def _run_preflight() -> None: |
144 | 177 | fail(str(exc)) |
145 | 178 |
|
146 | 179 |
|
147 | | -def _scaffold_example_config(project_root: Path) -> None: |
| 180 | +def _gather_answers(project_root: Path, agent_name: str | None, *, interactive: bool): |
| 181 | + """Collect the init choices — interactively, or from defaults. |
| 182 | +
|
| 183 | + In interactive mode this runs the guided flow (agent + LLMs + scan + |
| 184 | + a summary panel + confirm) and returns its answers. A Ctrl-C / EOF at |
| 185 | + any prompt or a declined confirm raises :class:`InitAborted`, which we |
| 186 | + turn into a clean non-zero exit **before** any file is written or the |
| 187 | + DB is bootstrapped. Non-interactive callers get the defaults with the |
| 188 | + ``--agent`` value threaded in. |
| 189 | +
|
| 190 | + Lazy-imports the interactive module so lightweight surfaces stay fast. |
| 191 | + """ |
| 192 | + from whygraph.core.config import DEFAULT_ANSWERS, InitAnswers |
| 193 | + |
| 194 | + if not interactive: |
| 195 | + return InitAnswers(agent=agent_name, reconfigure_toml=False) |
| 196 | + |
| 197 | + from whygraph.cli.interactive import InitAborted, prompt_for_init |
| 198 | + |
| 199 | + try: |
| 200 | + return prompt_for_init( |
| 201 | + project_root, |
| 202 | + preset_agent=agent_name, |
| 203 | + on_summary=_render_summary_panel, |
| 204 | + ) |
| 205 | + except InitAborted: |
| 206 | + fail("Aborted — no changes written.") |
| 207 | + # Unreachable (fail raises); satisfies type-checkers. |
| 208 | + return DEFAULT_ANSWERS |
| 209 | + |
| 210 | + |
| 211 | +def _render_summary_panel(summary: str) -> None: |
| 212 | + """Render the pre-write review summary as a Rich panel on stderr.""" |
| 213 | + from rich.panel import Panel |
| 214 | + |
| 215 | + from ..console import console |
| 216 | + |
| 217 | + console.print( |
| 218 | + Panel(summary, title="Review your choices", border_style="cyan", expand=False) |
| 219 | + ) |
| 220 | + |
| 221 | + |
| 222 | +def _scaffold_example_config(project_root: Path, answers) -> None: |
148 | 223 | """Write the committable ``whygraph.example.toml`` at the project root. |
149 | 224 |
|
150 | | - Always refreshed so it tracks the shipped defaults. Lazy-imports the |
151 | | - config helper so lightweight surfaces like ``--list-agents`` and |
| 225 | + Always refreshed so it tracks the shipped defaults and any non-secret |
| 226 | + choices from ``answers``. Secrets are never written here. Lazy-imports |
| 227 | + the config helper so lightweight surfaces like ``--list-agents`` and |
152 | 228 | ``--help`` don't pay the cost. |
153 | 229 | """ |
154 | 230 | from whygraph.core.config import write_example_config |
155 | 231 |
|
156 | | - path = write_example_config(project_root) |
| 232 | + path = write_example_config(project_root, answers) |
157 | 233 | click.echo( |
158 | 234 | f"Wrote example config to {path} — copy to whygraph.toml and edit to customize" |
159 | 235 | ) |
160 | 236 |
|
161 | 237 |
|
| 238 | +def _maybe_write_user_config(project_root: Path, answers, *, write_user: bool) -> None: |
| 239 | + """Write ``whygraph.toml`` when appropriate, else preserve an existing one. |
| 240 | +
|
| 241 | + Writes only when ``write_user`` (interactive or ``--yes``) **and** the |
| 242 | + file is absent or the user chose to reconfigure it. A bare non-TTY |
| 243 | + ``init`` never writes it (``write_user`` is ``False``), preserving the |
| 244 | + historical scaffold-only behaviour. |
| 245 | + """ |
| 246 | + from whygraph.core.config import CONFIG_FILENAME, write_user_config |
| 247 | + |
| 248 | + user_path = project_root / CONFIG_FILENAME |
| 249 | + if write_user and (not user_path.exists() or answers.reconfigure_toml): |
| 250 | + path = write_user_config(project_root, answers) |
| 251 | + click.echo(f"Wrote {path} — gitignored; holds any secrets you entered") |
| 252 | + elif user_path.exists(): |
| 253 | + click.echo(f"Kept existing {user_path}") |
| 254 | + |
| 255 | + |
162 | 256 | def _ensure_gitignore(project_root: Path) -> None: |
163 | 257 | """Keep the user config and generated caches out of git. |
164 | 258 |
|
|
0 commit comments