module.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. from __future__ import annotations
  2. import logging
  3. from abc import ABC
  4. from pathlib import Path
  5. from typing import Any, Optional
  6. from rich.console import Console
  7. from rich.panel import Panel
  8. from rich.prompt import Confirm
  9. from typer import Argument, Context, Option, Typer, Exit
  10. from .display import DisplayManager
  11. from .library import LibraryManager
  12. from .prompt import PromptHandler
  13. from .template import Template
  14. logger = logging.getLogger(__name__)
  15. console = Console()
  16. def parse_var_inputs(var_options: list[str], extra_args: list[str]) -> dict[str, Any]:
  17. """Parse variable inputs from --var options and extra args.
  18. Supports formats:
  19. --var KEY=VALUE
  20. --var KEY VALUE
  21. Args:
  22. var_options: List of variable options from CLI
  23. extra_args: Additional arguments that may contain values
  24. Returns:
  25. Dictionary of parsed variables
  26. """
  27. variables = {}
  28. # Parse --var KEY=VALUE format
  29. for var_option in var_options:
  30. if '=' in var_option:
  31. key, value = var_option.split('=', 1)
  32. variables[key] = value
  33. else:
  34. # --var KEY VALUE format - value should be in extra_args
  35. if extra_args:
  36. variables[var_option] = extra_args.pop(0)
  37. else:
  38. logger.warning(f"No value provided for variable '{var_option}'")
  39. return variables
  40. class Module(ABC):
  41. """Streamlined base module that auto-detects variables from templates."""
  42. def __init__(self) -> None:
  43. if not all([self.name, self.description]):
  44. raise ValueError(
  45. f"Module {self.__class__.__name__} must define name and description"
  46. )
  47. logger.info(f"Initializing module '{self.name}'")
  48. logger.debug(f"Module '{self.name}' configuration: description='{self.description}'")
  49. self.libraries = LibraryManager()
  50. self.display = DisplayManager()
  51. def list(self) -> list[Template]:
  52. """List all templates."""
  53. logger.debug(f"Listing templates for module '{self.name}'")
  54. templates = []
  55. entries = self.libraries.find(self.name, sort_results=True)
  56. for template_dir, library_name in entries:
  57. try:
  58. template = Template(template_dir, library_name=library_name)
  59. templates.append(template)
  60. except Exception as exc:
  61. logger.error(f"Failed to load template from {template_dir}: {exc}")
  62. continue
  63. filtered_templates = templates
  64. if filtered_templates:
  65. self.display.display_templates_table(
  66. filtered_templates,
  67. self.name,
  68. f"{self.name.capitalize()} templates"
  69. )
  70. else:
  71. logger.info(f"No templates found for module '{self.name}'")
  72. return filtered_templates
  73. def search(
  74. self,
  75. query: str = Argument(..., help="Search string to filter templates by ID")
  76. ) -> list[Template]:
  77. """Search for templates by ID containing the search string."""
  78. logger.debug(f"Searching templates for module '{self.name}' with query='{query}'")
  79. templates = []
  80. entries = self.libraries.find(self.name, sort_results=True)
  81. for template_dir, library_name in entries:
  82. try:
  83. template = Template(template_dir, library_name=library_name)
  84. templates.append(template)
  85. except Exception as exc:
  86. logger.error(f"Failed to load template from {template_dir}: {exc}")
  87. continue
  88. # Apply search filtering
  89. filtered_templates = [t for t in templates if query.lower() in t.id.lower()]
  90. if filtered_templates:
  91. logger.info(f"Found {len(filtered_templates)} templates matching '{query}' for module '{self.name}'")
  92. self.display.display_templates_table(
  93. filtered_templates,
  94. self.name,
  95. f"{self.name.capitalize()} templates matching '{query}'"
  96. )
  97. else:
  98. logger.info(f"No templates found matching '{query}' for module '{self.name}'")
  99. console.print(f"[yellow]No templates found matching '{query}' for module '{self.name}'[/yellow]")
  100. return filtered_templates
  101. def show(
  102. self,
  103. id: str,
  104. show_content: bool = False,
  105. ) -> None:
  106. """Show template details."""
  107. logger.debug(f"Showing template '{id}' from module '{self.name}'")
  108. template = self._load_template_by_id(id)
  109. if not template:
  110. logger.warning(f"Template '{id}' not found in module '{self.name}'")
  111. console.print(f"[red]Template '{id}' not found in module '{self.name}'[/red]")
  112. return
  113. # Apply config defaults (same as in generate)
  114. # This ensures the display shows the actual defaults that will be used
  115. if template.variables:
  116. from .config import ConfigManager
  117. config = ConfigManager()
  118. config_defaults = config.get_defaults(self.name)
  119. if config_defaults:
  120. logger.debug(f"Loading config defaults for module '{self.name}'")
  121. # Apply config defaults (this respects the variable types and validation)
  122. successful = template.variables.apply_defaults(config_defaults, "config")
  123. if successful:
  124. logger.debug(f"Applied config defaults for: {', '.join(successful)}")
  125. self._display_template_details(template, id)
  126. def generate(
  127. self,
  128. id: str = Argument(..., help="Template ID"),
  129. directory: Optional[str] = Argument(None, help="Output directory (defaults to template ID)"),
  130. interactive: bool = Option(True, "--interactive/--no-interactive", "-i/-n", help="Enable interactive prompting for variables"),
  131. var: Optional[list[str]] = Option(None, "--var", "-v", help="Variable override (repeatable). Use KEY=VALUE or --var KEY VALUE"),
  132. ctx: Context = None,
  133. ) -> None:
  134. """Generate from template.
  135. Variable precedence chain (lowest to highest):
  136. 1. Module spec (defined in cli/modules/*.py)
  137. 2. Template spec (from template.yaml)
  138. 3. Config defaults (from ~/.config/boilerplates/config.yaml)
  139. 4. CLI overrides (--var flags)
  140. Examples:
  141. # Generate to directory named after template
  142. cli compose generate traefik
  143. # Generate to custom directory
  144. cli compose generate traefik my-proxy
  145. # Generate with variables
  146. cli compose generate traefik --var traefik_enabled=false
  147. """
  148. logger.info(f"Starting generation for template '{id}' from module '{self.name}'")
  149. template = self._load_template_by_id(id)
  150. # Apply config defaults (precedence: config > template > module)
  151. # Config only sets VALUES, not the spec structure
  152. if template.variables:
  153. from .config import ConfigManager
  154. config = ConfigManager()
  155. config_defaults = config.get_defaults(self.name)
  156. if config_defaults:
  157. logger.info(f"Loading config defaults for module '{self.name}'")
  158. # Apply config defaults (this respects the variable types and validation)
  159. successful = template.variables.apply_defaults(config_defaults, "config")
  160. if successful:
  161. logger.debug(f"Applied config defaults for: {', '.join(successful)}")
  162. # Apply CLI overrides (highest precedence)
  163. extra_args = list(ctx.args) if ctx and hasattr(ctx, "args") else []
  164. cli_overrides = parse_var_inputs(var or [], extra_args)
  165. if cli_overrides:
  166. logger.info(f"Received {len(cli_overrides)} variable overrides from CLI")
  167. if template.variables:
  168. successful_overrides = template.variables.apply_defaults(cli_overrides, "cli")
  169. if successful_overrides:
  170. logger.debug(f"Applied CLI overrides for: {', '.join(successful_overrides)}")
  171. self._display_template_details(template, id)
  172. console.print()
  173. variable_values = {}
  174. if interactive and template.variables:
  175. prompt_handler = PromptHandler()
  176. collected_values = prompt_handler.collect_variables(template.variables)
  177. if collected_values:
  178. variable_values.update(collected_values)
  179. logger.info(f"Collected {len(collected_values)} variable values from user input")
  180. if template.variables:
  181. # Use get_satisfied_values() to exclude variables from sections with unsatisfied dependencies
  182. variable_values.update(template.variables.get_satisfied_values())
  183. try:
  184. # Validate all variables before rendering
  185. if template.variables:
  186. template.variables.validate_all()
  187. rendered_files = template.render(template.variables)
  188. # Safety check for render result
  189. if not rendered_files:
  190. console.print("[red]Error: Template rendering returned no files[/red]")
  191. raise Exit(code=1)
  192. logger.info(f"Successfully rendered template '{id}'")
  193. # Determine output directory (default to template ID)
  194. output_dir = Path(directory) if directory else Path(id)
  195. # Check if directory exists and is not empty
  196. dir_exists = output_dir.exists()
  197. dir_not_empty = dir_exists and any(output_dir.iterdir())
  198. # Check which files already exist
  199. existing_files = []
  200. if dir_exists:
  201. for file_path in rendered_files.keys():
  202. full_path = output_dir / file_path
  203. if full_path.exists():
  204. existing_files.append(full_path)
  205. # Warn if directory is not empty (both interactive and non-interactive)
  206. if dir_not_empty:
  207. if interactive:
  208. console.print(f"\n[yellow]⚠ Warning: Directory '{output_dir}' is not empty.[/yellow]")
  209. if existing_files:
  210. console.print(f"[yellow] {len(existing_files)} file(s) will be overwritten.[/yellow]")
  211. if not Confirm.ask(f"Continue and potentially overwrite files in '{output_dir}'?", default=False):
  212. console.print("[yellow]Generation cancelled.[/yellow]")
  213. return
  214. else:
  215. # Non-interactive mode: show warning but continue
  216. logger.warning(f"Directory '{output_dir}' is not empty")
  217. if existing_files:
  218. logger.warning(f"{len(existing_files)} file(s) will be overwritten")
  219. # Display file generation confirmation in interactive mode
  220. if interactive:
  221. self.display.display_file_generation_confirmation(
  222. output_dir,
  223. rendered_files,
  224. existing_files if existing_files else None
  225. )
  226. # Final confirmation (only if we didn't already ask about overwriting)
  227. if not dir_not_empty:
  228. if not Confirm.ask("Generate these files?", default=True):
  229. console.print("[yellow]Generation cancelled.[/yellow]")
  230. return
  231. # Create the output directory if it doesn't exist
  232. output_dir.mkdir(parents=True, exist_ok=True)
  233. # Write rendered files to the output directory
  234. for file_path, content in rendered_files.items():
  235. full_path = output_dir / file_path
  236. full_path.parent.mkdir(parents=True, exist_ok=True)
  237. with open(full_path, 'w', encoding='utf-8') as f:
  238. f.write(content)
  239. console.print(f"[green]Generated file: {file_path}[/green]")
  240. console.print(f"\n[green]✓ Template generated successfully in '{output_dir}'[/green]")
  241. logger.info(f"Template written to directory: {output_dir}")
  242. # Display next steps if provided in template metadata
  243. if template.metadata.next_steps:
  244. self.display.display_next_steps(template.metadata.next_steps, variable_values)
  245. except Exception as e:
  246. logger.error(f"Error rendering template '{id}': {e}")
  247. console.print(f"[red]Error generating template: {e}[/red]")
  248. # Stop execution without letting Typer/Click print the exception again.
  249. raise Exit(code=1)
  250. def config_get(
  251. self,
  252. var_name: Optional[str] = Argument(None, help="Variable name to get (omit to show all defaults)"),
  253. ) -> None:
  254. """Get default value(s) for this module.
  255. Examples:
  256. # Get all defaults for module
  257. cli compose defaults get
  258. # Get specific variable default
  259. cli compose defaults get service_name
  260. """
  261. from .config import ConfigManager
  262. config = ConfigManager()
  263. if var_name:
  264. # Get specific variable default
  265. value = config.get_default_value(self.name, var_name)
  266. if value is not None:
  267. console.print(f"[green]{var_name}[/green] = [yellow]{value}[/yellow]")
  268. else:
  269. console.print(f"[red]No default set for variable '{var_name}' in module '{self.name}'[/red]")
  270. else:
  271. # Show all defaults (flat list)
  272. defaults = config.get_defaults(self.name)
  273. if defaults:
  274. console.print(f"[bold]Config defaults for module '{self.name}':[/bold]\n")
  275. for var_name, var_value in defaults.items():
  276. console.print(f" [green]{var_name}[/green] = [yellow]{var_value}[/yellow]")
  277. else:
  278. console.print(f"[yellow]No defaults configured for module '{self.name}'[/yellow]")
  279. def config_set(
  280. self,
  281. var_name: str = Argument(..., help="Variable name to set default for"),
  282. value: str = Argument(..., help="Default value"),
  283. ) -> None:
  284. """Set a default value for a variable.
  285. This only sets the DEFAULT VALUE, not the variable spec.
  286. The variable must be defined in the module or template spec.
  287. Examples:
  288. # Set default value
  289. cli compose defaults set service_name my-awesome-app
  290. # Set author for all compose templates
  291. cli compose defaults set author "Christian Lempa"
  292. """
  293. from .config import ConfigManager
  294. config = ConfigManager()
  295. # Set the default value
  296. config.set_default_value(self.name, var_name, value)
  297. console.print(f"[green] Set default:[/green] [cyan]{var_name}[/cyan] = [yellow]{value}[/yellow]")
  298. console.print(f"\n[dim]This will be used as the default value when generating templates with this module.[/dim]")
  299. def config_remove(
  300. self,
  301. var_name: str = Argument(..., help="Variable name to remove"),
  302. ) -> None:
  303. """Remove a specific default variable value.
  304. Examples:
  305. # Remove a default value
  306. cli compose defaults remove service_name
  307. """
  308. from .config import ConfigManager
  309. config = ConfigManager()
  310. defaults = config.get_defaults(self.name)
  311. if not defaults:
  312. console.print(f"[yellow]No defaults configured for module '{self.name}'[/yellow]")
  313. return
  314. if var_name in defaults:
  315. del defaults[var_name]
  316. config.set_defaults(self.name, defaults)
  317. console.print(f"[green] Removed default for '{var_name}'[/green]")
  318. else:
  319. console.print(f"[red]No default found for variable '{var_name}'[/red]")
  320. def config_clear(
  321. self,
  322. var_name: Optional[str] = Argument(None, help="Variable name to clear (omit to clear all defaults)"),
  323. force: bool = Option(False, "--force", "-f", help="Skip confirmation prompt"),
  324. ) -> None:
  325. """Clear default value(s) for this module.
  326. Examples:
  327. # Clear specific variable default
  328. cli compose defaults clear service_name
  329. # Clear all defaults for module
  330. cli compose defaults clear --force
  331. """
  332. from .config import ConfigManager
  333. config = ConfigManager()
  334. defaults = config.get_defaults(self.name)
  335. if not defaults:
  336. console.print(f"[yellow]No defaults configured for module '{self.name}'[/yellow]")
  337. return
  338. if var_name:
  339. # Clear specific variable
  340. if var_name in defaults:
  341. del defaults[var_name]
  342. config.set_defaults(self.name, defaults)
  343. console.print(f"[green] Cleared default for '{var_name}'[/green]")
  344. else:
  345. console.print(f"[red]No default found for variable '{var_name}'[/red]")
  346. else:
  347. # Clear all defaults
  348. if not force:
  349. console.print(f"[bold yellow] Warning:[/bold yellow] This will clear ALL defaults for module '[cyan]{self.name}[/cyan]'")
  350. console.print()
  351. # Show what will be cleared
  352. for var_name, var_value in defaults.items():
  353. console.print(f" [green]{var_name}[/green] = [yellow]{var_value}[/yellow]")
  354. console.print()
  355. if not Confirm.ask(f"[bold red]Are you sure?[/bold red]", default=False):
  356. console.print("[green]Operation cancelled.[/green]")
  357. return
  358. config.clear_defaults(self.name)
  359. console.print(f"[green] Cleared all defaults for module '{self.name}'[/green]")
  360. def config_list(self) -> None:
  361. """Display the defaults for this specific module in YAML format.
  362. Examples:
  363. # Show the defaults for the current module
  364. cli compose defaults list
  365. """
  366. from .config import ConfigManager
  367. import yaml
  368. config = ConfigManager()
  369. # Get only the defaults for this module
  370. defaults = config.get_defaults(self.name)
  371. if not defaults:
  372. console.print(f"[yellow]No configuration found for module '{self.name}'[/yellow]")
  373. console.print(f"\n[dim]Config file location: {config.get_config_path()}[/dim]")
  374. return
  375. # Create a minimal config structure with only this module's defaults
  376. module_config = {
  377. "defaults": {
  378. self.name: defaults
  379. }
  380. }
  381. # Convert config to YAML string
  382. yaml_output = yaml.dump(module_config, default_flow_style=False, sort_keys=False)
  383. console.print(f"[bold]Configuration for module:[/bold] [cyan]{self.name}[/cyan]")
  384. console.print(f"[dim]Config file: {config.get_config_path()}[/dim]\n")
  385. console.print(Panel(yaml_output, title=f"{self.name.capitalize()} Config", border_style="blue"))
  386. def validate(
  387. self,
  388. template_id: str = Argument(None, help="Template ID to validate (if omitted, validates all templates)"),
  389. verbose: bool = Option(False, "--verbose", "-v", help="Show detailed validation information")
  390. ) -> None:
  391. """Validate templates for Jinja2 syntax errors and undefined variables.
  392. Examples:
  393. # Validate all templates in this module
  394. cli compose validate
  395. # Validate a specific template
  396. cli compose validate gitlab
  397. # Validate with verbose output
  398. cli compose validate --verbose
  399. """
  400. from rich.table import Table
  401. if template_id:
  402. # Validate a specific template
  403. try:
  404. template = self._load_template_by_id(template_id)
  405. console.print(f"[bold]Validating template:[/bold] [cyan]{template_id}[/cyan]\n")
  406. try:
  407. # Trigger validation by accessing used_variables
  408. _ = template.used_variables
  409. # Trigger variable definition validation by accessing variables
  410. _ = template.variables
  411. console.print(f"[green] Template '{template_id}' is valid[/green]")
  412. if verbose:
  413. console.print(f"\n[dim]Template path: {template.template_dir}[/dim]")
  414. console.print(f"[dim]Found {len(template.used_variables)} variables[/dim]")
  415. except ValueError as e:
  416. console.print(f"[red] Validation failed for '{template_id}':[/red]")
  417. console.print(f"\n{e}")
  418. raise Exit(code=1)
  419. except Exception as e:
  420. console.print(f"[red]Error loading template '{template_id}': {e}[/red]")
  421. raise Exit(code=1)
  422. else:
  423. # Validate all templates
  424. console.print(f"[bold]Validating all {self.name} templates...[/bold]\n")
  425. entries = self.libraries.find(self.name, sort_results=True)
  426. total = len(entries)
  427. valid_count = 0
  428. invalid_count = 0
  429. errors = []
  430. for template_dir, library_name in entries:
  431. template_id = template_dir.name
  432. try:
  433. template = Template(template_dir, library_name=library_name)
  434. # Trigger validation
  435. _ = template.used_variables
  436. _ = template.variables
  437. valid_count += 1
  438. if verbose:
  439. console.print(f"[green][/green] {template_id}")
  440. except ValueError as e:
  441. invalid_count += 1
  442. errors.append((template_id, str(e)))
  443. if verbose:
  444. console.print(f"[red][/red] {template_id}")
  445. except Exception as e:
  446. invalid_count += 1
  447. errors.append((template_id, f"Load error: {e}"))
  448. if verbose:
  449. console.print(f"[yellow]?[/yellow] {template_id}")
  450. # Summary
  451. console.print(f"\n[bold]Validation Summary:[/bold]")
  452. summary_table = Table(show_header=False, box=None, padding=(0, 2))
  453. summary_table.add_column(style="bold")
  454. summary_table.add_column()
  455. summary_table.add_row("Total templates:", str(total))
  456. summary_table.add_row("[green]Valid:[/green]", str(valid_count))
  457. summary_table.add_row("[red]Invalid:[/red]", str(invalid_count))
  458. console.print(summary_table)
  459. # Show errors if any
  460. if errors:
  461. console.print(f"\n[bold red]Validation Errors:[/bold red]")
  462. for template_id, error_msg in errors:
  463. console.print(f"\n[yellow]Template:[/yellow] [cyan]{template_id}[/cyan]")
  464. console.print(f"[dim]{error_msg}[/dim]")
  465. raise Exit(code=1)
  466. else:
  467. console.print(f"\n[green] All templates are valid![/green]")
  468. @classmethod
  469. def register_cli(cls, app: Typer) -> None:
  470. """Register module commands with the main app."""
  471. logger.debug(f"Registering CLI commands for module '{cls.name}'")
  472. module_instance = cls()
  473. module_app = Typer(help=cls.description)
  474. module_app.command("list")(module_instance.list)
  475. module_app.command("search")(module_instance.search)
  476. module_app.command("show")(module_instance.show)
  477. module_app.command("validate")(module_instance.validate)
  478. module_app.command(
  479. "generate",
  480. context_settings={"allow_extra_args": True, "ignore_unknown_options": True}
  481. )(module_instance.generate)
  482. # Add defaults commands (simplified - only manage default values)
  483. defaults_app = Typer(help="Manage default values for template variables")
  484. defaults_app.command("get", help="Get default value(s)")(module_instance.config_get)
  485. defaults_app.command("set", help="Set a default value")(module_instance.config_set)
  486. defaults_app.command("remove", help="Remove a specific default value")(module_instance.config_remove)
  487. defaults_app.command("clear", help="Clear default value(s)")(module_instance.config_clear)
  488. defaults_app.command("list", help="Display the config for this module in YAML format")(module_instance.config_list)
  489. module_app.add_typer(defaults_app, name="defaults")
  490. app.add_typer(module_app, name=cls.name, help=cls.description)
  491. logger.info(f"Module '{cls.name}' CLI commands registered")
  492. def _load_template_by_id(self, template_id: str) -> Template:
  493. result = self.libraries.find_by_id(self.name, template_id)
  494. if not result:
  495. logger.debug(f"Template '{template_id}' not found in module '{self.name}'")
  496. raise FileNotFoundError(f"Template '{template_id}' not found in module '{self.name}'")
  497. template_dir, library_name = result
  498. try:
  499. return Template(template_dir, library_name=library_name)
  500. except (ValueError, FileNotFoundError) as exc:
  501. raise FileNotFoundError(f"Template '{template_id}' validation failed in module '{self.name}'") from exc
  502. except Exception as exc:
  503. logger.error(f"Failed to load template from {template_dir}: {exc}")
  504. raise FileNotFoundError(f"Template '{template_id}' could not be loaded in module '{self.name}'") from exc
  505. def _display_template_details(self, template: Template, template_id: str) -> None:
  506. """Display template information panel and variables table."""
  507. self.display.display_template_details(template, template_id)