Răsfoiți Sursa

added prompt logic

xcad 5 luni în urmă
părinte
comite
54f3425cc0
4 a modificat fișierele cu 24 adăugiri și 71 ștergeri
  1. 1 10
      cli/core/module.py
  2. 21 58
      cli/core/prompt.py
  3. 0 1
      cli/modules/compose.py
  4. 2 2
      library/compose/nginx/compose.yaml

+ 1 - 10
cli/core/module.py

@@ -196,19 +196,16 @@ class Module(ABC):
 
   def generate(self, id: str = Argument(..., metavar="template", help="The template to generate from"), out: Optional[Path] = Option(None, "--out", "-o", help="Output file to save the generated template")):
     """Generate a new template with complex variable prompting logic"""
-    logger.info(f"Generating template '{id}' from module '{self.name}'")
-    
+
     # Find template by ID
     template = self.libraries.find_by_id(module_name=self.name, files=self.files, template_id=id)
     if not template:
-      logger.error(f"Template '{id}' not found")
       print(f"Template '{id}' not found.")
       return
 
     # Validate if the variables in the template are valid ones
     success, missing = self._validate_variables(template.vars)
     if not success:
-      logger.error(f"Template '{id}' has invalid variables: {missing}")
       print(f"Template '{id}' has invalid variables: {missing}")
       return
     
@@ -219,26 +216,20 @@ class Module(ABC):
       logger.debug(f"Variable processing completed with {len(final_variable_values)} variables")
       
     except KeyboardInterrupt:
-      logger.info("Template generation cancelled by user")
       print("\n[red]Template generation cancelled.[/red]")
       return
     except Exception as e:
-      logger.error(f"Error during prompting: {e}")
       print(f"Error during variable prompting: {e}")
       return
     
     # Step 7: Generate template with final variable values
-    logger.debug(f"Step 7: Generating template with final values")
     try:
       generated_content = template.render(final_variable_values)
-      logger.debug("Template rendered successfully")
     except Exception as e:
-      logger.error(f"Error rendering template: {e}")
       print(f"Error rendering template: {e}")
       return
     
     # Step 8: Output the generated content
-    logger.debug(f"Step 8: Outputting generated content")
     if out:
       try:
         out.parent.mkdir(parents=True, exist_ok=True)

+ 21 - 58
cli/core/prompt.py

@@ -29,9 +29,7 @@ class PromptHandler:
   def __call__(self) -> Dict[str, Any]:
     """Execute the complex prompting logic and return final variable values."""
     logger.debug(f"Starting advanced prompt handler with {len(self.variable_groups)} variable groups")
-    
-    self._show_welcome_message()
-    
+
     # Process each variable group with the complex logic
     for group_name, group_data in self.variable_groups.items():
       self._process_variable_group(group_name, group_data)
@@ -39,19 +37,6 @@ class PromptHandler:
     self._show_summary()
     return self.final_values
   
-  def _show_welcome_message(self):
-    """Display a welcome message for the template generation."""
-    welcome_text = Text("🚀 Template Generation", style="bold blue")
-    subtitle = Text("Configure variables for your template", style="dim")
-    
-    panel = Panel(
-      f"{welcome_text}\n{subtitle}",
-      box=box.ROUNDED,
-      padding=(1, 2)
-    )
-    self.console.print(panel)
-    self.console.print()
-  
   def _process_variable_group(self, group_name: str, group_data: Dict[str, Any]):
     """Process a single variable group with complex prompting logic.
     
@@ -61,16 +46,15 @@ class PromptHandler:
     3. If group is enabled → prompt for variables without values
     4. Ask if user wants to change existing variable values
     """
-    logger.debug(f"Processing variable group: {group_name}")
     
     variables = group_data.get('vars', {})
     if not variables:
-      logger.debug(f"Group {group_name} has no variables, skipping")
       return
-      
+
     # Show group header
-    self._show_group_header(group_name, group_data.get('description', ''))
-    
+    self.console.print(f"[bold cyan]{group_name.title()} Variables[/bold cyan]")
+    self.console.print()
+
     # Step 1: Check for variables with no default values (always prompt)
     vars_without_defaults = self._get_variables_without_defaults(variables)
     
