"""Configuration management for CLI.""" import json import os from dataclasses import dataclass, field from pathlib import Path from typing import Any @dataclass class CLIConfig: """CLI configuration.""" # API settings api_url: str = "http://127.0.0.1:8080" auth_token: str | None = None # User settings email: str | None = None allow_emoji: bool = False # Session settings default_session: str = "default" auto_save: bool = True # Display settings show_mood: bool = True show_relationship: bool = False show_facts: bool = False show_timestamps: bool = False # Paths config_dir: Path = field(default_factory=lambda: Path.home() / ".lc") sessions_file: Path = field(init=False) config_file: Path = field(init=False) def __post_init__(self): """Initialize computed fields.""" self.sessions_file = self.config_dir / "sessions.json" self.config_file = self.config_dir / "config.json" @classmethod def load(cls) -> "CLIConfig": """Load configuration from file. Returns: CLIConfig: Loaded configuration """ config = cls() config.config_dir.mkdir(parents=True, exist_ok=True) if config.config_file.exists(): try: with open(config.config_file, "r") as f: data = json.load(f) # Update fields from loaded data for key, value in data.items(): if hasattr(config, key): setattr(config, key, value) except (json.JSONDecodeError, IOError) as e: print(f"Warning: Could not load config: {e}") return config def save(self) -> None: """Save configuration to file.""" self.config_dir.mkdir(parents=True, exist_ok=True) data = { "api_url": self.api_url, "auth_token": self.auth_token, "email": self.email, "allow_emoji": self.allow_emoji, "default_session": self.default_session, "auto_save": self.auto_save, "show_mood": self.show_mood, "show_relationship": self.show_relationship, "show_facts": self.show_facts, "show_timestamps": self.show_timestamps, } with open(self.config_file, "w") as f: json.dump(data, f, indent=2) def get_api_url(self) -> str: """Get API URL, checking environment variables first. Returns: str: API URL """ return os.getenv("LOYAL_COMPANION_API_URL", self.api_url) def get_auth_token(self) -> str | None: """Get auth token, checking environment variables first. Returns: str | None: Auth token or None """ return os.getenv("LOYAL_COMPANION_TOKEN", self.auth_token)