Skip to content

Commit

Permalink
handle all file detection stuff in load config function
Browse files Browse the repository at this point in the history
  • Loading branch information
hubertdeng123 committed Aug 29, 2024
1 parent 04958e8 commit 83836cd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 33 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ repos:
additional_dependencies:
- "pytest"
- "types-PyYAML"
- "pydantic-settings"
language_version: python3.12
- repo: https://github.com/shellcheck-py/shellcheck-py
rev: v0.9.0.6
Expand Down
27 changes: 5 additions & 22 deletions src/commands/list_dependencies.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from __future__ import annotations

import os
from argparse import ArgumentParser, Namespace, _SubParsersAction

import yaml
from argparse import _SubParsersAction
from argparse import ArgumentParser
from argparse import Namespace

from utils.config import load_devservices_config
from utils.devenv import get_code_root


def add_parser(subparsers: _SubParsersAction[ArgumentParser]) -> None:
Expand All @@ -24,27 +22,12 @@ def add_parser(subparsers: _SubParsersAction[ArgumentParser]) -> None:

def list_dependencies(args: Namespace) -> None:
"""List the dependencies of a service."""
service_name = args.service_name
if not service_name:
current_dir = os.getcwd()
service_path = os.path.join(current_dir)
else:
code_root = get_code_root()
service_path = os.path.join(code_root, service_name)
try:
config = load_devservices_config(service_path)
except FileNotFoundError:
print(
f'Service "{service_name}" not found, or devservices config does not exist'
)
return
except yaml.YAMLError as e:
raise Exception(f"Failed to load service config: {e}")
config = load_devservices_config(args.service_name)

dependencies = config.devservices_config.dependencies

if not dependencies:
print(f"No dependencies found for {service_name}")
print(f"No dependencies found for {config.devservices_config.service_name}")
return

print(f"Dependencies of {config.devservices_config.service_name}:")
Expand Down
45 changes: 34 additions & 11 deletions src/utils/config.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
from __future__ import annotations

import os
from typing import Dict, List, Optional, Union
from typing import Dict
from typing import List
from typing import Optional
from typing import Union

import yaml
from pydantic import BaseModel, Field, model_validator, validator

from constants import DEVSERVICES_DIR_NAME, DOCKER_COMPOSE_FILE_NAME
from constants import DEVSERVICES_DIR_NAME
from constants import DOCKER_COMPOSE_FILE_NAME
from pydantic import BaseModel
from pydantic import Field
from pydantic import model_validator
from pydantic import validator
from utils.devenv import get_code_root


class Dependency(BaseModel):
Expand Down Expand Up @@ -48,7 +55,9 @@ def check_modes(
modes: Dict[str, List[str]],
values: Dict[str, Union[float, str, Dict[str, Dependency]]],
) -> Dict[str, List[str]]:
dependencies: Dict[str, Dependency] = values.get("dependencies", {})
dependencies = values.get("dependencies", {})
if not isinstance(dependencies, dict):
raise ValueError("Dependencies must be a dictionary")
for mode, services in modes.items():
for service in services:
if service not in dependencies:
Expand Down Expand Up @@ -85,13 +94,27 @@ def check_services_match(self) -> Config:
return self


def load_devservices_config(service_path: str) -> Dict[str, Dict[str, str]]:
def load_devservices_config(service_name: Optional[str]) -> Config:
"""Load the devservices config for a service."""
config_path = os.path.join(
service_path, DEVSERVICES_DIR_NAME, DOCKER_COMPOSE_FILE_NAME
)
if not os.path.exists(config_path):
raise FileNotFoundError(f"Config file not found: {config_path}")
if not service_name:
current_dir = os.getcwd()
config_path = os.path.join(
current_dir, DEVSERVICES_DIR_NAME, DOCKER_COMPOSE_FILE_NAME
)
if not os.path.exists(config_path):
raise FileNotFoundError(
f"Config file not found in current directory: {config_path}"
)
else:
code_root = get_code_root()
service_path = os.path.join(code_root, service_name)
config_path = os.path.join(
service_path, DEVSERVICES_DIR_NAME, DOCKER_COMPOSE_FILE_NAME
)
if not os.path.exists(config_path):
raise FileNotFoundError(
f"Config file for {service_name} not found from code root: {config_path}"
)
with open(config_path, "r") as stream:
try:
config = yaml.safe_load(stream)
Expand Down

0 comments on commit 83836cd

Please sign in to comment.