diff --git a/src/haiku/rag/cli.py b/src/haiku/rag/cli.py index 31742e10..078a2178 100644 --- a/src/haiku/rag/cli.py +++ b/src/haiku/rag/cli.py @@ -42,8 +42,19 @@ def main( callback=version_callback, help="Show version and exit", ), + config: Path | None = typer.Option( + None, + "--config", + help="Path to YAML configuration file", + ), ): """haiku.rag CLI - Vector database RAG system""" + # Store config path in environment for config loader to use + if config: + import os + + os.environ["HAIKU_RAG_CONFIG_PATH"] = str(config.absolute()) + # Configure logging minimally for CLI context if Config.ENV == "development": # Lazy import logfire only in development @@ -308,6 +319,54 @@ def settings(): app.show_settings() +@cli.command("init-config", help="Generate a YAML configuration file") +def init_config( + output: Path = typer.Argument( + Path("haiku.rag.yaml"), + help="Output path for the config file", + ), + from_env: bool = typer.Option( + False, + "--from-env", + help="Migrate settings from .env file", + ), +): + """Generate a YAML configuration file with defaults or from .env.""" + import yaml + + from haiku.rag.config_loader import generate_default_config, load_config_from_env + + if output.exists(): + typer.echo( + f"Error: {output} already exists. Remove it first or choose a different path." + ) + raise typer.Exit(1) + + if from_env: + # Load from environment variables (including .env if present) + from dotenv import load_dotenv + + load_dotenv() + config_data = load_config_from_env() + if not config_data: + typer.echo("Warning: No environment variables found to migrate.") + typer.echo("Generating default configuration instead.") + config_data = generate_default_config() + else: + config_data = generate_default_config() + + # Write YAML with comments + with open(output, "w") as f: + f.write("# haiku.rag configuration file\n") + f.write( + "# See https://ggozad.github.io/haiku.rag/configuration/ for details\n\n" + ) + yaml.dump(config_data, f, default_flow_style=False, sort_keys=False) + + typer.echo(f"Configuration file created: {output}") + typer.echo("Edit the file to customize your settings.") + + @cli.command( "rebuild", help="Rebuild the database by deleting all chunks and re-indexing all documents", diff --git a/src/haiku/rag/config_loader.py b/src/haiku/rag/config_loader.py index 8f435371..b5a8e272 100644 --- a/src/haiku/rag/config_loader.py +++ b/src/haiku/rag/config_loader.py @@ -9,12 +9,18 @@ def find_config_file(cli_path: Path | None = None) -> Path | None: """Find the YAML config file using the search path. Search order: - 1. CLI-provided path (if given) + 1. CLI-provided path (via HAIKU_RAG_CONFIG_PATH env var or parameter) 2. ./haiku.rag.yaml (current directory) 3. ~/.config/haiku.rag/config.yaml (user config) Returns None if no config file is found. """ + # Check environment variable first (set by CLI --config flag) + if not cli_path: + env_path = os.getenv("HAIKU_RAG_CONFIG_PATH") + if env_path: + cli_path = Path(env_path) + if cli_path: if cli_path.exists(): return cli_path