diff --git a/kiwi_scp/instance.py b/kiwi_scp/instance.py index 4eda01f..b22ef1c 100644 --- a/kiwi_scp/instance.py +++ b/kiwi_scp/instance.py @@ -1,7 +1,7 @@ import functools import re from pathlib import Path -from typing import Generator, List +from typing import Generator, List, Tuple, Optional import attr import click @@ -24,7 +24,7 @@ class Service: "service": { self.name: self.description } - }) + }).strip() @property def configs(self) -> Generator[Path, None, None]: @@ -36,7 +36,7 @@ class Service: cd_match = _RE_CONFDIR.match(host_part) if cd_match: - yield cd_match.group(1) + yield Path(cd_match.group(1)) @attr.s @@ -50,7 +50,8 @@ class Services: service.name: service.description for service in self.content } - }) + }).strip() + @attr.s class Instance: @@ -68,18 +69,21 @@ class Instance: with open(directory.joinpath(COMPOSE_FILE_NAME), "r") as cf: return YAML().load(cf) - def get_services(self, project_name: str) -> Services: + def get_services(self, project_name: str, service_names: Optional[Tuple[str]] = None) -> Services: yml = Instance._parse_compose_file(self.directory.joinpath(project_name)) - - return Services(project_name, [ + services = [ Service(name, description) for name, description in yml["services"].items() - ]) + ] - def get_service(self, project_name: str, service_name: str) -> Service: - yml = Instance._parse_compose_file(self.directory.joinpath(project_name)) - - return Service(service_name, yml["services"][service_name]) + if service_names is None: + return Services(project_name, services) + else: + return Services(project_name, [ + service + for service in services + if service.name in service_names + ]) pass_instance = click.make_pass_decorator(Instance, ensure=True) diff --git a/tests/test_instance.py b/tests/test_instance.py index 8e4f47e..81d8d1b 100644 --- a/tests/test_instance.py +++ b/tests/test_instance.py @@ -14,14 +14,13 @@ class TestDefault: assert p.name == "hello-world.project" - ss = list(i.get_services(p.name)) + ss = i.get_services(p.name) - assert len(ss) == 5 + assert len(ss.content) == 5 - s = ss[0] + s = ss.content[0] assert s.name == "greeter" - assert s == i.get_service(p.name, s.name) def test_empty(self): i = Instance() diff --git a/tests/test_service.py b/tests/test_service.py index d0198a7..42244c8 100644 --- a/tests/test_service.py +++ b/tests/test_service.py @@ -1,60 +1,62 @@ from pathlib import Path +from ruamel.yaml import CommentedMap + from kiwi_scp.instance import Service class TestDefault: - def test_no_description(self): - s = Service.from_description( + def test_empty(self): + s = Service( name="s", - description={}, + description=CommentedMap(), ) assert s.name == "s" - assert s.configs == [] + assert list(s.configs) == [] def test_no_configs(self): - s = Service.from_description( + s = Service( name="s", - description={ + description=CommentedMap({ "image": "repo/image:tag", - }, + }), ) assert s.name == "s" - assert s.configs == [] + assert list(s.configs) == [] def test_no_configs_in_volumes(self): - s = Service.from_description( + s = Service( name="s", - description={ + description=CommentedMap({ "image": "repo/image:tag", "volumes": [ "docker_volume/third/dir:/path/to/third/mountpoint", "${TARGETDIR}/some/dir:/path/to/some/mountpoint", "$TARGETDIR/other/dir:/path/to/other/mountpoint", ] - }, + }), ) assert s.name == "s" - assert s.configs == [] + assert list(s.configs) == [] def test_with_configs(self): - s = Service.from_description( + s = Service( name="s", - description={ + description=CommentedMap({ "image": "repo/image:tag", "volumes": [ "${CONFDIR}/some/config:/path/to/some/config", "$CONFDIR/other/config:/path/to/other/config", ] - }, + }), ) assert s.name == "s" - assert len(s.configs) == 2 - assert s.configs == [ + assert len(list(s.configs)) == 2 + assert list(s.configs) == [ Path("some/config"), Path("other/config"), ]