module.py 24 KB

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