@@ -83,13 +67,12 @@ class PromptHandler:
       default_value = self.resolved_defaults.get(var_name)
       self.final_values[var_name] = default_value
     
+    # When group is not enabled
     if not group_enabled:
-      logger.debug(f"Group {group_name} disabled by user, but defaults have been applied")
       return
       
     # Step 3: Prompt for required variables (those without defaults)
     if vars_without_defaults:
-      self.console.print(f"[bold red]Required variables for {group_name}:[/bold red]")
       for var_name in vars_without_defaults:
         var_data = variables[var_name]
         value = self._prompt_for_variable(var_name, var_data, required=True)
@@ -103,14 +86,6 @@ class PromptHandler:
     
     self.console.print()  # Add spacing between groups
   
-  def _show_group_header(self, group_name: str, description: str):
-    """Display a header for the variable group."""
-    header = f"[bold cyan]📦 {group_name.title()} Variables[/bold cyan]"
-    if description:
-      header += f"\n[dim]{description}[/dim]"
-    
-    self.console.print(Panel(header, box=box.SIMPLE, padding=(0, 1)))
-  
   def _get_variables_without_defaults(self, variables: Dict[str, Any]) -> List[str]:
     """Get list of variable names that have no default values."""
     return [
@@ -149,6 +124,7 @@ class PromptHandler:
         default=False
       )
     except (EOFError, KeyboardInterrupt):
+      # For optional group configuration, gracefully handle interruption
       logger.debug(f"User interrupted prompt for group {group_name}, defaulting to disabled")
       return False
   
@@ -203,8 +179,10 @@ class PromptHandler:
     
     # Build prompt message
     prompt_parts = [f"[bold]{var_name}[/bold]"]
+    if required:
+      prompt_parts.append("[red](Required)[/red]")
     if description:
-      prompt_parts.append(f"({description})")
+      prompt_parts.append(f"[dim]{description}[/dim]")
     
     prompt_message = " ".join(prompt_parts)
     
@@ -228,7 +206,7 @@ class PromptHandler:
         return self._prompt_string(prompt_message, current_value, required)
         
     except KeyboardInterrupt:
-      self.console.print("\n[red]Operation cancelled by user[/red]")
+      # Let KeyboardInterrupt propagate up to be handled at module level
       raise
     except Exception as e:
       logger.error(f"Error prompting for variable {var_name}: {e}")
@@ -249,11 +227,8 @@ class PromptHandler:
           
         return value.strip()
       except (EOFError, KeyboardInterrupt):
-        if required:
-          self.console.print(f"\n[red]This field is required. Using empty string.[/red]")
-          return ""
-        else:
-          return default_val or ""
+        # Let KeyboardInterrupt propagate up for proper cancellation
+        raise KeyboardInterrupt("Template generation cancelled by user")
   
   def _prompt_boolean(self, prompt_message: str, current_value: Any = None) -> bool:
     """Prompt for boolean input."""
@@ -261,7 +236,7 @@ class PromptHandler:
     try:
       return Confirm.ask(prompt_message, default=default_val)
     except (EOFError, KeyboardInterrupt):
-      return default_val if default_val is not None else False
+      raise KeyboardInterrupt("Template generation cancelled by user")
   
   def _prompt_integer(self, prompt_message: str, current_value: Any = None) -> int:
     """Prompt for integer input with validation."""
@@ -273,7 +248,7 @@ class PromptHandler:
       except ValueError:
         self.console.print("[red]Please enter a valid integer[/red]")
       except (EOFError, KeyboardInterrupt):
-        return default_val if default_val is not None else 0
+        raise KeyboardInterrupt("Template generation cancelled by user")
   
   def _prompt_float(self, prompt_message: str, current_value: Any = None) -> float:
     """Prompt for float input with validation."""
@@ -285,7 +260,7 @@ class PromptHandler:
       except ValueError:
         self.console.print("[red]Please enter a valid number[/red]")
       except (EOFError, KeyboardInterrupt):
