module.py 40 KB

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