"""Configuration manager for k8s-tool.""" import json import os from pathlib import Path from typing import List, Dict, Any from rich.console import Console console = Console() class ConfigManager: """Manage application configuration.""" def __init__(self): """Initialize configuration manager.""" self.config_dir = Path.home() / ".config" / "k8s-tool" self.config_file = self.config_dir / "k8s-tool.cfg" self._ensure_config_dir() self.config = self._load_config() def _ensure_config_dir(self): """Ensure configuration directory exists.""" try: self.config_dir.mkdir(parents=True, exist_ok=True) except Exception as e: console.print(f"[yellow]Warning:[/yellow] Could not create config directory: {e}") def _load_config(self) -> Dict[str, Any]: """Load configuration from file.""" if not self.config_file.exists(): return {"favorites": []} try: with open(self.config_file, 'r') as f: return json.load(f) except Exception as e: console.print(f"[yellow]Warning:[/yellow] Could not load config: {e}") return {"favorites": []} def _save_config(self): """Save configuration to file.""" try: with open(self.config_file, 'w') as f: json.dump(self.config, f, indent=2) except Exception as e: console.print(f"[red]Error:[/red] Could not save config: {e}") def get_favorites(self) -> List[str]: """Get list of favorite namespaces.""" return self.config.get("favorites", []) def add_favorite(self, namespace: str) -> bool: """Add namespace to favorites.""" favorites = self.config.get("favorites", []) if namespace not in favorites: favorites.append(namespace) self.config["favorites"] = sorted(favorites) self._save_config() console.print(f"[green]✓[/green] Added [cyan]{namespace}[/cyan] to favorites") return True else: console.print(f"[yellow]![/yellow] [cyan]{namespace}[/cyan] is already in favorites") return False def remove_favorite(self, namespace: str) -> bool: """Remove namespace from favorites.""" favorites = self.config.get("favorites", []) if namespace in favorites: favorites.remove(namespace) self.config["favorites"] = favorites self._save_config() console.print(f"[green]✓[/green] Removed [cyan]{namespace}[/cyan] from favorites") return True else: console.print(f"[yellow]![/yellow] [cyan]{namespace}[/cyan] is not in favorites") return False def is_favorite(self, namespace: str) -> bool: """Check if namespace is in favorites.""" return namespace in self.config.get("favorites", []) def clear_favorites(self): """Clear all favorites.""" self.config["favorites"] = [] self._save_config() console.print("[green]✓[/green] Cleared all favorites")