base_module.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. generate_template,
  12. list_templates,
  13. search_templates,
  14. show_template,
  15. validate_templates,
  16. )
  17. from .config_commands import (
  18. config_clear,
  19. config_get,
  20. config_list,
  21. config_remove,
  22. config_set,
  23. )
  24. logger = logging.getLogger(__name__)
  25. # Expected length of library entry tuple: (path, library_name, needs_qualification)
  26. LIBRARY_ENTRY_MIN_LENGTH = 2
  27. class Module(ABC):
  28. """Streamlined base module that auto-detects variables from templates.
  29. Subclasses must define:
  30. - name: str (class attribute)
  31. - description: str (class attribute)
  32. """
  33. # Class attributes that must be defined by subclasses
  34. name: str
  35. description: str
  36. # Schema version supported by this module (override in subclasses)
  37. schema_version: str = "1.0"
  38. def __init__(self) -> None:
  39. # Validate required class attributes
  40. if not hasattr(self.__class__, 'name') or not hasattr(self.__class__, 'description'):
  41. raise TypeError(
  42. f"Module {self.__class__.__name__} must define 'name' and 'description' class attributes"
  43. )
  44. logger.info(f"Initializing module '{self.name}'")
  45. logger.debug(
  46. f"Module '{self.name}' configuration: description='{self.description}'"
  47. )
  48. self.libraries = LibraryManager()
  49. self.display = DisplayManager()
  50. def _load_all_templates(self, filter_fn=None) -> list:
  51. """Load all templates for this module with optional filtering."""
  52. templates = []
  53. entries = self.libraries.find(self.name, sort_results=True)
  54. for entry in entries:
  55. # Unpack entry - returns (path, library_name, needs_qualification)
  56. template_dir = entry[0]
  57. library_name = entry[1]
  58. needs_qualification = (
  59. entry[2] if len(entry) > LIBRARY_ENTRY_MIN_LENGTH else False
  60. )
  61. try:
  62. # Get library object to determine type
  63. library = next(
  64. (
  65. lib
  66. for lib in self.libraries.libraries
  67. if lib.name == library_name
  68. ),
  69. None,
  70. )
  71. library_type = library.library_type if library else "git"
  72. template = Template(
  73. template_dir, library_name=library_name, library_type=library_type
  74. )
  75. # Validate schema version compatibility
  76. template._validate_schema_version(self.schema_version, self.name)
  77. # If template ID needs qualification, set qualified ID
  78. if needs_qualification:
  79. template.set_qualified_id()
  80. # Apply filter if provided
  81. if filter_fn is None or filter_fn(template):
  82. templates.append(template)
  83. except Exception as exc:
  84. logger.error(f"Failed to load template from {template_dir}: {exc}")
  85. continue
  86. return templates
  87. def _load_template_by_id(self, id: str):
  88. """Load a template by its ID, supporting qualified IDs."""
  89. logger.debug(f"Loading template with ID '{id}' from module '{self.name}'")
  90. # find_by_id now handles both simple and qualified IDs
  91. result = self.libraries.find_by_id(self.name, id)
  92. if not result:
  93. raise FileNotFoundError(
  94. f"Template '{id}' not found in module '{self.name}'"
  95. )
  96. template_dir, library_name = result
  97. # Get library type
  98. library = next(
  99. (lib for lib in self.libraries.libraries if lib.name == library_name), None
  100. )
  101. library_type = library.library_type if library else "git"
  102. try:
  103. template = Template(
  104. template_dir, library_name=library_name, library_type=library_type
  105. )
  106. # Validate schema version compatibility
  107. template._validate_schema_version(self.schema_version, self.name)
  108. # If the original ID was qualified, preserve it
  109. if "." in id:
  110. template.id = id
  111. return template
  112. except Exception as exc:
  113. logger.error(f"Failed to load template '{id}': {exc}")
  114. raise FileNotFoundError(
  115. f"Template '{id}' could not be loaded: {exc}"
  116. ) from exc
  117. def list(
  118. self,
  119. raw: Annotated[
  120. bool, Option("--raw", help="Output raw list format instead of rich table")
  121. ] = False,
  122. ) -> list:
  123. """List all templates."""
  124. return list_templates(self, raw)
  125. def search(
  126. self,
  127. query: Annotated[str, Argument(help="Search string to filter templates by ID")],
  128. ) -> list:
  129. """Search for templates by ID containing the search string."""
  130. return search_templates(self, query)
  131. def show(self, id: str) -> None:
  132. """Show template details."""
  133. return show_template(self, id)
  134. def generate(
  135. self,
  136. id: Annotated[str, Argument(help="Template ID")],
  137. directory: Annotated[
  138. str | None, Argument(help="Output directory (defaults to template ID)")
  139. ] = None,
  140. interactive: Annotated[
  141. bool,
  142. Option(
  143. "--interactive/--no-interactive",
  144. "-i/-n",
  145. help="Enable interactive prompting for variables",
  146. ),
  147. ] = True,
  148. var: Annotated[
  149. list[str] | None,
  150. Option(
  151. "--var",
  152. "-v",
  153. help="Variable override (repeatable). Supports: KEY=VALUE or KEY VALUE",
  154. ),
  155. ] = None,
  156. var_file: Annotated[
  157. str | None,
  158. Option(
  159. "--var-file",
  160. "-f",
  161. help="Load variables from YAML file (overrides config defaults, overridden by --var)",
  162. ),
  163. ] = None,
  164. dry_run: Annotated[
  165. bool, Option("--dry-run", help="Preview template generation without writing files")
  166. ] = False,
  167. show_files: Annotated[
  168. bool,
  169. Option(
  170. "--show-files",
  171. help="Display generated file contents in plain text (use with --dry-run)",
  172. ),
  173. ] = False,
  174. quiet: Annotated[
  175. bool, Option("--quiet", "-q", help="Suppress all non-error output")
  176. ] = False,
  177. ) -> None:
  178. """Generate from template.
  179. Variable precedence chain (lowest to highest):
  180. 1. Module spec (defined in cli/modules/*.py)
  181. 2. Template spec (from template.yaml)
  182. 3. Config defaults (from ~/.config/boilerplates/config.yaml)
  183. 4. Variable file (from --var-file)
  184. 5. CLI overrides (--var flags)
  185. """
  186. return generate_template(
  187. self, id, directory, interactive, var, var_file, dry_run, show_files, quiet
  188. )
  189. def validate(
  190. self,
  191. template_id: str | None = None,
  192. path: str | None = None,
  193. verbose: Annotated[
  194. bool, Option("--verbose", "-v", help="Show detailed validation information")
  195. ] = False,
  196. semantic: Annotated[
  197. bool,
  198. Option(
  199. "--semantic/--no-semantic",
  200. help="Enable semantic validation (Docker Compose schema, etc.)",
  201. ),
  202. ] = True,
  203. ) -> None:
  204. """Validate templates for Jinja2 syntax, undefined variables, and semantic correctness."""
  205. return validate_templates(self, template_id, path, verbose, semantic)
  206. def config_get(
  207. self,
  208. var_name: str | None = None,
  209. ) -> None:
  210. """Get default value(s) for this module."""
  211. return config_get(self, var_name)
  212. def config_set(
  213. self,
  214. var_name: str,
  215. value: str | None = None,
  216. ) -> None:
  217. """Set a default value for a variable."""
  218. return config_set(self, var_name, value)
  219. def config_remove(
  220. self,
  221. var_name: Annotated[str, Argument(help="Variable name to remove")],
  222. ) -> None:
  223. """Remove a specific default variable value."""
  224. return config_remove(self, var_name)
  225. def config_clear(
  226. self,
  227. var_name: str | None = None,
  228. force: bool = False,
  229. ) -> None:
  230. """Clear default value(s) for this module."""
  231. return config_clear(self, var_name, force)
  232. def config_list(self) -> None:
  233. """Display the defaults for this specific module in YAML format."""
  234. return config_list(self)
  235. @classmethod
  236. def register_cli(cls, app: Typer) -> None:
  237. """Register module commands with the main app."""
  238. logger.debug(f"Registering CLI commands for module '{cls.name}'")
  239. module_instance = cls()
  240. module_app = Typer(help=cls.description)
  241. module_app.command("list")(module_instance.list)
  242. module_app.command("search")(module_instance.search)
  243. module_app.command("show")(module_instance.show)
  244. module_app.command("validate")(module_instance.validate)
  245. module_app.command(
  246. "generate",
  247. context_settings={"allow_extra_args": True, "ignore_unknown_options": True},
  248. )(module_instance.generate)
  249. # Add defaults commands (simplified - only manage default values)
  250. defaults_app = Typer(help="Manage default values for template variables")
  251. defaults_app.command("get", help="Get default value(s)")(
  252. module_instance.config_get
  253. )
  254. defaults_app.command("set", help="Set a default value")(
  255. module_instance.config_set
  256. )
  257. defaults_app.command("rm", help="Remove a specific default value")(
  258. module_instance.config_remove
  259. )
  260. defaults_app.command("clear", help="Clear default value(s)")(
  261. module_instance.config_clear
  262. )
  263. defaults_app.command(
  264. "list", help="Display the config for this module in YAML format"
  265. )(module_instance.config_list)
  266. module_app.add_typer(defaults_app, name="defaults")
  267. app.add_typer(module_app, name=cls.name, help=cls.description)
  268. logger.info(f"Module '{cls.name}' CLI commands registered")