chore: fix templating
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
import git
|
||||
|
||||
logger = logging.getLogger()
|
||||
|
||||
|
||||
def git_push_service_directory(
|
||||
repo_root: str | Path, branch_name: str, service_dir_rel_path: str, commit_msg: str
|
||||
):
|
||||
"""
|
||||
Push newly created service to Gitea
|
||||
"""
|
||||
try:
|
||||
repo = git.Repo(repo_root)
|
||||
|
||||
logger.info(f"Switching to branch '{branch_name}'...")
|
||||
# Create branch if it doesn't exist, then checkout
|
||||
if branch_name in repo.heads:
|
||||
new_branch = repo.heads[branch_name]
|
||||
else:
|
||||
new_branch = repo.create_head(branch_name)
|
||||
new_branch.checkout()
|
||||
|
||||
logger.info(f"Staging files inside: {service_dir_rel_path}...")
|
||||
repo.index.add([service_dir_rel_path])
|
||||
|
||||
# Check if there are changes to avoid empty commit crashes
|
||||
if not repo.is_dirty(index=True, working_tree=False, path=service_dir_rel_path):
|
||||
logger.info("No changes detected. Skipping commit.")
|
||||
return
|
||||
|
||||
logger.info("Committing changes...")
|
||||
repo.index.commit(commit_msg)
|
||||
|
||||
logger.info(f"Pushing branch '{branch_name}' to origin...")
|
||||
origin = repo.remote(name="origin")
|
||||
origin.push(refspec=f"{branch_name}:{branch_name}").raise_if_error()
|
||||
|
||||
logger.info("Successfully pushed!")
|
||||
|
||||
except Exception as e:
|
||||
logger.info(f"Git Operation Failed: {e}")
|
||||
+31
-6
@@ -1,8 +1,9 @@
|
||||
import re
|
||||
from pathlib import Path
|
||||
from typing import ClassVar
|
||||
|
||||
import yaml
|
||||
from pydantic import BaseModel
|
||||
from pydantic import BaseModel, field_validator
|
||||
|
||||
from core.enums import ServiceStatus
|
||||
|
||||
@@ -16,12 +17,36 @@ class Service(BaseModel):
|
||||
name: str
|
||||
status: ServiceStatus
|
||||
|
||||
@property
|
||||
def name_upper(self):
|
||||
return self.name.upper()
|
||||
|
||||
@property
|
||||
def name_lower(self):
|
||||
return self.name.lower()
|
||||
|
||||
@property
|
||||
def dir_name(self):
|
||||
return f"{self.id_str}-{self.name_lower}"
|
||||
|
||||
@property
|
||||
def id_str(self):
|
||||
# TODO: max 999 services, enough?
|
||||
return f"{self.id:03d}"
|
||||
|
||||
def to_dict(self):
|
||||
return {"name": self.name, "status": str(self.status)}
|
||||
return {"name": self.name_lower, "status": str(self.status)}
|
||||
|
||||
class Config:
|
||||
use_enum_values = True
|
||||
|
||||
@field_validator("name")
|
||||
@classmethod
|
||||
def validate_name_chars(cls, value: str) -> str:
|
||||
if not re.match(r"^[a-zA-Z0-9-]+$", value):
|
||||
raise ValueError("name can only contain letters, numbers, and dashes")
|
||||
return value
|
||||
|
||||
|
||||
class ServiceRegistry(BaseModel):
|
||||
config_path: ClassVar[str] = str(SERVICE_REGISTRY_FILENAME)
|
||||
@@ -40,11 +65,11 @@ class ServiceRegistry(BaseModel):
|
||||
def get_services(self) -> list[Service]:
|
||||
return [Service(id=svc_id, **svc_data) for svc_id, svc_data in self.services.items()]
|
||||
|
||||
def add_service(self, name: str) -> int:
|
||||
s = Service(id=self.next_available_id, name=name, status=ServiceStatus.ACTIVE)
|
||||
self.services.update({s.id: s.to_dict()})
|
||||
def add_service(self, name: str) -> Service:
|
||||
svc = Service(id=self.next_available_id, name=name, status=ServiceStatus.ACTIVE)
|
||||
self.services.update({svc.id: svc.to_dict()})
|
||||
self.next_available_id += 1
|
||||
with open(self.config_path, "w") as f:
|
||||
yaml.safe_dump(self.model_dump(), f)
|
||||
|
||||
return self.next_available_id
|
||||
return svc
|
||||
|
||||
@@ -8,8 +8,9 @@ SERVICE_DEST_DIR = REPO_ROOT / "services"
|
||||
|
||||
|
||||
def render_new_service_templates(
|
||||
service_id: int,
|
||||
service_name: str,
|
||||
service_id: str,
|
||||
service_name_upper: str,
|
||||
service_name_lower: str,
|
||||
ioc_host: str,
|
||||
ioc_port: int,
|
||||
user: str,
|
||||
@@ -22,9 +23,8 @@ def render_new_service_templates(
|
||||
dst_path=str(SERVICE_DEST_DIR),
|
||||
data={
|
||||
"service_id": service_id,
|
||||
"service_name": service_name,
|
||||
# TODO: make sure this is ok where used (need to replace some chars by '-'?)
|
||||
"service_name_lower": service_name.lower(),
|
||||
"service_name_upper": service_name_upper,
|
||||
"service_name_lower": service_name_lower,
|
||||
# TODO: restrict this, use enum
|
||||
"ioc_host": ioc_host,
|
||||
"ioc_port": ioc_port,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
import git
|
||||
@@ -8,3 +9,10 @@ def get_git_root(path) -> Path:
|
||||
git_root = git_repo.git.rev_parse("--show-toplevel")
|
||||
|
||||
return Path(git_root)
|
||||
|
||||
|
||||
def init_logging(log_level: str = "info"):
|
||||
"""
|
||||
Configure logging -- set log level
|
||||
"""
|
||||
logging.basicConfig(level=getattr(logging, log_level.upper()))
|
||||
|
||||
+18
-6
@@ -1,24 +1,36 @@
|
||||
import typer
|
||||
|
||||
from core.git import git_push_service_directory
|
||||
from core.models import ServiceRegistry
|
||||
from core.render_templates import render_new_service_templates
|
||||
from core.utils import get_git_root, init_logging
|
||||
|
||||
service = typer.Typer(no_args_is_help=True)
|
||||
|
||||
REPO_ROOT = get_git_root(__file__)
|
||||
|
||||
|
||||
@service.command()
|
||||
def add(
|
||||
name: str = typer.Option(..., "--name", "-n"),
|
||||
):
|
||||
init_logging()
|
||||
# TODO: add restriction on name (maybe: [a-zA-Z0-9-])
|
||||
registry = ServiceRegistry.read_from_config()
|
||||
service_id = registry.add_service(name)
|
||||
service = registry.add_service(name)
|
||||
render_new_service_templates(
|
||||
service_id=service_id,
|
||||
service_name=name,
|
||||
ioc_host="",
|
||||
ioc_port=9999,
|
||||
user="",
|
||||
service_id=service.id_str,
|
||||
service_name_upper=service.name_upper,
|
||||
service_name_lower=service.name_lower,
|
||||
ioc_host="ex-host", # TODO
|
||||
ioc_port=9999, # TODO
|
||||
user="user", # TODO
|
||||
)
|
||||
git_push_service_directory(
|
||||
repo_root=REPO_ROOT,
|
||||
branch_name=f"feature/add-service-{service.name_lower}",
|
||||
service_dir_rel_path=f"services/{service.dir_name}",
|
||||
commit_msg=f"feature: add new service {service.name_lower}",
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user