base_module.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. """Base module class for template management."""
  2. from __future__ import annotations
  3. import logging
  4. from abc import ABC
  5. from typing import Annotated
  6. from typer import Argument, Option, Typer
  7. from ..display import DisplayManager
  8. from ..library import LibraryManager
  9. from ..template import Template
  10. from .base_commands import (
  11. GenerationConfig,
  12. ValidationConfig,
  13. generate_template,
  14. list_templates,
  15. search_templates,
  16. show_template,
  17. validate_templates,
  18. )
  19. from .config_commands import (
  20. config_clear,
  21. config_get,
  22. config_list,
  23. config_remove,
  24. config_set,
  25. )
  26. logger = logging.getLogger(__name__)
  27. # Expected length of library entry tuple: (path, library_name, needs_qualification)
  28. LIBRARY_ENTRY_MIN_LENGTH = 2
  29. class Module(ABC):
  30. """Streamlined base module that auto-detects variables from templates.
  31. Subclasses must define:
  32. - name: str (class attribute)
  33. - description: str (class attribute)
  34. """
  35. # Class attributes that must be defined by subclasses
  36. name: str
  37. description: str
  38. kind_validator_class = None
  39. def __init__(self) -> None:
  40. # Validate required class attributes
  41. if not hasattr(self.__class__, "name") or not hasattr(self.__class__, "description"):
  42. raise TypeError(f"Module {self.__class__.__name__} must define 'name' and 'description' class attributes")
  43. logger.info(f"Initializing module '{self.name}'")
  44. logger.debug(f"Module '{self.name}' configuration: description='{self.description}'")
  45. self.libraries = LibraryManager()
  46. self.display = DisplayManager()
  47. def _load_all_templates(self, filter_fn=None) -> list:
  48. """Load all templates for this module with optional filtering."""
  49. templates = []
  50. entries = self.libraries.find(self.name, sort_results=True)
  51. for entry in entries:
  52. # Unpack entry - returns (path, library_name, needs_qualification)
  53. template_dir = entry[0]
  54. library_name = entry[1]
  55. needs_qualification = entry[2] if len(entry) > LIBRARY_ENTRY_MIN_LENGTH else False
  56. try:
  57. # Get library object to determine type
  58. library = next(
  59. (lib for lib in self.libraries.libraries if lib.name == library_name),
  60. None,
  61. )
  62. library_type = library.library_type if library else "git"
  63. template = Template(template_dir, library_name=library_name, library_type=library_type)
  64. # If template ID needs qualification, set qualified ID
  65. if needs_qualification:
  66. template.set_qualified_id()
  67. # Apply filter if provided
  68. if filter_fn is None or filter_fn(template):
  69. templates.append(template)
  70. except Exception as exc:
  71. logger.error(f"Failed to load template from {template_dir}: {exc}")
  72. continue
  73. return templates
  74. def _load_template_by_id(self, id: str):
  75. """Load a template by its ID, supporting qualified IDs."""
  76. logger.debug(f"Loading template with ID '{id}' from module '{self.name}'")
  77. # find_by_id now handles both simple and qualified IDs
  78. result = self.libraries.find_by_id(self.name, id)
  79. if not result:
  80. raise FileNotFoundError(f"Template '{id}' not found in module '{self.name}'")
  81. template_dir, library_name = result
  82. # Get library type
  83. library = next((lib for lib in self.libraries.libraries if lib.name == library_name), None)
  84. library_type = library.library_type if library else "git"
  85. try:
  86. template = Template(template_dir, library_name=library_name, library_type=library_type)
  87. # If the original ID was qualified, preserve it
  88. if "." in id:
  89. template.id = id
  90. return template
  91. except Exception as exc:
  92. logger.error(f"Failed to load template '{id}': {exc}")
  93. raise FileNotFoundError(f"Template '{id}' could not be loaded: {exc}") from exc
  94. def list(
  95. self,
  96. raw: Annotated[bool, Option("--raw", help="Output raw list format instead of rich table")] = False,
  97. ) -> list:
  98. """List all templates."""
  99. return list_templates(self, raw)
  100. def search(
  101. self,
  102. query: Annotated[str, Argument(help="Search string to filter templates by ID")],
  103. ) -> list:
  104. """Search for templates by ID containing the search string."""
  105. return search_templates(self, query)
  106. def show(
  107. self,
  108. id: str,
  109. var: Annotated[
  110. list[str] | None,
  111. Option(
  112. "--var",
  113. "-v",
  114. help="Variable override (repeatable). Supports: KEY=VALUE or KEY VALUE",
  115. ),
  116. ] = None,
  117. var_file: Annotated[
  118. str | None,
  119. Option(
  120. "--var-file",
  121. "-f",
  122. help="Load variables from YAML file (overrides config defaults)",
  123. ),
  124. ] = None,
  125. ) -> None:
  126. """Show template details with optional variable overrides."""
  127. return show_template(self, id, var, var_file)
  128. def generate(
  129. self,
  130. id: Annotated[str, Argument(help="Template ID")],
  131. *,
  132. output: Annotated[
  133. str | None,
  134. Option(
  135. "--output",
  136. "-o",
  137. help="Local output directory",
  138. ),
  139. ] = None,
  140. remote: Annotated[
  141. str | None,
  142. Option(
  143. "--remote",
  144. "-r",
  145. help="Upload generated files to this SSH host instead of a local directory",
  146. ),
  147. ] = None,
  148. remote_path: Annotated[
  149. str | None,
  150. Option(
  151. "--remote-path",
  152. help="Remote target directory (defaults to ~/<slug> when --remote is used)",
  153. ),
  154. ] = None,
  155. interactive: Annotated[
  156. bool,
  157. Option(
  158. "--interactive/--no-interactive",
  159. help="Enable interactive prompting for variables",
  160. ),
  161. ] = True,
  162. name: Annotated[
  163. str | None,
  164. Option(
  165. "--name",
  166. "-n",
  167. help="Rename top-level generated files/directories with this name",
  168. ),
  169. ] = None,
  170. var: Annotated[
  171. list[str] | None,
  172. Option(
  173. "--var",
  174. "-v",
  175. help="Variable override (repeatable). Supports: KEY=VALUE or KEY VALUE",
  176. ),
  177. ] = None,
  178. var_file: Annotated[
  179. str | None,
  180. Option(
  181. "--var-file",
  182. "-f",
  183. help="Load variables from YAML file (overrides config defaults, overridden by --var)",
  184. ),
  185. ] = None,
  186. dry_run: Annotated[
  187. bool,
  188. Option("--dry-run", help="Preview template generation without writing files"),
  189. ] = False,
  190. show_files: Annotated[
  191. bool,
  192. Option(
  193. "--show-files",
  194. help="Display generated file contents in plain text (use with --dry-run)",
  195. ),
  196. ] = False,
  197. quiet: Annotated[bool, Option("--quiet", "-q", help="Suppress all non-error output")] = False,
  198. ) -> None:
  199. """Generate from template.
  200. Variable precedence chain (lowest to highest):
  201. 1. Template defaults (from template.json)
  202. 2. Config defaults (from ~/.config/boilerplates/config.yaml)
  203. 3. Variable file (from --var-file)
  204. 4. CLI overrides (--var flags)
  205. """
  206. config = GenerationConfig(
  207. id=id,
  208. output=output,
  209. remote=remote,
  210. remote_path=remote_path,
  211. interactive=interactive,
  212. var=var,
  213. var_file=var_file,
  214. name=name,
  215. dry_run=dry_run,
  216. show_files=show_files,
  217. quiet=quiet,
  218. )
  219. return generate_template(self, config)
  220. def validate(
  221. self,
  222. template_id: Annotated[
  223. str | None,
  224. Argument(help="Template ID to validate (omit to validate all templates)"),
  225. ] = None,
  226. *,
  227. path: Annotated[
  228. str | None,
  229. Option("--path", help="Path to template directory for validation"),
  230. ] = None,
  231. all_templates: Annotated[
  232. bool,
  233. Option("--all", help="Validate all templates in this module (default when no template ID is provided)"),
  234. ] = False,
  235. verbose: Annotated[bool, Option("--verbose", "-v", help="Show detailed validation information")] = False,
  236. semantic: Annotated[
  237. bool,
  238. Option(
  239. "--semantic/--no-semantic",
  240. help="Enable semantic validation for rendered files",
  241. ),
  242. ] = True,
  243. matrix: Annotated[
  244. bool,
  245. Option(
  246. "--matrix",
  247. help="Validate all reachable dependency states for a single template",
  248. ),
  249. ] = False,
  250. kind: Annotated[
  251. bool,
  252. Option(
  253. "--kind",
  254. help="Enable dependency-matrix kind-specific validation when available",
  255. ),
  256. ] = False,
  257. ) -> None:
  258. """Validate templates for syntax, rendered semantics, and optional dependency matrix checks.
  259. Examples:
  260. # Validate specific template
  261. cli terraform validate cloudflare-dns-record
  262. # Validate all templates
  263. cli terraform validate
  264. # Validate rendered semantic and kind-specific matrix cases
  265. cli terraform validate cloudflare-dns-record --matrix --kind
  266. """
  267. return validate_templates(
  268. self,
  269. template_id,
  270. path,
  271. ValidationConfig(
  272. verbose=verbose,
  273. semantic=semantic,
  274. matrix=matrix,
  275. kind=kind,
  276. all_templates=all_templates,
  277. kind_validator=self.kind_validator_class(verbose).validate_rendered_files
  278. if kind and self.kind_validator_class
  279. else None,
  280. ),
  281. )
  282. def config_get(
  283. self,
  284. var_name: str | None = None,
  285. ) -> None:
  286. """Get default value(s) for this module."""
  287. return config_get(self, var_name)
  288. def config_set(
  289. self,
  290. var_name: str,
  291. value: str | None = None,
  292. ) -> None:
  293. """Set a default value for a variable."""
  294. return config_set(self, var_name, value)
  295. def config_remove(
  296. self,
  297. var_name: Annotated[str, Argument(help="Variable name to remove")],
  298. ) -> None:
  299. """Remove a specific default variable value."""
  300. return config_remove(self, var_name)
  301. def config_clear(
  302. self,
  303. var_name: str | None = None,
  304. force: bool = False,
  305. ) -> None:
  306. """Clear default value(s) for this module."""
  307. return config_clear(self, var_name, force)
  308. def config_list(self) -> None:
  309. """Display the defaults for this specific module in YAML format."""
  310. return config_list(self)
  311. @classmethod
  312. def register_cli(cls, app: Typer) -> None:
  313. """Register module commands with the main app."""
  314. logger.debug(f"Registering CLI commands for module '{cls.name}'")
  315. module_instance = cls()
  316. module_app = Typer(help=cls.description)
  317. module_app.command("list")(module_instance.list)
  318. module_app.command("search")(module_instance.search)
  319. module_app.command("show")(module_instance.show)
  320. module_app.command("validate")(module_instance.validate)
  321. module_app.command(
  322. "generate",
  323. context_settings={"allow_extra_args": True, "ignore_unknown_options": True},
  324. )(module_instance.generate)
  325. # Add defaults commands (simplified - only manage default values)
  326. defaults_app = Typer(help="Manage default values for template variables")
  327. defaults_app.command("get", help="Get default value(s)")(module_instance.config_get)
  328. defaults_app.command("set", help="Set a default value")(module_instance.config_set)
  329. defaults_app.command("rm", help="Remove a specific default value")(module_instance.config_remove)
  330. defaults_app.command("clear", help="Clear default value(s)")(module_instance.config_clear)
  331. defaults_app.command("list", help="Display the config for this module in YAML format")(
  332. module_instance.config_list
  333. )
  334. module_app.add_typer(defaults_app, name="defaults")
  335. app.add_typer(
  336. module_app,
  337. name=cls.name,
  338. help=cls.description,
  339. rich_help_panel="Template Commands",
  340. )
  341. logger.info(f"Module '{cls.name}' CLI commands registered")