-        return default_val if default_val is not None else 0.0
+        raise KeyboardInterrupt("Template generation cancelled by user")
   
   def _prompt_choice(self, prompt_message: str, options: List[Any], current_value: Any = None) -> Any:
     """Prompt for choice from a list of options."""
@@ -313,7 +288,7 @@ class PromptHandler:
             return matching_options[0]
           self.console.print(f"[red]Please enter a valid option number (1-{len(options)}) or exact option name[/red]")
       except (EOFError, KeyboardInterrupt):
-        return current_value if current_value is not None else options[0] if options else None
+        raise KeyboardInterrupt("Template generation cancelled by user")
   
   def _prompt_list(self, prompt_message: str, current_value: Any = None) -> List[str]:
     """Prompt for list input (comma-separated values)."""
@@ -336,12 +311,7 @@ class PromptHandler:
       items = [item.strip() for item in value.split(',') if item.strip()]
       return items
     except (EOFError, KeyboardInterrupt):
-      if current_value and isinstance(current_value, list):
-        return current_value
-      elif current_value:
-        return [str(current_value)]
-      else:
-        return []
+      raise KeyboardInterrupt("Template generation cancelled by user")
   
   def _show_summary(self):
     """Display a summary of all configured variables."""
@@ -349,10 +319,6 @@ class PromptHandler:
       self.console.print("[yellow]No variables were configured.[/yellow]")
       return
     
-    self.console.print("\n" + "="*50)
-    self.console.print("[bold green]📋 Configuration Summary[/bold green]")
-    self.console.print("="*50)
-    
     table = Table(box=box.SIMPLE_HEAVY)
     table.add_column("Variable", style="cyan", min_width=20)
     table.add_column("Value", style="green")
@@ -371,9 +337,6 @@ class PromptHandler:
     self.console.print(table)
     self.console.print()
     
-    try:
-      if not Confirm.ask("[bold]Proceed with template generation?[/bold]", default=True):
-        raise KeyboardInterrupt("Template generation cancelled by user")
-    except (EOFError, KeyboardInterrupt):
-      # If user cancels, still proceed with defaults
-      self.console.print("[yellow]Using current configuration to proceed.[/yellow]")
+    # Ask user if they want to proceed with template generation
+    if not Confirm.ask("[bold]Proceed with template generation?[/bold]", default=True):
+      raise KeyboardInterrupt("Template generation cancelled by user")

+ 0 - 1
cli/modules/compose.py

@@ -48,7 +48,6 @@ class ComposeModule(Module):
         "description": "Variables for Traefik labels",
         "vars": {
           "traefik": {"description": "Enable Traefik labels", "value": False, "var_type": "boolean"},
-          "traefik_enable": {"description": "Enable Traefik for this service", "value": True, "var_type": "boolean"},
           "traefik_host": {"description": "Traefik host rule", "value": "example.com"},
           "traefik_tls": {"description": "Enable TLS for Traefik", "value": True, "var_type": "boolean"},
           "traefik_certresolver": {"description": "Traefik certificate resolver", "value": "letsencrypt"},

+ 2 - 2
library/compose/nginx/compose.yaml

@@ -20,7 +20,7 @@ services:
       replicas: {{ swarm_replicas | default(1) }}
       {% if traefik %}
       labels:
-        - traefik.enable={{ traefik_enable | default(true) }}
+        - traefik.enable={{ traefik }}
         - traefik.http.services.{{ container_name }}.loadbalancer.server.port=80
         - traefik.http.routers.{{ container_name }}.entrypoints=websecure
         - traefik.http.routers.{{ container_name }}.rule=Host(`{{ traefik_host }}`)
@@ -39,7 +39,7 @@ services:
     #   - ./data:/usr/share/nginx/html:ro
     {% if traefik and not swarm %}
     labels:
-      - traefik.enable={{ traefik_enable | default(true) }}
+      - traefik.enable={{ traefik  }}
       - traefik.http.services.{{ container_name }}.loadbalancer.server.port=80
       - traefik.http.routers.{{ container_name }}.entrypoints=websecure
       - traefik.http.routers.{{ container_name }}.rule=Host(`{{ traefik_host }}`)