mirror of
https://github.com/Monadical-SAS/cubbi.git
synced 2025-12-21 04:39:07 +00:00
feat(cli): auto connect to a session
This commit is contained in:
@@ -52,22 +52,22 @@ class ConfigManager:
|
||||
try:
|
||||
with open(self.config_path, "r") as f:
|
||||
config_data = yaml.safe_load(f) or {}
|
||||
|
||||
|
||||
# Create a new config from scratch, then update with data from file
|
||||
config = Config(
|
||||
docker=config_data.get('docker', {}),
|
||||
defaults=config_data.get('defaults', {})
|
||||
docker=config_data.get("docker", {}),
|
||||
defaults=config_data.get("defaults", {}),
|
||||
)
|
||||
|
||||
|
||||
# Add drivers
|
||||
if 'drivers' in config_data:
|
||||
for driver_name, driver_data in config_data['drivers'].items():
|
||||
if "drivers" in config_data:
|
||||
for driver_name, driver_data in config_data["drivers"].items():
|
||||
config.drivers[driver_name] = Driver.model_validate(driver_data)
|
||||
|
||||
|
||||
# Add sessions (stored as simple dictionaries)
|
||||
if 'sessions' in config_data:
|
||||
config.sessions = config_data['sessions']
|
||||
|
||||
if "sessions" in config_data:
|
||||
config.sessions = config_data["sessions"]
|
||||
|
||||
return config
|
||||
except Exception as e:
|
||||
print(f"Error loading config: {e}")
|
||||
@@ -79,10 +79,10 @@ class ConfigManager:
|
||||
"""Create a default configuration"""
|
||||
self.config_dir.mkdir(parents=True, exist_ok=True)
|
||||
self.drivers_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
|
||||
# Load built-in drivers from directories
|
||||
builtin_drivers = self.load_builtin_drivers()
|
||||
|
||||
|
||||
# Merge with default drivers, with directory drivers taking precedence
|
||||
drivers = {**DEFAULT_DRIVERS, **builtin_drivers}
|
||||
|
||||
@@ -106,10 +106,10 @@ class ConfigManager:
|
||||
self.config = config
|
||||
|
||||
self.config_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
|
||||
# Use model_dump with mode="json" for proper serialization of enums
|
||||
config_dict = self.config.model_dump(mode="json")
|
||||
|
||||
|
||||
# Write to file
|
||||
with open(self.config_path, "w") as f:
|
||||
yaml.dump(config_dict, f)
|
||||
@@ -137,23 +137,28 @@ class ConfigManager:
|
||||
def list_sessions(self) -> Dict:
|
||||
"""List all sessions in the config"""
|
||||
return self.config.sessions
|
||||
|
||||
|
||||
def load_driver_from_dir(self, driver_dir: Path) -> Optional[Driver]:
|
||||
"""Load a driver configuration from a directory"""
|
||||
yaml_path = driver_dir / "mai-driver.yaml" # Keep this name for backward compatibility
|
||||
|
||||
yaml_path = (
|
||||
driver_dir / "mai-driver.yaml"
|
||||
) # Keep this name for backward compatibility
|
||||
|
||||
if not yaml_path.exists():
|
||||
return None
|
||||
|
||||
|
||||
try:
|
||||
with open(yaml_path, "r") as f:
|
||||
driver_data = yaml.safe_load(f)
|
||||
|
||||
|
||||
# Extract required fields
|
||||
if not all(k in driver_data for k in ["name", "description", "version", "maintainer"]):
|
||||
if not all(
|
||||
k in driver_data
|
||||
for k in ["name", "description", "version", "maintainer"]
|
||||
):
|
||||
print(f"Driver config {yaml_path} missing required fields")
|
||||
return None
|
||||
|
||||
|
||||
# Create driver object
|
||||
driver = Driver(
|
||||
name=driver_data["name"],
|
||||
@@ -163,37 +168,37 @@ class ConfigManager:
|
||||
image=f"monadical/mc-{driver_data['name']}:latest",
|
||||
ports=driver_data.get("ports", []),
|
||||
)
|
||||
|
||||
|
||||
return driver
|
||||
except Exception as e:
|
||||
print(f"Error loading driver from {yaml_path}: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def load_builtin_drivers(self) -> Dict[str, Driver]:
|
||||
"""Load all built-in drivers from the drivers directory"""
|
||||
drivers = {}
|
||||
|
||||
|
||||
if not BUILTIN_DRIVERS_DIR.exists():
|
||||
return drivers
|
||||
|
||||
|
||||
for driver_dir in BUILTIN_DRIVERS_DIR.iterdir():
|
||||
if driver_dir.is_dir():
|
||||
driver = self.load_driver_from_dir(driver_dir)
|
||||
if driver:
|
||||
drivers[driver.name] = driver
|
||||
|
||||
|
||||
return drivers
|
||||
|
||||
|
||||
def get_driver_path(self, driver_name: str) -> Optional[Path]:
|
||||
"""Get the directory path for a driver"""
|
||||
# Check built-in drivers first
|
||||
builtin_path = BUILTIN_DRIVERS_DIR / driver_name
|
||||
if builtin_path.exists() and builtin_path.is_dir():
|
||||
return builtin_path
|
||||
|
||||
|
||||
# Then check user drivers
|
||||
user_path = self.drivers_dir / driver_name
|
||||
if user_path.exists() and user_path.is_dir():
|
||||
return user_path
|
||||
|
||||
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user