Initial upload: API Mock CLI v0.1.0
Some checks failed
CI / test (3.10) (push) Has been cancelled
CI / test (3.11) (push) Has been cancelled
CI / test (3.12) (push) Has been cancelled
CI / test (3.9) (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / type-check (push) Has been cancelled
CI / build (push) Has been cancelled
Some checks failed
CI / test (3.10) (push) Has been cancelled
CI / test (3.11) (push) Has been cancelled
CI / test (3.12) (push) Has been cancelled
CI / test (3.9) (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / type-check (push) Has been cancelled
CI / build (push) Has been cancelled
This commit is contained in:
68
src/cli/config.py
Normal file
68
src/cli/config.py
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user