diff --git a/src/codeguard/core/config.py b/src/codeguard/core/config.py new file mode 100644 index 0000000..091404d --- /dev/null +++ b/src/codeguard/core/config.py @@ -0,0 +1,38 @@ +from dataclasses import field +from pathlib import Path +from typing import List, Optional + +import yaml +from pydantic import BaseModel + + +class Config(BaseModel): + model: str = "codellama" + languages: List[str] = field(default_factory=lambda: ["python", "javascript", "go", "rust"]) + severity_threshold: str = "low" + fail_on_critical: bool = False + output_format: str = "text" + ollama_url: str = "http://localhost:11434" + timeout: int = 120 + + +def load_config(config_path: Optional[str] = None) -> Config: + if config_path is None: + config_path_str = str(Path.cwd() / "codeguard.yaml") + else: + config_path_str = config_path + + config_file = Path(config_path_str) + + if config_file.exists(): + with open(config_file) as f: + data = yaml.safe_load(f) or {} + return Config(**data) + + return Config() + + +def save_config(config: Config, config_path: str = "codeguard.yaml") -> None: + config_file = Path(config_path) + with open(config_file, "w") as f: + yaml.dump(config.model_dump(), f)