module.py 47 KB

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