Coverage for src/alprina_cli/config.py: 28%
46 statements
« prev ^ index » next coverage.py v7.11.3, created at 2025-11-12 18:07 +0100
« prev ^ index » next coverage.py v7.11.3, created at 2025-11-12 18:07 +0100
1"""
2Configuration management for Alprina CLI.
3"""
5import yaml
6import os
7from pathlib import Path
8from rich.console import Console
9from rich.panel import Panel
11console = Console()
13ALPRINA_DIR = Path.home() / ".alprina"
14CONFIG_FILE = ALPRINA_DIR / "config.json"
16DEFAULT_CONFIG = {
17 "version": "0.1.0",
18 "backend_url": "https://api.alprina.ai",
19 "timeout": 30,
20 "max_retries": 3,
21 "log_level": "INFO",
22 "theme": "dark",
23 "memory": {
24 "enabled": True,
25 "api_key": "", # Set via environment variable MEM0_API_KEY
26 "user_id": "default"
27 }
28}
31def load_config() -> dict:
32 """Load configuration from file."""
33 if not CONFIG_FILE.exists():
34 return DEFAULT_CONFIG.copy()
36 try:
37 with open(CONFIG_FILE, "r") as f:
38 import json
39 config = json.load(f)
40 return {**DEFAULT_CONFIG, **config}
41 except Exception as e:
42 console.print(f"[yellow]Warning: Could not load config: {e}[/yellow]")
43 return DEFAULT_CONFIG.copy()
46def save_config(config: dict):
47 """Save configuration to file."""
48 ALPRINA_DIR.mkdir(exist_ok=True)
50 import json
51 with open(CONFIG_FILE, "w") as f:
52 json.dump(config, f, indent=2)
55def get_api_key() -> str:
56 """
57 Get API key from environment variable or auth file.
59 Returns:
60 API key string or None if not found
61 """
62 # Check environment variable first
63 api_key = os.getenv("ALPRINA_API_KEY")
64 if api_key:
65 return api_key
67 # Check auth file
68 auth_file = ALPRINA_DIR / "auth.json"
69 if auth_file.exists():
70 try:
71 import json
72 with open(auth_file, "r") as f:
73 auth_data = json.load(f)
74 return auth_data.get("api_key")
75 except Exception:
76 pass
78 return None
81def init_config_command():
82 """Initialize default configuration."""
83 if CONFIG_FILE.exists():
84 from rich.prompt import Confirm
85 if not Confirm.ask("Config file already exists. Overwrite?", default=False):
86 return
88 save_config(DEFAULT_CONFIG)
90 console.print(Panel(
91 f"[green]✓ Configuration initialized[/green]\n\n"
92 f"Location: {CONFIG_FILE}\n\n"
93 f"Edit this file to customize Alprina settings.",
94 title="Config Initialized"
95 ))