module.py 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. from __future__ import annotations
  2. import logging
  3. from abc import ABC
  4. from pathlib import Path
  5. from typing import Any, Optional, List, Dict
  6. from rich.console import Console
  7. from rich.panel import Panel
  8. from rich.prompt import Confirm
  9. from typer import Argument, Option, Typer, Exit
  10. from .display import DisplayManager
  11. from .exceptions import (
  12. TemplateRenderError,
  13. TemplateSyntaxError,
  14. TemplateValidationError,
  15. )
  16. from .library import LibraryManager
  17. from .prompt import PromptHandler
  18. from .template import Template
  19. logger = logging.getLogger(__name__)
  20. console = Console()
  21. console_err = Console(stderr=True)
  22. def parse_var_inputs(var_options: List[str], extra_args: List[str]) -> Dict[str, Any]:
  23. """Parse variable inputs from --var options and extra args.
  24. Supports formats:
  25. --var KEY=VALUE
  26. --var KEY VALUE
  27. Args:
  28. var_options: List of variable options from CLI
  29. extra_args: Additional arguments that may contain values
  30. Returns:
  31. Dictionary of parsed variables
  32. """
  33. variables = {}
  34. # Parse --var KEY=VALUE format
  35. for var_option in var_options:
  36. if "=" in var_option:
  37. key, value = var_option.split("=", 1)
  38. variables[key] = value
  39. else:
  40. # --var KEY VALUE format - value should be in extra_args
  41. if extra_args:
  42. variables[var_option] = extra_args.pop(0)
  43. else:
  44. logger.warning(f"No value provided for variable '{var_option}'")
  45. return variables
  46. class Module(ABC):
  47. """Streamlined base module that auto-detects variables from templates."""
  48. # Schema version supported by this module (override in subclasses)
  49. schema_version: str = "1.0"
  50. def __init__(self) -> None:
  51. if not all([self.name, self.description]):
  52. raise ValueError(
  53. f"Module {self.__class__.__name__} must define name and description"
  54. )
  55. logger.info(f"Initializing module '{self.name}'")
  56. logger.debug(
  57. f"Module '{self.name}' configuration: description='{self.description}'"
  58. )
  59. self.libraries = LibraryManager()
  60. self.display = DisplayManager()
  61. def list(
  62. self,
  63. raw: bool = Option(
  64. False, "--raw", help="Output raw list format instead of rich table"
  65. ),
  66. ) -> list[Template]:
  67. """List all templates."""
  68. logger.debug(f"Listing templates for module '{self.name}'")
  69. templates = []
  70. entries = self.libraries.find(self.name, sort_results=True)
  71. for entry in entries:
  72. # Unpack entry - now returns (path, library_name, needs_qualification)
  73. template_dir = entry[0]
  74. library_name = entry[1]
  75. needs_qualification = entry[2] if len(entry) > 2 else False
  76. try:
  77. # Get library object to determine type
  78. library = next(
  79. (
  80. lib
  81. for lib in self.libraries.libraries
  82. if lib.name == library_name
  83. ),
  84. None,
  85. )
  86. library_type = library.library_type if library else "git"
  87. template = Template(
  88. template_dir, library_name=library_name, library_type=library_type
  89. )
  90. # Validate schema version compatibility
  91. template._validate_schema_version(self.schema_version, self.name)
  92. # If template ID needs qualification, set qualified ID
  93. if needs_qualification:
  94. template.set_qualified_id()
  95. templates.append(template)
  96. except Exception as exc:
  97. logger.error(f"Failed to load template from {template_dir}: {exc}")
  98. continue
  99. filtered_templates = templates
  100. if filtered_templates:
  101. if raw:
  102. # Output raw format (tab-separated values for easy filtering with awk/sed/cut)
  103. # Format: ID\tNAME\tTAGS\tVERSION\tLIBRARY
  104. for template in filtered_templates:
  105. name = template.metadata.name or "Unnamed Template"
  106. tags_list = template.metadata.tags or []
  107. tags = ",".join(tags_list) if tags_list else "-"
  108. version = (
  109. str(template.metadata.version)
  110. if template.metadata.version
  111. else "-"
  112. )
  113. library = template.metadata.library or "-"
  114. print(f"{template.id}\t{name}\t{tags}\t{version}\t{library}")
  115. else:
  116. # Output rich table format
  117. self.display.display_templates_table(
  118. filtered_templates, self.name, f"{self.name.capitalize()} templates"
  119. )
  120. else:
  121. logger.info(f"No templates found for module '{self.name}'")
  122. return filtered_templates
  123. def search(
  124. self, query: str = Argument(..., help="Search string to filter templates by ID")
  125. ) -> list[Template]:
  126. """Search for templates by ID containing the search string."""
  127. logger.debug(
  128. f"Searching templates for module '{self.name}' with query='{query}'"
  129. )
  130. templates = []
  131. entries = self.libraries.find(self.name, sort_results=True)
  132. for entry in entries:
  133. # Unpack entry - now returns (path, library_name, needs_qualification)
  134. template_dir = entry[0]
  135. library_name = entry[1]
  136. needs_qualification = entry[2] if len(entry) > 2 else False
  137. try:
  138. # Get library object to determine type
  139. library = next(
  140. (
  141. lib
  142. for lib in self.libraries.libraries
  143. if lib.name == library_name
  144. ),
  145. None,
  146. )
  147. library_type = library.library_type if library else "git"
  148. template = Template(
  149. template_dir, library_name=library_name, library_type=library_type
  150. )
  151. # Validate schema version compatibility
  152. template._validate_schema_version(self.schema_version, self.name)
  153. # If template ID needs qualification, set qualified ID
  154. if needs_qualification:
  155. template.set_qualified_id()
  156. templates.append(template)
  157. except Exception as exc:
  158. logger.error(f"Failed to load template from {template_dir}: {exc}")
  159. continue
  160. # Apply search filtering
  161. filtered_templates = [t for t in templates if query.lower() in t.id.lower()]
  162. if filtered_templates:
  163. logger.info(
  164. f"Found {len(filtered_templates)} templates matching '{query}' for module '{self.name}'"
  165. )
  166. self.display.display_templates_table(
  167. filtered_templates,
  168. self.name,
  169. f"{self.name.capitalize()} templates matching '{query}'",
  170. )
  171. else:
  172. logger.info(
  173. f"No templates found matching '{query}' for module '{self.name}'"
  174. )
  175. self.display.display_warning(
  176. f"No templates found matching '{query}'",
  177. context=f"module '{self.name}'",
  178. )
  179. return filtered_templates
  180. def show(
  181. self,
  182. id: str,
  183. all_vars: bool = Option(
  184. False,
  185. "--all",
  186. help="Show all variables/sections, even those with unsatisfied needs",
  187. ),
  188. ) -> None:
  189. """Show template details."""
  190. logger.debug(f"Showing template '{id}' from module '{self.name}'")
  191. template = self._load_template_by_id(id)
  192. if not template:
  193. self.display.display_error(
  194. f"Template '{id}' not found", context=f"module '{self.name}'"
  195. )
  196. return
  197. # Apply config defaults (same as in generate)
  198. # This ensures the display shows the actual defaults that will be used
  199. if template.variables:
  200. from .config import ConfigManager
  201. config = ConfigManager()
  202. config_defaults = config.get_defaults(self.name)
  203. if config_defaults:
  204. logger.debug(f"Loading config defaults for module '{self.name}'")
  205. # Apply config defaults (this respects the variable types and validation)
  206. successful = template.variables.apply_defaults(
  207. config_defaults, "config"
  208. )
  209. if successful:
  210. logger.debug(
  211. f"Applied config defaults for: {', '.join(successful)}"
  212. )
  213. # Re-sort sections after applying config (toggle values may have changed)
  214. template.variables.sort_sections()
  215. self._display_template_details(template, id, show_all=all_vars)
  216. def _apply_variable_defaults(self, template: Template) -> None:
  217. """Apply config defaults and CLI overrides to template variables.
  218. Args:
  219. template: Template instance with variables to configure
  220. """
  221. if not template.variables:
  222. return
  223. from .config import ConfigManager
  224. config = ConfigManager()
  225. config_defaults = config.get_defaults(self.name)
  226. if config_defaults:
  227. logger.info(f"Loading config defaults for module '{self.name}'")
  228. successful = template.variables.apply_defaults(config_defaults, "config")
  229. if successful:
  230. logger.debug(f"Applied config defaults for: {', '.join(successful)}")
  231. def _apply_cli_overrides(
  232. self, template: Template, var: Optional[List[str]], ctx=None
  233. ) -> None:
  234. """Apply CLI variable overrides to template.
  235. Args:
  236. template: Template instance to apply overrides to
  237. var: List of variable override strings from --var flags
  238. ctx: Context object containing extra args (optional, will get current context if None)
  239. """
  240. if not template.variables:
  241. return
  242. # Get context if not provided (compatible with all Typer versions)
  243. if ctx is None:
  244. import click
  245. try:
  246. ctx = click.get_current_context()
  247. except RuntimeError:
  248. ctx = None
  249. extra_args = list(ctx.args) if ctx and hasattr(ctx, "args") else []
  250. cli_overrides = parse_var_inputs(var or [], extra_args)
  251. if cli_overrides:
  252. logger.info(f"Received {len(cli_overrides)} variable overrides from CLI")
  253. successful_overrides = template.variables.apply_defaults(
  254. cli_overrides, "cli"
  255. )
  256. if successful_overrides:
  257. logger.debug(
  258. f"Applied CLI overrides for: {', '.join(successful_overrides)}"
  259. )
  260. def _collect_variable_values(
  261. self, template: Template, interactive: bool
  262. ) -> Dict[str, Any]:
  263. """Collect variable values from user prompts and template defaults.
  264. Args:
  265. template: Template instance with variables
  266. interactive: Whether to prompt user for values interactively
  267. Returns:
  268. Dictionary of variable names to values
  269. """
  270. variable_values = {}
  271. # Collect values interactively if enabled
  272. if interactive and template.variables:
  273. prompt_handler = PromptHandler()
  274. collected_values = prompt_handler.collect_variables(template.variables)
  275. if collected_values:
  276. variable_values.update(collected_values)
  277. logger.info(
  278. f"Collected {len(collected_values)} variable values from user input"
  279. )
  280. # Add satisfied variable values (respects dependencies and toggles)
  281. if template.variables:
  282. variable_values.update(template.variables.get_satisfied_values())
  283. return variable_values
  284. def _check_output_directory(
  285. self, output_dir: Path, rendered_files: Dict[str, str], interactive: bool
  286. ) -> Optional[List[Path]]:
  287. """Check output directory for conflicts and get user confirmation if needed.
  288. Args:
  289. output_dir: Directory where files will be written
  290. rendered_files: Dictionary of file paths to rendered content
  291. interactive: Whether to prompt user for confirmation
  292. Returns:
  293. List of existing files that will be overwritten, or None to cancel
  294. """
  295. dir_exists = output_dir.exists()
  296. dir_not_empty = dir_exists and any(output_dir.iterdir())
  297. # Check which files already exist
  298. existing_files = []
  299. if dir_exists:
  300. for file_path in rendered_files.keys():
  301. full_path = output_dir / file_path
  302. if full_path.exists():
  303. existing_files.append(full_path)
  304. # Warn if directory is not empty
  305. if dir_not_empty:
  306. if interactive:
  307. details = []
  308. if existing_files:
  309. details.append(
  310. f"{len(existing_files)} file(s) will be overwritten."
  311. )
  312. if not self.display.display_warning_with_confirmation(
  313. f"Directory '{output_dir}' is not empty.",
  314. details if details else None,
  315. default=False,
  316. ):
  317. self.display.display_info("Generation cancelled")
  318. return None
  319. else:
  320. # Non-interactive mode: show warning but continue
  321. logger.warning(f"Directory '{output_dir}' is not empty")
  322. if existing_files:
  323. logger.warning(f"{len(existing_files)} file(s) will be overwritten")
  324. return existing_files
  325. def _get_generation_confirmation(
  326. self,
  327. output_dir: Path,
  328. rendered_files: Dict[str, str],
  329. existing_files: Optional[List[Path]],
  330. dir_not_empty: bool,
  331. dry_run: bool,
  332. interactive: bool,
  333. ) -> bool:
  334. """Display file generation confirmation and get user approval.
  335. Args:
  336. output_dir: Output directory path
  337. rendered_files: Dictionary of file paths to content
  338. existing_files: List of existing files that will be overwritten
  339. dir_not_empty: Whether output directory already contains files
  340. dry_run: Whether this is a dry run
  341. interactive: Whether to prompt for confirmation
  342. Returns:
  343. True if user confirms generation, False to cancel
  344. """
  345. if not interactive:
  346. return True
  347. self.display.display_file_generation_confirmation(
  348. output_dir, rendered_files, existing_files if existing_files else None
  349. )
  350. # Final confirmation (only if we didn't already ask about overwriting)
  351. if not dir_not_empty and not dry_run:
  352. if not Confirm.ask("Generate these files?", default=True):
  353. self.display.display_info("Generation cancelled")
  354. return False
  355. return True
  356. def _execute_dry_run(
  357. self,
  358. id: str,
  359. output_dir: Path,
  360. rendered_files: Dict[str, str],
  361. show_files: bool,
  362. ) -> None:
  363. """Execute dry run mode with comprehensive simulation.
  364. Simulates all filesystem operations that would occur during actual generation,
  365. including directory creation, file writing, and permission checks.
  366. Args:
  367. id: Template ID
  368. output_dir: Directory where files would be written
  369. rendered_files: Dictionary of file paths to rendered content
  370. show_files: Whether to display file contents
  371. """
  372. import os
  373. console.print()
  374. console.print(
  375. "[bold cyan]Dry Run Mode - Simulating File Generation[/bold cyan]"
  376. )
  377. console.print()
  378. # Simulate directory creation
  379. self.display.display_heading("Directory Operations", icon_type="folder")
  380. # Check if output directory exists
  381. if output_dir.exists():
  382. self.display.display_success(
  383. f"Output directory exists: [cyan]{output_dir}[/cyan]"
  384. )
  385. # Check if we have write permissions
  386. if os.access(output_dir, os.W_OK):
  387. self.display.display_success("Write permission verified")
  388. else:
  389. self.display.display_warning("Write permission may be denied")
  390. else:
  391. console.print(
  392. f" [dim]→[/dim] Would create output directory: [cyan]{output_dir}[/cyan]"
  393. )
  394. # Check if parent directory exists and is writable
  395. parent = output_dir.parent
  396. if parent.exists() and os.access(parent, os.W_OK):
  397. self.display.display_success("Parent directory writable")
  398. else:
  399. self.display.display_warning("Parent directory may not be writable")
  400. # Collect unique subdirectories that would be created
  401. subdirs = set()
  402. for file_path in rendered_files.keys():
  403. parts = Path(file_path).parts
  404. for i in range(1, len(parts)):
  405. subdirs.add(Path(*parts[:i]))
  406. if subdirs:
  407. console.print(
  408. f" [dim]→[/dim] Would create {len(subdirs)} subdirectory(ies)"
  409. )
  410. for subdir in sorted(subdirs):
  411. console.print(f" [dim]📁[/dim] {subdir}/")
  412. console.print()
  413. # Display file operations in a table
  414. self.display.display_heading("File Operations", icon_type="file")
  415. total_size = 0
  416. new_files = 0
  417. overwrite_files = 0
  418. file_operations = []
  419. for file_path, content in sorted(rendered_files.items()):
  420. full_path = output_dir / file_path
  421. file_size = len(content.encode("utf-8"))
  422. total_size += file_size
  423. # Determine status
  424. if full_path.exists():
  425. status = "Overwrite"
  426. overwrite_files += 1
  427. else:
  428. status = "Create"
  429. new_files += 1
  430. file_operations.append((file_path, file_size, status))
  431. self.display.display_file_operation_table(file_operations)
  432. console.print()
  433. # Summary statistics
  434. if total_size < 1024:
  435. size_str = f"{total_size}B"
  436. elif total_size < 1024 * 1024:
  437. size_str = f"{total_size / 1024:.1f}KB"
  438. else:
  439. size_str = f"{total_size / (1024 * 1024):.1f}MB"
  440. summary_items = {
  441. "Total files:": str(len(rendered_files)),
  442. "New files:": str(new_files),
  443. "Files to overwrite:": str(overwrite_files),
  444. "Total size:": size_str,
  445. }
  446. self.display.display_summary_table("Summary", summary_items)
  447. console.print()
  448. # Show file contents if requested
  449. if show_files:
  450. console.print("[bold cyan]Generated File Contents:[/bold cyan]")
  451. console.print()
  452. for file_path, content in sorted(rendered_files.items()):
  453. console.print(f"[cyan]File:[/cyan] {file_path}")
  454. print(f"{'─' * 80}")
  455. print(content)
  456. print() # Add blank line after content
  457. console.print()
  458. self.display.display_success("Dry run complete - no files were written")
  459. console.print(f"[dim]Files would have been generated in '{output_dir}'[/dim]")
  460. logger.info(
  461. f"Dry run completed for template '{id}' - {len(rendered_files)} files, {total_size} bytes"
  462. )
  463. def _write_generated_files(
  464. self, output_dir: Path, rendered_files: Dict[str, str], quiet: bool = False
  465. ) -> None:
  466. """Write rendered files to the output directory.
  467. Args:
  468. output_dir: Directory to write files to
  469. rendered_files: Dictionary of file paths to rendered content
  470. quiet: Suppress output messages
  471. """
  472. output_dir.mkdir(parents=True, exist_ok=True)
  473. for file_path, content in rendered_files.items():
  474. full_path = output_dir / file_path
  475. full_path.parent.mkdir(parents=True, exist_ok=True)
  476. with open(full_path, "w", encoding="utf-8") as f:
  477. f.write(content)
  478. if not quiet:
  479. console.print(
  480. f"[green]Generated file: {file_path}[/green]"
  481. ) # Keep simple per-file output
  482. if not quiet:
  483. self.display.display_success(
  484. f"Template generated successfully in '{output_dir}'"
  485. )
  486. logger.info(f"Template written to directory: {output_dir}")
  487. def generate(
  488. self,
  489. id: str = Argument(..., help="Template ID"),
  490. directory: Optional[str] = Argument(
  491. None, help="Output directory (defaults to template ID)"
  492. ),
  493. interactive: bool = Option(
  494. True,
  495. "--interactive/--no-interactive",
  496. "-i/-n",
  497. help="Enable interactive prompting for variables",
  498. ),
  499. var: Optional[list[str]] = Option(
  500. None,
  501. "--var",
  502. "-v",
  503. help="Variable override (repeatable). Supports: KEY=VALUE or KEY VALUE",
  504. ),
  505. dry_run: bool = Option(
  506. False, "--dry-run", help="Preview template generation without writing files"
  507. ),
  508. show_files: bool = Option(
  509. False,
  510. "--show-files",
  511. help="Display generated file contents in plain text (use with --dry-run)",
  512. ),
  513. quiet: bool = Option(
  514. False, "--quiet", "-q", help="Suppress all non-error output"
  515. ),
  516. all_vars: bool = Option(
  517. False,
  518. "--all",
  519. help="Show all variables/sections, even those with unsatisfied needs",
  520. ),
  521. ) -> None:
  522. """Generate from template.
  523. Variable precedence chain (lowest to highest):
  524. 1. Module spec (defined in cli/modules/*.py)
  525. 2. Template spec (from template.yaml)
  526. 3. Config defaults (from ~/.config/boilerplates/config.yaml)
  527. 4. CLI overrides (--var flags)
  528. Examples:
  529. # Generate to directory named after template
  530. cli compose generate traefik
  531. # Generate to custom directory
  532. cli compose generate traefik my-proxy
  533. # Generate with variables
  534. cli compose generate traefik --var traefik_enabled=false
  535. # Preview without writing files (dry run)
  536. cli compose generate traefik --dry-run
  537. # Preview and show generated file contents
  538. cli compose generate traefik --dry-run --show-files
  539. """
  540. logger.info(
  541. f"Starting generation for template '{id}' from module '{self.name}'"
  542. )
  543. # Create a display manager with quiet mode if needed
  544. display = DisplayManager(quiet=quiet) if quiet else self.display
  545. template = self._load_template_by_id(id)
  546. # Apply defaults and overrides
  547. self._apply_variable_defaults(template)
  548. self._apply_cli_overrides(template, var)
  549. # Re-sort sections after all overrides (toggle values may have changed)
  550. if template.variables:
  551. template.variables.sort_sections()
  552. if not quiet:
  553. self._display_template_details(template, id, show_all=all_vars)
  554. console.print()
  555. # Collect variable values
  556. variable_values = self._collect_variable_values(template, interactive)
  557. try:
  558. # Validate and render template
  559. if template.variables:
  560. template.variables.validate_all()
  561. # Check if we're in debug mode (logger level is DEBUG)
  562. debug_mode = logger.isEnabledFor(logging.DEBUG)
  563. rendered_files, variable_values = template.render(
  564. template.variables, debug=debug_mode
  565. )
  566. if not rendered_files:
  567. display.display_error(
  568. "Template rendering returned no files",
  569. context="template generation",
  570. )
  571. raise Exit(code=1)
  572. logger.info(f"Successfully rendered template '{id}'")
  573. # Determine output directory
  574. if directory:
  575. output_dir = Path(directory)
  576. # Check if path looks like an absolute path but is missing the leading slash
  577. # This handles cases like "Users/username/path" which should be "/Users/username/path"
  578. if not output_dir.is_absolute() and str(output_dir).startswith(
  579. ("Users/", "home/", "usr/", "opt/", "var/", "tmp/")
  580. ):
  581. output_dir = Path("/") / output_dir
  582. logger.debug(
  583. f"Normalized relative-looking absolute path to: {output_dir}"
  584. )
  585. else:
  586. output_dir = Path(id)
  587. # Check for conflicts and get confirmation (skip in quiet mode)
  588. if not quiet:
  589. existing_files = self._check_output_directory(
  590. output_dir, rendered_files, interactive
  591. )
  592. if existing_files is None:
  593. return # User cancelled
  594. # Get final confirmation for generation
  595. dir_not_empty = output_dir.exists() and any(output_dir.iterdir())
  596. if not self._get_generation_confirmation(
  597. output_dir,
  598. rendered_files,
  599. existing_files,
  600. dir_not_empty,
  601. dry_run,
  602. interactive,
  603. ):
  604. return # User cancelled
  605. else:
  606. # In quiet mode, just check for existing files without prompts
  607. existing_files = []
  608. # Execute generation (dry run or actual)
  609. if dry_run:
  610. if not quiet:
  611. self._execute_dry_run(id, output_dir, rendered_files, show_files)
  612. else:
  613. self._write_generated_files(output_dir, rendered_files, quiet=quiet)
  614. # Display next steps (not in quiet mode)
  615. if template.metadata.next_steps and not quiet:
  616. display.display_next_steps(
  617. template.metadata.next_steps, variable_values
  618. )
  619. except TemplateRenderError as e:
  620. # Display enhanced error information for template rendering errors (always show errors)
  621. display.display_template_render_error(e, context=f"template '{id}'")
  622. raise Exit(code=1)
  623. except Exception as e:
  624. display.display_error(str(e), context=f"generating template '{id}'")
  625. raise Exit(code=1)
  626. def config_get(
  627. self,
  628. var_name: Optional[str] = Argument(
  629. None, help="Variable name to get (omit to show all defaults)"
  630. ),
  631. ) -> None:
  632. """Get default value(s) for this module.
  633. Examples:
  634. # Get all defaults for module
  635. cli compose defaults get
  636. # Get specific variable default
  637. cli compose defaults get service_name
  638. """
  639. from .config import ConfigManager
  640. config = ConfigManager()
  641. if var_name:
  642. # Get specific variable default
  643. value = config.get_default_value(self.name, var_name)
  644. if value is not None:
  645. console.print(f"[green]{var_name}[/green] = [yellow]{value}[/yellow]")
  646. else:
  647. self.display.display_warning(
  648. f"No default set for variable '{var_name}'",
  649. context=f"module '{self.name}'",
  650. )
  651. else:
  652. # Show all defaults (flat list)
  653. defaults = config.get_defaults(self.name)
  654. if defaults:
  655. console.print(
  656. f"[bold]Config defaults for module '{self.name}':[/bold]\n"
  657. )
  658. for var_name, var_value in defaults.items():
  659. console.print(
  660. f" [green]{var_name}[/green] = [yellow]{var_value}[/yellow]"
  661. )
  662. else:
  663. console.print(
  664. f"[yellow]No defaults configured for module '{self.name}'[/yellow]"
  665. )
  666. def config_set(
  667. self,
  668. var_name: str = Argument(..., help="Variable name or var=value format"),
  669. value: Optional[str] = Argument(
  670. None, help="Default value (not needed if using var=value format)"
  671. ),
  672. ) -> None:
  673. """Set a default value for a variable.
  674. This only sets the DEFAULT VALUE, not the variable spec.
  675. The variable must be defined in the module or template spec.
  676. Supports both formats:
  677. - var_name value
  678. - var_name=value
  679. Examples:
  680. # Set default value (format 1)
  681. cli compose defaults set service_name my-awesome-app
  682. # Set default value (format 2)
  683. cli compose defaults set service_name=my-awesome-app
  684. # Set author for all compose templates
  685. cli compose defaults set author "Christian Lempa"
  686. """
  687. from .config import ConfigManager
  688. config = ConfigManager()
  689. # Parse var_name and value - support both "var value" and "var=value" formats
  690. if "=" in var_name and value is None:
  691. # Format: var_name=value
  692. parts = var_name.split("=", 1)
  693. actual_var_name = parts[0]
  694. actual_value = parts[1]
  695. elif value is not None:
  696. # Format: var_name value
  697. actual_var_name = var_name
  698. actual_value = value
  699. else:
  700. self.display.display_error(
  701. f"Missing value for variable '{var_name}'", context="config set"
  702. )
  703. console.print(
  704. "[dim]Usage: defaults set VAR_NAME VALUE or defaults set VAR_NAME=VALUE[/dim]"
  705. )
  706. raise Exit(code=1)
  707. # Set the default value
  708. config.set_default_value(self.name, actual_var_name, actual_value)
  709. self.display.display_success(
  710. f"Set default: [cyan]{actual_var_name}[/cyan] = [yellow]{actual_value}[/yellow]"
  711. )
  712. console.print(
  713. "\n[dim]This will be used as the default value when generating templates with this module.[/dim]"
  714. )
  715. def config_remove(
  716. self,
  717. var_name: str = Argument(..., help="Variable name to remove"),
  718. ) -> None:
  719. """Remove a specific default variable value.
  720. Examples:
  721. # Remove a default value
  722. cli compose defaults rm service_name
  723. """
  724. from .config import ConfigManager
  725. config = ConfigManager()
  726. defaults = config.get_defaults(self.name)
  727. if not defaults:
  728. console.print(
  729. f"[yellow]No defaults configured for module '{self.name}'[/yellow]"
  730. )
  731. return
  732. if var_name in defaults:
  733. del defaults[var_name]
  734. config.set_defaults(self.name, defaults)
  735. self.display.display_success(f"Removed default for '{var_name}'")
  736. else:
  737. self.display.display_error(f"No default found for variable '{var_name}'")
  738. def config_clear(
  739. self,
  740. var_name: Optional[str] = Argument(
  741. None, help="Variable name to clear (omit to clear all defaults)"
  742. ),
  743. force: bool = Option(False, "--force", "-f", help="Skip confirmation prompt"),
  744. ) -> None:
  745. """Clear default value(s) for this module.
  746. Examples:
  747. # Clear specific variable default
  748. cli compose defaults clear service_name
  749. # Clear all defaults for module
  750. cli compose defaults clear --force
  751. """
  752. from .config import ConfigManager
  753. config = ConfigManager()
  754. defaults = config.get_defaults(self.name)
  755. if not defaults:
  756. console.print(
  757. f"[yellow]No defaults configured for module '{self.name}'[/yellow]"
  758. )
  759. return
  760. if var_name:
  761. # Clear specific variable
  762. if var_name in defaults:
  763. del defaults[var_name]
  764. config.set_defaults(self.name, defaults)
  765. self.display.display_success(f"Cleared default for '{var_name}'")
  766. else:
  767. self.display.display_error(
  768. f"No default found for variable '{var_name}'"
  769. )
  770. else:
  771. # Clear all defaults
  772. if not force:
  773. detail_lines = [
  774. f"This will clear ALL defaults for module '{self.name}':",
  775. "",
  776. ]
  777. for var_name, var_value in defaults.items():
  778. detail_lines.append(
  779. f" [green]{var_name}[/green] = [yellow]{var_value}[/yellow]"
  780. )
  781. self.display.display_warning("Warning: This will clear ALL defaults")
  782. console.print()
  783. for line in detail_lines:
  784. console.print(line)
  785. console.print()
  786. if not Confirm.ask("[bold red]Are you sure?[/bold red]", default=False):
  787. console.print("[green]Operation cancelled.[/green]")
  788. return
  789. config.clear_defaults(self.name)
  790. self.display.display_success(
  791. f"Cleared all defaults for module '{self.name}'"
  792. )
  793. def config_list(self) -> None:
  794. """Display the defaults for this specific module in YAML format.
  795. Examples:
  796. # Show the defaults for the current module
  797. cli compose defaults list
  798. """
  799. from .config import ConfigManager
  800. import yaml
  801. config = ConfigManager()
  802. # Get only the defaults for this module
  803. defaults = config.get_defaults(self.name)
  804. if not defaults:
  805. console.print(
  806. f"[yellow]No configuration found for module '{self.name}'[/yellow]"
  807. )
  808. console.print(
  809. f"\n[dim]Config file location: {config.get_config_path()}[/dim]"
  810. )
  811. return
  812. # Create a minimal config structure with only this module's defaults
  813. module_config = {"defaults": {self.name: defaults}}
  814. # Convert config to YAML string
  815. yaml_output = yaml.dump(
  816. module_config, default_flow_style=False, sort_keys=False
  817. )
  818. console.print(
  819. f"[bold]Configuration for module:[/bold] [cyan]{self.name}[/cyan]"
  820. )
  821. console.print(f"[dim]Config file: {config.get_config_path()}[/dim]\n")
  822. console.print(
  823. Panel(
  824. yaml_output,
  825. title=f"{self.name.capitalize()} Config",
  826. border_style="blue",
  827. )
  828. )
  829. def validate(
  830. self,
  831. template_id: str = Argument(
  832. None, help="Template ID to validate (if omitted, validates all templates)"
  833. ),
  834. path: Optional[str] = Option(
  835. None,
  836. "--path",
  837. "-p",
  838. help="Validate a template from a specific directory path",
  839. ),
  840. verbose: bool = Option(
  841. False, "--verbose", "-v", help="Show detailed validation information"
  842. ),
  843. semantic: bool = Option(
  844. True,
  845. "--semantic/--no-semantic",
  846. help="Enable semantic validation (Docker Compose schema, etc.)",
  847. ),
  848. ) -> None:
  849. """Validate templates for Jinja2 syntax, undefined variables, and semantic correctness.
  850. Validation includes:
  851. - Jinja2 syntax checking
  852. - Variable definition checking
  853. - Semantic validation (when --semantic is enabled):
  854. - Docker Compose file structure
  855. - YAML syntax
  856. - Configuration best practices
  857. Examples:
  858. # Validate all templates in this module
  859. cli compose validate
  860. # Validate a specific template
  861. cli compose validate gitlab
  862. # Validate a template from a specific path
  863. cli compose validate --path /path/to/template
  864. # Validate with verbose output
  865. cli compose validate --verbose
  866. # Skip semantic validation (only Jinja2)
  867. cli compose validate --no-semantic
  868. """
  869. from .validators import get_validator_registry
  870. # Validate from path takes precedence
  871. if path:
  872. try:
  873. template_path = Path(path).resolve()
  874. if not template_path.exists():
  875. self.display.display_error(f"Path does not exist: {path}")
  876. raise Exit(code=1)
  877. if not template_path.is_dir():
  878. self.display.display_error(f"Path is not a directory: {path}")
  879. raise Exit(code=1)
  880. console.print(
  881. f"[bold]Validating template from path:[/bold] [cyan]{template_path}[/cyan]\n"
  882. )
  883. template = Template(template_path, library_name="local")
  884. template_id = template.id
  885. except Exception as e:
  886. self.display.display_error(
  887. f"Failed to load template from path '{path}': {e}"
  888. )
  889. raise Exit(code=1)
  890. elif template_id:
  891. # Validate a specific template by ID
  892. try:
  893. template = self._load_template_by_id(template_id)
  894. console.print(
  895. f"[bold]Validating template:[/bold] [cyan]{template_id}[/cyan]\n"
  896. )
  897. except Exception as e:
  898. self.display.display_error(
  899. f"Failed to load template '{template_id}': {e}"
  900. )
  901. raise Exit(code=1)
  902. else:
  903. # Validate all templates - handled separately below
  904. template = None
  905. # Single template validation
  906. if template:
  907. try:
  908. # Trigger validation by accessing used_variables
  909. _ = template.used_variables
  910. # Trigger variable definition validation by accessing variables
  911. _ = template.variables
  912. self.display.display_success("Jinja2 validation passed")
  913. # Semantic validation
  914. if semantic:
  915. console.print(
  916. "\n[bold cyan]Running semantic validation...[/bold cyan]"
  917. )
  918. registry = get_validator_registry()
  919. has_semantic_errors = False
  920. # Render template with default values for validation
  921. debug_mode = logger.isEnabledFor(logging.DEBUG)
  922. rendered_files, _ = template.render(
  923. template.variables, debug=debug_mode
  924. )
  925. for file_path, content in rendered_files.items():
  926. result = registry.validate_file(content, file_path)
  927. if (
  928. result.errors
  929. or result.warnings
  930. or (verbose and result.info)
  931. ):
  932. console.print(f"\n[cyan]File:[/cyan] {file_path}")
  933. result.display(f"{file_path}")
  934. if result.errors:
  935. has_semantic_errors = True
  936. if not has_semantic_errors:
  937. self.display.display_success("Semantic validation passed")
  938. else:
  939. self.display.display_error("Semantic validation found errors")
  940. raise Exit(code=1)
  941. if verbose:
  942. console.print(
  943. f"\n[dim]Template path: {template.template_dir}[/dim]"
  944. )
  945. console.print(
  946. f"[dim]Found {len(template.used_variables)} variables[/dim]"
  947. )
  948. if semantic:
  949. console.print(
  950. f"[dim]Generated {len(rendered_files)} files[/dim]"
  951. )
  952. except TemplateRenderError as e:
  953. # Display enhanced error information for template rendering errors
  954. self.display.display_template_render_error(
  955. e, context=f"template '{template_id}'"
  956. )
  957. raise Exit(code=1)
  958. except (TemplateSyntaxError, TemplateValidationError, ValueError) as e:
  959. self.display.display_error(f"Validation failed for '{template_id}':")
  960. console.print(f"\n{e}")
  961. raise Exit(code=1)
  962. except Exception as e:
  963. self.display.display_error(
  964. f"Unexpected error validating '{template_id}': {e}"
  965. )
  966. raise Exit(code=1)
  967. return
  968. else:
  969. # Validate all templates
  970. console.print(f"[bold]Validating all {self.name} templates...[/bold]\n")
  971. entries = self.libraries.find(self.name, sort_results=True)
  972. total = len(entries)
  973. valid_count = 0
  974. invalid_count = 0
  975. errors = []
  976. for template_dir, library_name in entries:
  977. template_id = template_dir.name
  978. try:
  979. template = Template(template_dir, library_name=library_name)
  980. # Trigger validation
  981. _ = template.used_variables
  982. _ = template.variables
  983. valid_count += 1
  984. if verbose:
  985. self.display.display_success(template_id)
  986. except ValueError as e:
  987. invalid_count += 1
  988. errors.append((template_id, str(e)))
  989. if verbose:
  990. self.display.display_error(template_id)
  991. except Exception as e:
  992. invalid_count += 1
  993. errors.append((template_id, f"Load error: {e}"))
  994. if verbose:
  995. self.display.display_warning(template_id)
  996. # Summary
  997. summary_items = {
  998. "Total templates:": str(total),
  999. "[green]Valid:[/green]": str(valid_count),
  1000. "[red]Invalid:[/red]": str(invalid_count),
  1001. }
  1002. self.display.display_summary_table("Validation Summary", summary_items)
  1003. # Show errors if any
  1004. if errors:
  1005. console.print("\n[bold red]Validation Errors:[/bold red]")
  1006. for template_id, error_msg in errors:
  1007. console.print(
  1008. f"\n[yellow]Template:[/yellow] [cyan]{template_id}[/cyan]"
  1009. )
  1010. console.print(f"[dim]{error_msg}[/dim]")
  1011. raise Exit(code=1)
  1012. else:
  1013. self.display.display_success("All templates are valid!")
  1014. @classmethod
  1015. def register_cli(cls, app: Typer) -> None:
  1016. """Register module commands with the main app."""
  1017. logger.debug(f"Registering CLI commands for module '{cls.name}'")
  1018. module_instance = cls()
  1019. module_app = Typer(help=cls.description)
  1020. module_app.command("list")(module_instance.list)
  1021. module_app.command("search")(module_instance.search)
  1022. module_app.command("show")(module_instance.show)
  1023. module_app.command("validate")(module_instance.validate)
  1024. module_app.command(
  1025. "generate",
  1026. context_settings={"allow_extra_args": True, "ignore_unknown_options": True},
  1027. )(module_instance.generate)
  1028. # Add defaults commands (simplified - only manage default values)
  1029. defaults_app = Typer(help="Manage default values for template variables")
  1030. defaults_app.command("get", help="Get default value(s)")(
  1031. module_instance.config_get
  1032. )
  1033. defaults_app.command("set", help="Set a default value")(
  1034. module_instance.config_set
  1035. )
  1036. defaults_app.command("rm", help="Remove a specific default value")(
  1037. module_instance.config_remove
  1038. )
  1039. defaults_app.command("clear", help="Clear default value(s)")(
  1040. module_instance.config_clear
  1041. )
  1042. defaults_app.command(
  1043. "list", help="Display the config for this module in YAML format"
  1044. )(module_instance.config_list)
  1045. module_app.add_typer(defaults_app, name="defaults")
  1046. app.add_typer(module_app, name=cls.name, help=cls.description)
  1047. logger.info(f"Module '{cls.name}' CLI commands registered")
  1048. def _load_template_by_id(self, id: str) -> Template:
  1049. """Load a template by its ID, supporting qualified IDs.
  1050. Supports both formats:
  1051. - Simple: "alloy" (uses priority system)
  1052. - Qualified: "alloy.default" (loads from specific library)
  1053. Args:
  1054. id: Template ID (simple or qualified)
  1055. Returns:
  1056. Template instance
  1057. Raises:
  1058. FileNotFoundError: If template is not found
  1059. """
  1060. logger.debug(f"Loading template with ID '{id}' from module '{self.name}'")
  1061. # find_by_id now handles both simple and qualified IDs
  1062. result = self.libraries.find_by_id(self.name, id)
  1063. if not result:
  1064. raise FileNotFoundError(
  1065. f"Template '{id}' not found in module '{self.name}'"
  1066. )
  1067. template_dir, library_name = result
  1068. # Get library type
  1069. library = next(
  1070. (lib for lib in self.libraries.libraries if lib.name == library_name), None
  1071. )
  1072. library_type = library.library_type if library else "git"
  1073. try:
  1074. template = Template(
  1075. template_dir, library_name=library_name, library_type=library_type
  1076. )
  1077. # Validate schema version compatibility
  1078. template._validate_schema_version(self.schema_version, self.name)
  1079. # If the original ID was qualified, preserve it
  1080. if "." in id:
  1081. template.id = id
  1082. return template
  1083. except Exception as exc:
  1084. logger.error(f"Failed to load template '{id}': {exc}")
  1085. raise FileNotFoundError(
  1086. f"Template '{id}' could not be loaded: {exc}"
  1087. ) from exc
  1088. def _display_template_details(
  1089. self, template: Template, id: str, show_all: bool = False
  1090. ) -> None:
  1091. """Display template information panel and variables table.
  1092. Args:
  1093. template: Template instance to display
  1094. id: Template ID
  1095. show_all: If True, show all variables/sections regardless of needs satisfaction
  1096. """
  1097. self.display.display_template_details(template, id, show_all=show_all)