module.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. out: Optional[Path] = Option(None, "--out", "-o", help="Output directory"),
  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. """
  151. logger.info(f"Starting generation for template '{id}' from module '{self.name}'")
  152. template = self._load_template_by_id(id)
  153. # Apply config defaults (precedence: config > template > module)
  154. # Config only sets VALUES, not the spec structure
  155. if template.variables:
  156. from .config import ConfigManager
  157. config = ConfigManager()
  158. config_defaults = config.get_defaults(self.name)
  159. if config_defaults:
  160. logger.info(f"Loading config defaults for module '{self.name}'")
  161. # Apply config defaults (this respects the variable types and validation)
  162. successful = template.variables.apply_defaults(config_defaults, "config")
  163. if successful:
  164. logger.debug(f"Applied config defaults for: {', '.join(successful)}")
  165. # Apply CLI overrides (highest precedence)
  166. extra_args = list(ctx.args) if ctx and hasattr(ctx, "args") else []
  167. cli_overrides = parse_var_inputs(var or [], extra_args)
  168. if cli_overrides:
  169. logger.info(f"Received {len(cli_overrides)} variable overrides from CLI")
  170. if template.variables:
  171. successful_overrides = template.variables.apply_defaults(cli_overrides, "cli")
  172. if successful_overrides:
  173. logger.debug(f"Applied CLI overrides for: {', '.join(successful_overrides)}")
  174. self._display_template_details(template, id)
  175. console.print()
  176. variable_values = {}
  177. if interactive and template.variables:
  178. prompt_handler = PromptHandler()
  179. collected_values = prompt_handler.collect_variables(template.variables)
  180. if collected_values:
  181. variable_values.update(collected_values)
  182. logger.info(f"Collected {len(collected_values)} variable values from user input")
  183. if template.variables:
  184. variable_values.update(template.variables.get_all_values())
  185. try:
  186. # Validate all variables before rendering
  187. if template.variables:
  188. template.variables.validate_all()
  189. rendered_files = template.render(template.variables)
  190. # Safety check for render result
  191. if not rendered_files:
  192. console.print("[red]Error: Template rendering returned no files[/red]")
  193. raise Exit(code=1)
  194. logger.info(f"Successfully rendered template '{id}'")
  195. output_dir = out or Path(".")
  196. # Check which files already exist
  197. existing_files = []
  198. if output_dir.exists():
  199. for file_path in rendered_files.keys():
  200. full_path = output_dir / file_path
  201. if full_path.exists():
  202. existing_files.append(full_path)
  203. # Display file generation confirmation
  204. if interactive:
  205. self.display.display_file_generation_confirmation(
  206. output_dir,
  207. rendered_files,
  208. existing_files if existing_files else None
  209. )
  210. # Ask for confirmation
  211. if existing_files:
  212. prompt_msg = f"[yellow][/yellow] {len(existing_files)} file(s) will be overwritten. Continue?"
  213. else:
  214. prompt_msg = "Generate these files?"
  215. if not Confirm.ask(prompt_msg, default=True):
  216. console.print("[yellow]Generation cancelled.[/yellow]")
  217. return
  218. else:
  219. # Non-interactive mode: warn if files will be overwritten
  220. if existing_files:
  221. logger.warning(f"{len(existing_files)} file(s) will be overwritten in '{output_dir}'")
  222. # Create the output directory if it doesn't exist
  223. output_dir.mkdir(parents=True, exist_ok=True)
  224. # Write rendered files to the output directory
  225. for file_path, content in rendered_files.items():
  226. full_path = output_dir / file_path
  227. full_path.parent.mkdir(parents=True, exist_ok=True)
  228. with open(full_path, 'w', encoding='utf-8') as f:
  229. f.write(content)
  230. console.print(f"[green]Generated file: {file_path}[/green]")
  231. logger.info(f"Template written to directory: {output_dir}")
  232. # If no output directory was specified, print the masked content to the console
  233. if not out:
  234. console.print("\n[bold]Rendered output (sensitive values masked):[/bold]")
  235. masked_files = template.mask_sensitive_values(rendered_files, template.variables)
  236. for file_path, content in masked_files.items():
  237. console.print(Panel(content, title=file_path, border_style="green"))
  238. except Exception as e:
  239. logger.error(f"Error rendering template '{id}': {e}")
  240. console.print(f"[red]Error generating template: {e}[/red]")
  241. # Stop execution without letting Typer/Click print the exception again.
  242. raise Exit(code=1)
  243. # --------------------------
  244. # SECTION: Config Commands
  245. # --------------------------
  246. def config_get(
  247. self,
  248. var_name: Optional[str] = Argument(None, help="Variable name to get (omit to show all defaults)"),
  249. ) -> None:
  250. """Get default value(s) for this module.
  251. Examples:
  252. # Get all defaults for module
  253. cli compose defaults get
  254. # Get specific variable default
  255. cli compose defaults get service_name
  256. """
  257. from .config import ConfigManager
  258. config = ConfigManager()
  259. if var_name:
  260. # Get specific variable default
  261. value = config.get_default_value(self.name, var_name)
  262. if value is not None:
  263. console.print(f"[green]{var_name}[/green] = [yellow]{value}[/yellow]")
  264. else:
  265. console.print(f"[red]No default set for variable '{var_name}' in module '{self.name}'[/red]")
  266. else:
  267. # Show all defaults (flat list)
  268. defaults = config.get_defaults(self.name)
  269. if defaults:
  270. console.print(f"[bold]Config defaults for module '{self.name}':[/bold]\n")
  271. for var_name, var_value in defaults.items():
  272. console.print(f" [green]{var_name}[/green] = [yellow]{var_value}[/yellow]")
  273. else:
  274. console.print(f"[yellow]No defaults configured for module '{self.name}'[/yellow]")
  275. def config_set(
  276. self,
  277. var_name: str = Argument(..., help="Variable name to set default for"),
  278. value: str = Argument(..., help="Default value"),
  279. ) -> None:
  280. """Set a default value for a variable.
  281. This only sets the DEFAULT VALUE, not the variable spec.
  282. The variable must be defined in the module or template spec.
  283. Examples:
  284. # Set default value
  285. cli compose defaults set service_name my-awesome-app
  286. # Set author for all compose templates
  287. cli compose defaults set author "Christian Lempa"
  288. """
  289. from .config import ConfigManager
  290. config = ConfigManager()
  291. # Set the default value
  292. config.set_default_value(self.name, var_name, value)
  293. console.print(f"[green] Set default:[/green] [cyan]{var_name}[/cyan] = [yellow]{value}[/yellow]")
  294. console.print(f"\n[dim]This will be used as the default value when generating templates with this module.[/dim]")
  295. def config_remove(
  296. self,
  297. var_name: str = Argument(..., help="Variable name to remove"),
  298. ) -> None:
  299. """Remove a specific default variable value.
  300. Examples:
  301. # Remove a default value
  302. cli compose defaults remove service_name
  303. """
  304. from .config import ConfigManager
  305. config = ConfigManager()
  306. defaults = config.get_defaults(self.name)
  307. if not defaults:
  308. console.print(f"[yellow]No defaults configured for module '{self.name}'[/yellow]")
  309. return
  310. if var_name in defaults:
  311. del defaults[var_name]
  312. config.set_defaults(self.name, defaults)
  313. console.print(f"[green] Removed default for '{var_name}'[/green]")
  314. else:
  315. console.print(f"[red]No default found for variable '{var_name}'[/red]")
  316. def config_clear(
  317. self,
  318. var_name: Optional[str] = Argument(None, help="Variable name to clear (omit to clear all defaults)"),
  319. force: bool = Option(False, "--force", "-f", help="Skip confirmation prompt"),
  320. ) -> None:
  321. """Clear default value(s) for this module.
  322. Examples:
  323. # Clear specific variable default
  324. cli compose defaults clear service_name
  325. # Clear all defaults for module
  326. cli compose defaults clear --force
  327. """
  328. from .config import ConfigManager
  329. config = ConfigManager()
  330. defaults = config.get_defaults(self.name)
  331. if not defaults:
  332. console.print(f"[yellow]No defaults configured for module '{self.name}'[/yellow]")
  333. return
  334. if var_name:
  335. # Clear specific variable
  336. if var_name in defaults:
  337. del defaults[var_name]
  338. config.set_defaults(self.name, defaults)
  339. console.print(f"[green] Cleared default for '{var_name}'[/green]")
  340. else:
  341. console.print(f"[red]No default found for variable '{var_name}'[/red]")
  342. else:
  343. # Clear all defaults
  344. if not force:
  345. console.print(f"[bold yellow] Warning:[/bold yellow] This will clear ALL defaults for module '[cyan]{self.name}[/cyan]'")
  346. console.print()
  347. # Show what will be cleared
  348. for var_name, var_value in defaults.items():
  349. console.print(f" [green]{var_name}[/green] = [yellow]{var_value}[/yellow]")
  350. console.print()
  351. if not Confirm.ask(f"[bold red]Are you sure?[/bold red]", default=False):
  352. console.print("[green]Operation cancelled.[/green]")
  353. return
  354. config.clear_defaults(self.name)
  355. console.print(f"[green] Cleared all defaults for module '{self.name}'[/green]")
  356. def config_list(self) -> None:
  357. """Display the defaults for this specific module in YAML format.
  358. Examples:
  359. # Show the defaults for the current module
  360. cli compose defaults list
  361. """
  362. from .config import ConfigManager
  363. import yaml
  364. config = ConfigManager()
  365. # Get only the defaults for this module
  366. defaults = config.get_defaults(self.name)
  367. if not defaults:
  368. console.print(f"[yellow]No configuration found for module '{self.name}'[/yellow]")
  369. console.print(f"\n[dim]Config file location: {config.get_config_path()}[/dim]")
  370. return
  371. # Create a minimal config structure with only this module's defaults
  372. module_config = {
  373. "defaults": {
  374. self.name: defaults
  375. }
  376. }
  377. # Convert config to YAML string
  378. yaml_output = yaml.dump(module_config, default_flow_style=False, sort_keys=False)
  379. console.print(f"[bold]Configuration for module:[/bold] [cyan]{self.name}[/cyan]")
  380. console.print(f"[dim]Config file: {config.get_config_path()}[/dim]\n")
  381. console.print(Panel(yaml_output, title=f"{self.name.capitalize()} Config", border_style="blue"))
  382. def validate(
  383. self,
  384. template_id: str = Argument(None, help="Template ID to validate (if omitted, validates all templates)"),
  385. verbose: bool = Option(False, "--verbose", "-v", help="Show detailed validation information")
  386. ) -> None:
  387. """Validate templates for Jinja2 syntax errors and undefined variables.
  388. Examples:
  389. # Validate all templates in this module
  390. cli compose validate
  391. # Validate a specific template
  392. cli compose validate gitlab
  393. # Validate with verbose output
  394. cli compose validate --verbose
  395. """
  396. from rich.table import Table
  397. if template_id:
  398. # Validate a specific template
  399. try:
  400. template = self._load_template_by_id(template_id)
  401. console.print(f"[bold]Validating template:[/bold] [cyan]{template_id}[/cyan]\n")
  402. try:
  403. # Trigger validation by accessing used_variables
  404. _ = template.used_variables
  405. # Trigger variable definition validation by accessing variables
  406. _ = template.variables
  407. console.print(f"[green] Template '{template_id}' is valid[/green]")
  408. if verbose:
  409. console.print(f"\n[dim]Template path: {template.template_dir}[/dim]")
  410. console.print(f"[dim]Found {len(template.used_variables)} variables[/dim]")
  411. except ValueError as e:
  412. console.print(f"[red] Validation failed for '{template_id}':[/red]")
  413. console.print(f"\n{e}")
  414. raise Exit(code=1)
  415. except Exception as e:
  416. console.print(f"[red]Error loading template '{template_id}': {e}[/red]")
  417. raise Exit(code=1)
  418. else:
  419. # Validate all templates
  420. console.print(f"[bold]Validating all {self.name} templates...[/bold]\n")
  421. entries = self.libraries.find(self.name, sort_results=True)
  422. total = len(entries)
  423. valid_count = 0
  424. invalid_count = 0
  425. errors = []
  426. for template_dir, library_name in entries:
  427. template_id = template_dir.name
  428. try:
  429. template = Template(template_dir, library_name=library_name)
  430. # Trigger validation
  431. _ = template.used_variables
  432. _ = template.variables
  433. valid_count += 1
  434. if verbose:
  435. console.print(f"[green][/green] {template_id}")
  436. except ValueError as e:
  437. invalid_count += 1
  438. errors.append((template_id, str(e)))
  439. if verbose:
  440. console.print(f"[red][/red] {template_id}")
  441. except Exception as e:
  442. invalid_count += 1
  443. errors.append((template_id, f"Load error: {e}"))
  444. if verbose:
  445. console.print(f"[yellow]?[/yellow] {template_id}")
  446. # Summary
  447. console.print(f"\n[bold]Validation Summary:[/bold]")
  448. summary_table = Table(show_header=False, box=None, padding=(0, 2))
  449. summary_table.add_column(style="bold")
  450. summary_table.add_column()
  451. summary_table.add_row("Total templates:", str(total))
  452. summary_table.add_row("[green]Valid:[/green]", str(valid_count))
  453. summary_table.add_row("[red]Invalid:[/red]", str(invalid_count))
  454. console.print(summary_table)
  455. # Show errors if any
  456. if errors:
  457. console.print(f"\n[bold red]Validation Errors:[/bold red]")
  458. for template_id, error_msg in errors:
  459. console.print(f"\n[yellow]Template:[/yellow] [cyan]{template_id}[/cyan]")
  460. console.print(f"[dim]{error_msg}[/dim]")
  461. raise Exit(code=1)
  462. else:
  463. console.print(f"\n[green] All templates are valid![/green]")
  464. # !SECTION
  465. # ------------------------------
  466. # SECTION: CLI Registration
  467. # ------------------------------
  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. # !SECTION
  493. # --------------------------
  494. # SECTION: Private Methods
  495. # --------------------------
  496. def _load_template_by_id(self, template_id: str) -> Template:
  497. result = self.libraries.find_by_id(self.name, template_id)
  498. if not result:
  499. logger.debug(f"Template '{template_id}' not found in module '{self.name}'")
  500. raise FileNotFoundError(f"Template '{template_id}' not found in module '{self.name}'")
  501. template_dir, library_name = result
  502. try:
  503. return Template(template_dir, library_name=library_name)
  504. except (ValueError, FileNotFoundError) as exc:
  505. raise FileNotFoundError(f"Template '{template_id}' validation failed in module '{self.name}'") from exc
  506. except Exception as exc:
  507. logger.error(f"Failed to load template from {template_dir}: {exc}")
  508. raise FileNotFoundError(f"Template '{template_id}' could not be loaded in module '{self.name}'") from exc
  509. def _display_template_details(self, template: Template, template_id: str) -> None:
  510. """Display template information panel and variables table."""
  511. self.display.display_template_details(template, template_id)
  512. # !SECTION