From 099f4fb42826f5c352b8931bdf2a3d91c4a93e28 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Thu, 29 Jan 2026 13:53:36 +0000 Subject: [PATCH] Initial upload: API Mock CLI v0.1.0 --- src/cli/config.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/cli/config.py diff --git a/src/cli/config.py b/src/cli/config.py new file mode 100644 index 0000000..fe716cc --- /dev/null +++ b/src/cli/config.py @@ -0,0 +1,68 @@ +import os +from typing import Optional, Dict, Any +import yaml +from src.models.config import ServerConfig, ProjectConfig + + +class ConfigLoader: + CONFIG_FILE_NAMES = ["config.yaml", ".api-mock.yaml", "api-mock.yaml"] + + @staticmethod + def find_config_file(path: Optional[str] = None) -> Optional[str]: + if path and os.path.exists(path): + return path + for name in ConfigLoader.CONFIG_FILE_NAMES: + if os.path.exists(name): + return name + return None + + @staticmethod + def load_config(path: str) -> Dict[str, Any]: + with open(path, "r") as f: + return yaml.safe_load(f) or {} + + @staticmethod + def apply_env_overrides(config: Dict[str, Any]) -> Dict[str, Any]: + env_mappings = { + "API_MOCK_PORT": ("server", "port"), + "API_MOCK_HOST": ("server", "host"), + "API_MOCK_OFFLINE": ("server", "offline"), + "NGROK_AUTHTOKEN": ("server", "ngrok_authtoken"), + "API_MOCK_LOG_LEVEL": ("server", "log_level"), + } + for env_var, path in env_mappings.items(): + value = os.environ.get(env_var) + if value is not None: + section, key = path + if section not in config: + config[section] = {} + if key in ("port", "offline"): + value = int(value) if key == "port" else value.lower() == "true" + config[section][key] = value + return config + + @classmethod + def load(cls, config_path: Optional[str] = None) -> ProjectConfig: + path = cls.find_config_file(config_path) + if not path: + return ProjectConfig(name="default") + config_data = cls.load_config(path) + config_data = cls.apply_env_overrides(config_data) + return ProjectConfig(**config_data) + + @staticmethod + def save_config(config: ProjectConfig, path: str) -> None: + config_dict = config.model_dump(exclude_none=True) + with open(path, "w") as f: + yaml.dump(config_dict, f, default_flow_style=False) + + @staticmethod + def create_default_config(path: str = "config.yaml") -> ProjectConfig: + config = ProjectConfig( + name="my-mock-api", + version="1.0.0", + description="API Mock Project", + server=ServerConfig() + ) + ConfigLoader.save_config(config, path) + return config