Load configuration with priority support. Priority order (highest to lowest): 1. Environment variables 2. Local .env file (in current or parent directories) 3. Global ~/.codegraphcontext/.env Note: Does NOT create config directory - caller must call ensure_config_dir()
()
| 324 | |
| 325 | |
| 326 | def load_config() -> Dict[str, str]: |
| 327 | """ |
| 328 | Load configuration with priority support. |
| 329 | Priority order (highest to lowest): |
| 330 | 1. Environment variables |
| 331 | 2. Local .env file (in current or parent directories) |
| 332 | 3. Global ~/.codegraphcontext/.env |
| 333 | |
| 334 | Note: Does NOT create config directory - caller must call ensure_config_dir() first if needed. |
| 335 | """ |
| 336 | # Start with defaults |
| 337 | config = DEFAULT_CONFIG.copy() |
| 338 | |
| 339 | # Load global config |
| 340 | if CONFIG_FILE.exists(): |
| 341 | try: |
| 342 | with open(CONFIG_FILE, "r", encoding="utf-8") as f: |
| 343 | for line in f: |
| 344 | line = line.strip() |
| 345 | if line and not line.startswith("#") and "=" in line: |
| 346 | key, value = line.split("=", 1) |
| 347 | config[key.strip()] = value.strip() |
| 348 | except Exception as e: |
| 349 | console.print(f"[red]Error loading global config: {e}[/red]") |
| 350 | |
| 351 | # Load local .env file if it exists (overrides global) |
| 352 | local_env = find_local_env() |
| 353 | if local_env and local_env.exists(): |
| 354 | try: |
| 355 | with open(local_env, "r", encoding="utf-8") as f: |
| 356 | for line in f: |
| 357 | line = line.strip() |
| 358 | if line and not line.startswith("#") and "=" in line: |
| 359 | key, value = line.split("=", 1) |
| 360 | key = key.strip() |
| 361 | # Only override if it's a config key (not database credentials in local file) |
| 362 | if key in DEFAULT_CONFIG or key in DATABASE_CREDENTIAL_KEYS: |
| 363 | config[key] = value.strip() |
| 364 | except Exception as e: |
| 365 | console.print(f"[yellow]Warning: Error loading local .env: {e}[/yellow]") |
| 366 | |
| 367 | # Environment variables have highest priority |
| 368 | for key in DEFAULT_CONFIG.keys(): |
| 369 | env_value = os.getenv(key) |
| 370 | if env_value is not None: |
| 371 | config[key] = env_value |
| 372 | |
| 373 | return config |
| 374 | |
| 375 | |
| 376 | def should_apply_project_dotenv() -> bool: |
no test coverage detected