Skip to content

Commit 318ded8

Browse files
authored
Merge pull request #79 from minos-framework/issue-73-create-microservice-in-microservices-directory
#73 - Microservices path.
2 parents 591a63a + eb17d06 commit 318ded8

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

minos/cli/api/new.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
from ..consoles import (
88
console,
99
)
10+
from ..pathlib import (
11+
get_microservices_directory,
12+
)
1013
from ..templating import (
1114
TemplateFetcher,
1215
TemplateProcessor,
@@ -27,13 +30,19 @@ def new_project(path: Path) -> None:
2730

2831

2932
@app.command("microservice")
30-
def new_microservice(path: Path) -> None:
33+
def new_microservice(name: str) -> None:
3134
"""Initialize a microservice on the given directory."""
3235

3336
console.print(":wrench: Creating new Microservice...\n")
3437

38+
try:
39+
microservice_path = get_microservices_directory(Path.cwd()) / name
40+
except ValueError:
41+
console.print("No Minos project found. Consider 'minos project init'")
42+
raise typer.Exit(code=1)
43+
3544
fetcher = TemplateFetcher.from_name("microservice-init")
36-
processor = TemplateProcessor.from_fetcher(fetcher, path.absolute(), defaults={"name": path.name})
45+
processor = TemplateProcessor.from_fetcher(fetcher, microservice_path, defaults={"name": name})
3746
processor.render()
3847

3948

minos/cli/pathlib.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
Path,
33
)
44

5+
MINOS_PROJECT_FILENAME = ".minos-project.yaml"
6+
MINOS_MICROSERVICE_FILENAME = ".minos-microservice.yaml"
7+
MICROSERVICES_DIRECTORY = "microservices"
8+
59

610
def get_project_target_directory(path: Path) -> Path:
711
"""Get the target directory for a project.
@@ -10,9 +14,10 @@ def get_project_target_directory(path: Path) -> Path:
1014
"""
1115
current = path
1216
while current != current.parent:
13-
if (current / ".minos-project.yaml").exists():
17+
if (current / MINOS_PROJECT_FILENAME).exists():
1418
return current
15-
current = current.parent
19+
else:
20+
current = current.parent
1621

1722
raise ValueError(f"Unable to find the target directory from {path} origin.")
1823

@@ -26,13 +31,18 @@ def get_microservice_target_directory(path: Path, name: str) -> Path:
2631
"""
2732
current = path
2833
while current != current.parent:
29-
if (current / ".minos-microservice.yaml").exists():
34+
if (current / MINOS_MICROSERVICE_FILENAME).exists():
3035
return current
3136

32-
if (current / ".minos-project.yaml").exists():
33-
target = current / "microservices" / name
34-
if (target / ".minos-microservice.yaml").exists():
37+
if (current / MINOS_PROJECT_FILENAME).exists():
38+
target = current / MICROSERVICES_DIRECTORY / name
39+
if (target / MINOS_MICROSERVICE_FILENAME).exists():
3540
return target
3641
current = current.parent
3742

3843
raise ValueError(f"Unable to find the target directory for {name} from {path} origin.")
44+
45+
46+
def get_microservices_directory(path: Path) -> Path:
47+
project_directory = get_project_target_directory(path)
48+
return project_directory / MICROSERVICES_DIRECTORY

tests/test_cli/test_api/test_new.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121

2222

2323
class TestNew(unittest.TestCase):
24+
def setUp(self) -> None:
25+
self.minos_project_file = Path.cwd() / ".minos-project.yaml"
26+
self.minos_project_file.touch()
27+
28+
def tearDown(self) -> None:
29+
if self.minos_project_file.exists():
30+
self.minos_project_file.unlink()
31+
2432
def test_main(self):
2533
self.assertEqual(__main__.main, main)
2634

@@ -44,6 +52,18 @@ def test_new_microservice(self) -> None:
4452

4553
self.assertEqual(1, mock.call_count)
4654

55+
def test_new_microservice_no_project_file(self) -> None:
56+
self.minos_project_file.unlink()
57+
58+
with TemporaryDirectory() as tmp_dir_name:
59+
path = Path(tmp_dir_name) / "product"
60+
with patch("minos.cli.TemplateProcessor.render") as mock:
61+
result = CliRunner().invoke(app, ["new", "microservice", str(path)])
62+
63+
self.assertEqual(1, result.exit_code)
64+
65+
self.assertEqual(0, mock.call_count)
66+
4767

4868
if __name__ == "__main__":
4969
unittest.main()

0 commit comments

Comments
 (0)