Files
cli/sunbeam/config.py
Sienna Meridian Satterwhite 507b4d3fb7 feat(config): add production host and infrastructure directory configuration
- Add usage: sunbeam config [-h] action ...

positional arguments:
  action
    set       Set configuration values
    get       Get current configuration
    clear     Clear configuration

options:
  -h, --help  show this help message and exit subcommand with set/get/clear actions
- Store configuration in  with production_host and infra_directory
- Integrate with production environment detection, prioritizing config over SUNBEAM_SSH_HOST
- Add comprehensive test coverage with 11 new tests
- Update CLI help and error messages for better user experience
2026-03-07 16:08:38 +00:00

74 lines
2.4 KiB
Python

"""Configuration management — load/save ~/.sunbeam.json for production host and infra directory."""
import json
import os
from pathlib import Path
from typing import Optional
CONFIG_PATH = Path.home() / ".sunbeam.json"
class SunbeamConfig:
"""Sunbeam configuration with production host and infrastructure directory."""
def __init__(self, production_host: str = "", infra_directory: str = ""):
self.production_host = production_host
self.infra_directory = infra_directory
def to_dict(self) -> dict:
"""Convert configuration to dictionary for JSON serialization."""
return {
"production_host": self.production_host,
"infra_directory": self.infra_directory,
}
@classmethod
def from_dict(cls, data: dict) -> 'SunbeamConfig':
"""Create configuration from dictionary."""
return cls(
production_host=data.get("production_host", ""),
infra_directory=data.get("infra_directory", ""),
)
def load_config() -> SunbeamConfig:
"""Load configuration from ~/.sunbeam.json, return empty config if not found."""
if not CONFIG_PATH.exists():
return SunbeamConfig()
try:
with open(CONFIG_PATH, 'r') as f:
data = json.load(f)
return SunbeamConfig.from_dict(data)
except (json.JSONDecodeError, IOError, KeyError) as e:
from sunbeam.output import warn
warn(f"Failed to load config from {CONFIG_PATH}: {e}")
return SunbeamConfig()
def save_config(config: SunbeamConfig) -> None:
"""Save configuration to ~/.sunbeam.json."""
try:
CONFIG_PATH.parent.mkdir(parents=True, exist_ok=True)
with open(CONFIG_PATH, 'w') as f:
json.dump(config.to_dict(), f, indent=2)
from sunbeam.output import ok
ok(f"Configuration saved to {CONFIG_PATH}")
except IOError as e:
from sunbeam.output import die
die(f"Failed to save config to {CONFIG_PATH}: {e}")
def get_production_host() -> str:
"""Get production host from config or SUNBEAM_SSH_HOST environment variable."""
config = load_config()
if config.production_host:
return config.production_host
return os.environ.get("SUNBEAM_SSH_HOST", "")
def get_infra_directory() -> str:
"""Get infrastructure directory from config."""
config = load_config()
return config.infra_directory