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 functools import lru_cache
|
||||||
|
|
||||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
||||||
from pydantic import BaseModel
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||||
|
|
||||||
|
|
||||||
class AppConfig(BaseModel):
|
class AppConfig(BaseModel):
|
||||||
host: str = "127.0.0.1"
|
host: str = "127.0.0.1"
|
||||||
port: int = 8000
|
port: int = 8000
|
||||||
reload: bool = True
|
reload: bool = True
|
||||||
|
|
||||||
|
class GitConfig(BaseModel):
|
||||||
|
directory: str = "/opt/repo/sample"
|
||||||
|
branch: str = "master"
|
||||||
|
remote: str = "origin"
|
||||||
|
|
||||||
class Settings(BaseSettings):
|
class Settings(BaseSettings):
|
||||||
model_config = SettingsConfigDict(env_prefix="KARL_", env_nested_delimiter="__")
|
model_config = SettingsConfigDict(env_prefix="KARL_", env_nested_delimiter="__")
|
||||||
app: AppConfig = AppConfig()
|
app: AppConfig = AppConfig()
|
||||||
|
git: GitConfig = GitConfig()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_yaml(cls, path: Path | str = "config/config.yaml") -> "Settings":
|
def from_yaml(cls, path: Path | str = "config/config.yaml") -> "Settings":
|
||||||
|
|
@ -23,6 +30,7 @@ class Settings(BaseSettings):
|
||||||
data = yaml.safe_load(fh) or {}
|
data = yaml.safe_load(fh) or {}
|
||||||
return cls(**data)
|
return cls(**data)
|
||||||
|
|
||||||
|
|
||||||
@lru_cache
|
@lru_cache
|
||||||
def get_settings() -> Settings:
|
def get_settings() -> Settings:
|
||||||
return Settings.from_yaml()
|
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",
|
"jinja2>=3.1.4",
|
||||||
"pydantic-settings>=2.4.0",
|
"pydantic-settings>=2.4.0",
|
||||||
"pyyaml>=6.0.2",
|
"pyyaml>=6.0.2",
|
||||||
|
"gitpython>=3.1.45"
|
||||||
]
|
]
|
||||||
|
|
||||||
[project.optional-dependencies]
|
[project.optional-dependencies]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue