Skip to content

Commit

Permalink
Merge pull request #8 from getsentry/hubertdeng123/add-load-service-f…
Browse files Browse the repository at this point in the history
…unction

Add reusable load_service_config function
  • Loading branch information
hubertdeng123 authored Sep 9, 2024
2 parents ef5fa60 + c28ad4f commit 8be8c30
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 46 deletions.
33 changes: 10 additions & 23 deletions src/commands/list_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@
from argparse import _SubParsersAction
from argparse import ArgumentParser
from argparse import Namespace
from typing import Optional

from configs.service_config import load_service_config
from configs.service_config import ServiceConfig
from exceptions import ConfigError
from exceptions import ServiceNotFoundError
from services import find_matching_service
from utils.services import find_matching_service


def add_parser(subparsers: _SubParsersAction[ArgumentParser]) -> None:
Expand All @@ -29,26 +24,18 @@ def list_dependencies(args: Namespace) -> None:
"""List the dependencies of a service."""
service_name = args.service_name

service_config: Optional[ServiceConfig] = None
if service_name is not None:
try:
service_config = find_matching_service(service_name).service_config
except ServiceNotFoundError as e:
print(e)
return
else:
try:
service_config = load_service_config()
except ConfigError as e:
print(e)
return

dependencies = service_config.dependencies
try:
service = find_matching_service(service_name)
except Exception as e:
print(e)
exit(1)

dependencies = service.config.dependencies

if not dependencies:
print(f"No dependencies found for {service_config.service_name}")
print(f"No dependencies found for {service.name}")
return

print(f"Dependencies of {service_config.service_name}:")
print(f"Dependencies of {service.name}:")
for dependency_key, dependency_info in dependencies.items():
print("-", dependency_key, ":", dependency_info.description)
2 changes: 1 addition & 1 deletion src/commands/list_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from argparse import ArgumentParser
from argparse import Namespace

from services import get_local_services
from utils.devenv import get_coderoot
from utils.services import get_local_services


def add_parser(subparsers: _SubParsersAction[ArgumentParser]) -> None:
Expand Down
10 changes: 9 additions & 1 deletion src/commands/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from argparse import ArgumentParser
from argparse import Namespace

from utils.services import find_matching_service


def add_parser(subparsers: _SubParsersAction[ArgumentParser]) -> None:
parser = subparsers.add_parser("start", help="Start a service and its dependencies")
Expand All @@ -14,6 +16,12 @@ def add_parser(subparsers: _SubParsersAction[ArgumentParser]) -> None:
def start(args: Namespace) -> None:
"""Start a service and its dependencies."""
service_name = args.service_name

try:
service = find_matching_service(service_name)
except Exception as e:
print(e)
exit(1)
# Implementation here
print(f"Starting service: {service_name}")
print(f"Starting service: {service.name}")
# Use docker_compose utility to start the service
11 changes: 10 additions & 1 deletion src/commands/stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from argparse import ArgumentParser
from argparse import Namespace

from utils.services import find_matching_service


def add_parser(subparsers: _SubParsersAction[ArgumentParser]) -> None:
parser = subparsers.add_parser("stop", help="Stop a service and its dependencies")
Expand All @@ -14,6 +16,13 @@ def add_parser(subparsers: _SubParsersAction[ArgumentParser]) -> None:
def stop(args: Namespace) -> None:
"""Stop a service and its dependencies."""
service_name = args.service_name

try:
service = find_matching_service(service_name)
except Exception as e:
print(e)
exit(1)

# Implementation here
print(f"Stopping service: {service_name}")
print(f"Stopping service: {service.name}")
# Use docker_compose utility to stop the service
17 changes: 2 additions & 15 deletions src/configs/service_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from dataclasses import dataclass
from typing import Dict
from typing import List
from typing import Optional

import yaml
from constants import DEVSERVICES_DIR_NAME
Expand All @@ -17,7 +16,7 @@
@dataclass
class Dependency:
description: str
link: Optional[str] = None
link: str | None = None


@dataclass
Expand All @@ -44,19 +43,12 @@ def _validate(self) -> None:
)


@dataclass
class Config:
service_config: ServiceConfig


def load_service_config_from_file(repo_path: str) -> ServiceConfig:
config_path = os.path.join(
repo_path, DEVSERVICES_DIR_NAME, DOCKER_COMPOSE_FILE_NAME
)
if not os.path.exists(config_path):
raise ConfigNotFoundError(
f"Config file not found in current directory: {config_path}"
)
raise ConfigNotFoundError(f"Config file not found in directory: {config_path}")
with open(config_path, "r") as stream:
try:
config = yaml.safe_load(stream)
Expand All @@ -81,8 +73,3 @@ def load_service_config_from_file(repo_path: str) -> ServiceConfig:
raise ConfigParseError(
f"Error parsing config file: {config_path}"
) from yml_error


def load_service_config() -> ServiceConfig:
"""Load the service config for the current directory."""
return load_service_config_from_file(os.getcwd())
3 changes: 1 addition & 2 deletions src/utils/docker_compose.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from __future__ import annotations

import subprocess
from typing import Optional


def run_docker_compose_command(
command: str, service: Optional[str] = None
command: str, service: str | None = None
) -> subprocess.CompletedProcess[str]:
cmd = ["docker", "compose"] + command.split()
if service:
Expand Down
17 changes: 14 additions & 3 deletions src/services.py → src/utils/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class Service:
name: str
repo_path: str
service_config: ServiceConfig
config: ServiceConfig


def get_local_services(coderoot: str) -> List[Service]:
Expand All @@ -35,14 +35,25 @@ def get_local_services(coderoot: str) -> List[Service]:
Service(
name=service_name,
repo_path=repo_path,
service_config=service_config,
config=service_config,
)
)
return services


def find_matching_service(service_name: str) -> Service:
def find_matching_service(service_name: str | None = None) -> Service:
"""Find a service with the given name."""
if service_name is None:
from configs.service_config import load_service_config_from_file

repo_path = os.getcwd()
service_config = load_service_config_from_file(repo_path)

return Service(
name=service_config.service_name,
repo_path=repo_path,
config=service_config,
)
coderoot = get_coderoot()
services = get_local_services(coderoot)
for service in services:
Expand Down

0 comments on commit 8be8c30

Please sign in to comment.