|
| 1 | +"""CLI commands for Mellea configuration management.""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import typer |
| 6 | +from rich.console import Console |
| 7 | +from rich.syntax import Syntax |
| 8 | +from rich.table import Table |
| 9 | + |
| 10 | +from mellea.config import ( |
| 11 | + find_config_file, |
| 12 | + get_config_path, |
| 13 | + get_user_config_dir, |
| 14 | + init_project_config, |
| 15 | + init_user_config, |
| 16 | + load_config, |
| 17 | +) |
| 18 | + |
| 19 | +config_app = typer.Typer(name="config", help="Manage Mellea configuration files") |
| 20 | +console = Console() |
| 21 | + |
| 22 | + |
| 23 | +@config_app.command("init") |
| 24 | +def init_user( |
| 25 | + force: bool = typer.Option( |
| 26 | + False, "--force", "-f", help="Overwrite existing config file" |
| 27 | + ), |
| 28 | +) -> None: |
| 29 | + """Create a user configuration file at ~/.config/mellea/config.toml.""" |
| 30 | + try: |
| 31 | + config_path = init_user_config(force=force) |
| 32 | + console.print(f"[green]✓[/green] Created user config at: {config_path}") |
| 33 | + console.print( |
| 34 | + "\nEdit this file to set your default backend, model, and credentials." |
| 35 | + ) |
| 36 | + console.print( |
| 37 | + "Run [cyan]m config show[/cyan] to view the current configuration." |
| 38 | + ) |
| 39 | + except FileExistsError as e: |
| 40 | + console.print(f"[red]✗[/red] {e}") |
| 41 | + console.print("Use [cyan]--force[/cyan] to overwrite the existing file.") |
| 42 | + raise typer.Exit(1) |
| 43 | + except Exception as e: |
| 44 | + console.print(f"[red]✗[/red] Error creating config: {e}") |
| 45 | + raise typer.Exit(1) |
| 46 | + |
| 47 | + |
| 48 | +@config_app.command("init-project") |
| 49 | +def init_project( |
| 50 | + force: bool = typer.Option( |
| 51 | + False, "--force", "-f", help="Overwrite existing config file" |
| 52 | + ), |
| 53 | +) -> None: |
| 54 | + """Create a project configuration file at ./mellea.toml.""" |
| 55 | + try: |
| 56 | + config_path = init_project_config(force=force) |
| 57 | + console.print(f"[green]✓[/green] Created project config at: {config_path}") |
| 58 | + console.print("\nThis config will override user settings for this project.") |
| 59 | + console.print( |
| 60 | + "Run [cyan]m config show[/cyan] to view the effective configuration." |
| 61 | + ) |
| 62 | + except FileExistsError as e: |
| 63 | + console.print(f"[red]✗[/red] {e}") |
| 64 | + console.print("Use [cyan]--force[/cyan] to overwrite the existing file.") |
| 65 | + raise typer.Exit(1) |
| 66 | + except Exception as e: |
| 67 | + console.print(f"[red]✗[/red] Error creating config: {e}") |
| 68 | + raise typer.Exit(1) |
| 69 | + |
| 70 | + |
| 71 | +@config_app.command("show") |
| 72 | +def show_config() -> None: |
| 73 | + """Display the current effective configuration.""" |
| 74 | + try: |
| 75 | + config, config_path = load_config() |
| 76 | + |
| 77 | + # Display config source |
| 78 | + if config_path: |
| 79 | + console.print(f"[bold]Configuration loaded from:[/bold] {config_path}\n") |
| 80 | + else: |
| 81 | + console.print( |
| 82 | + "[yellow]No configuration file found. Using defaults.[/yellow]\n" |
| 83 | + ) |
| 84 | + |
| 85 | + # Create a table for the configuration |
| 86 | + table = Table( |
| 87 | + title="Effective Configuration", show_header=True, header_style="bold cyan" |
| 88 | + ) |
| 89 | + table.add_column("Setting", style="dim") |
| 90 | + table.add_column("Value") |
| 91 | + |
| 92 | + # Backend settings |
| 93 | + table.add_row( |
| 94 | + "Backend Name", config.backend.name or "[dim](default: ollama)[/dim]" |
| 95 | + ) |
| 96 | + table.add_row( |
| 97 | + "Model ID", |
| 98 | + config.backend.model_id or "[dim](default: granite-4-micro:3b)[/dim]", |
| 99 | + ) |
| 100 | + |
| 101 | + # Model options |
| 102 | + if config.backend.model_options: |
| 103 | + for key, value in config.backend.model_options.items(): |
| 104 | + table.add_row(f" {key}", str(value)) |
| 105 | + |
| 106 | + # Backend kwargs |
| 107 | + if config.backend.kwargs: |
| 108 | + for key, value in config.backend.kwargs.items(): |
| 109 | + table.add_row(f" backend.{key}", str(value)) |
| 110 | + |
| 111 | + # Credentials (masked) |
| 112 | + if config.credentials.openai_api_key: |
| 113 | + table.add_row("OpenAI API Key", "[dim]***configured***[/dim]") |
| 114 | + if config.credentials.watsonx_api_key: |
| 115 | + table.add_row("Watsonx API Key", "[dim]***configured***[/dim]") |
| 116 | + if config.credentials.watsonx_project_id: |
| 117 | + table.add_row("Watsonx Project ID", config.credentials.watsonx_project_id) |
| 118 | + if config.credentials.watsonx_url: |
| 119 | + table.add_row("Watsonx URL", config.credentials.watsonx_url) |
| 120 | + |
| 121 | + # General settings |
| 122 | + table.add_row( |
| 123 | + "Context Type", config.context_type or "[dim](default: simple)[/dim]" |
| 124 | + ) |
| 125 | + table.add_row("Log Level", config.log_level or "[dim](default: INFO)[/dim]") |
| 126 | + |
| 127 | + console.print(table) |
| 128 | + |
| 129 | + # Show search order |
| 130 | + console.print("\n[bold]Configuration search order:[/bold]") |
| 131 | + console.print("1. Project config: ./mellea.toml (current dir and parents)") |
| 132 | + console.print(f"2. User config: {get_user_config_dir() / 'config.toml'}") |
| 133 | + console.print( |
| 134 | + "\n[dim]Explicit parameters in code override config file values.[/dim]" |
| 135 | + ) |
| 136 | + |
| 137 | + except Exception as e: |
| 138 | + console.print(f"[red]✗[/red] Error loading config: {e}") |
| 139 | + raise typer.Exit(1) |
| 140 | + |
| 141 | + |
| 142 | +@config_app.command("path") |
| 143 | +def show_path() -> None: |
| 144 | + """Show the path to the currently loaded configuration file.""" |
| 145 | + try: |
| 146 | + config_path = find_config_file() |
| 147 | + |
| 148 | + if config_path: |
| 149 | + console.print(f"[green]✓[/green] Using config file: {config_path}") |
| 150 | + |
| 151 | + # Show the file content |
| 152 | + console.print("\n[bold]File contents:[/bold]") |
| 153 | + with open(config_path) as f: |
| 154 | + content = f.read() |
| 155 | + syntax = Syntax(content, "toml", theme="monokai", line_numbers=True) |
| 156 | + console.print(syntax) |
| 157 | + else: |
| 158 | + console.print("[yellow]No configuration file found.[/yellow]") |
| 159 | + console.print("\nSearched locations:") |
| 160 | + console.print("1. ./mellea.toml (current dir and parents)") |
| 161 | + console.print(f"2. {get_user_config_dir() / 'config.toml'}") |
| 162 | + console.print("\nRun [cyan]m config init[/cyan] to create a user config.") |
| 163 | + console.print( |
| 164 | + "Run [cyan]m config init-project[/cyan] to create a project config." |
| 165 | + ) |
| 166 | + except Exception as e: |
| 167 | + console.print(f"[red]✗[/red] Error: {e}") |
| 168 | + raise typer.Exit(1) |
| 169 | + |
| 170 | + |
| 171 | +@config_app.command("where") |
| 172 | +def show_locations() -> None: |
| 173 | + """Show all possible configuration file locations.""" |
| 174 | + user_config_dir = get_user_config_dir() |
| 175 | + user_config_path = user_config_dir / "config.toml" |
| 176 | + project_config_path = Path.cwd() / "mellea.toml" |
| 177 | + |
| 178 | + console.print("[bold]Configuration file locations:[/bold]\n") |
| 179 | + |
| 180 | + # User config |
| 181 | + console.print(f"[cyan]User config:[/cyan] {user_config_path}") |
| 182 | + if user_config_path.exists(): |
| 183 | + console.print(" [green]✓ exists[/green]") |
| 184 | + else: |
| 185 | + console.print(" [dim]✗ not found[/dim]") |
| 186 | + console.print(" Run [cyan]m config init[/cyan] to create") |
| 187 | + |
| 188 | + console.print() |
| 189 | + |
| 190 | + # Project config |
| 191 | + console.print(f"[cyan]Project config:[/cyan] {project_config_path}") |
| 192 | + if project_config_path.exists(): |
| 193 | + console.print(" [green]✓ exists[/green]") |
| 194 | + else: |
| 195 | + console.print(" [dim]✗ not found[/dim]") |
| 196 | + console.print(" Run [cyan]m config init-project[/cyan] to create") |
| 197 | + |
| 198 | + console.print() |
| 199 | + |
| 200 | + # Currently loaded |
| 201 | + current = find_config_file() |
| 202 | + if current: |
| 203 | + console.print(f"[bold green]Currently loaded:[/bold green] {current}") |
| 204 | + else: |
| 205 | + console.print( |
| 206 | + "[yellow]No config file currently loaded (using defaults)[/yellow]" |
| 207 | + ) |
0 commit comments