Merge pull request 'vcs' (#1) from vcs into develop
Reviewed-on: https://hattori.ztsh.eu/paas/karl/pulls/1
This commit is contained in:
commit
6476e6b3be
3 changed files with 40 additions and 3 deletions
|
|
@ -1,18 +1,25 @@
|
|||
from functools import lru_cache
|
||||
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
from pydantic import BaseModel
|
||||
from pathlib import Path
|
||||
|
||||
import yaml
|
||||
from pydantic import BaseModel
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
class AppConfig(BaseModel):
|
||||
host: str = "127.0.0.1"
|
||||
port: int = 8000
|
||||
reload: bool = True
|
||||
|
||||
class GitConfig(BaseModel):
|
||||
directory: str = "/opt/repo/sample"
|
||||
branch: str = "master"
|
||||
remote: str = "origin"
|
||||
|
||||
class Settings(BaseSettings):
|
||||
model_config = SettingsConfigDict(env_prefix="KARL_", env_nested_delimiter="__")
|
||||
app: AppConfig = AppConfig()
|
||||
git: GitConfig = GitConfig()
|
||||
|
||||
@classmethod
|
||||
def from_yaml(cls, path: Path | str = "config/config.yaml") -> "Settings":
|
||||
|
|
@ -23,6 +30,7 @@ class Settings(BaseSettings):
|
|||
data = yaml.safe_load(fh) or {}
|
||||
return cls(**data)
|
||||
|
||||
|
||||
@lru_cache
|
||||
def get_settings() -> Settings:
|
||||
return Settings.from_yaml()
|
||||
|
|
|
|||
|
|
@ -0,0 +1,28 @@
|
|||
from git import Repo, Remote
|
||||
|
||||
from app.config import get_settings
|
||||
|
||||
|
||||
class GitService:
|
||||
def __init__(self):
|
||||
self._settings = get_settings()
|
||||
self._repo = Repo(self._settings.git.directory)
|
||||
self._origin: Remote = self._repo.remotes.origin
|
||||
|
||||
def get_modified_compose(self) -> str | None:
|
||||
self._update()
|
||||
return self._diff()
|
||||
|
||||
def _update(self):
|
||||
self._origin.pull()
|
||||
|
||||
def _diff(self) -> str | None:
|
||||
diff = self._repo.head.commit.diff("HEAD~1")
|
||||
composes = [f for f in diff if f.a_path.endswith("docker-compose.yml")]
|
||||
match len(composes):
|
||||
case 0:
|
||||
return None
|
||||
case 1:
|
||||
return composes[0].a_path
|
||||
case _:
|
||||
raise Exception("Multiple compose files modified")
|
||||
|
|
@ -11,6 +11,7 @@ dependencies = [
|
|||
"jinja2>=3.1.4",
|
||||
"pydantic-settings>=2.4.0",
|
||||
"pyyaml>=6.0.2",
|
||||
"gitpython>=3.1.45"
|
||||
]
|
||||
|
||||
[project.optional-dependencies]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue