aboutsummaryrefslogtreecommitdiff
path: root/tools/cru-py/cru/service/__main__.py
blob: 923c25b630ab77ea546d74f2a3b652c22a4e8257 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from pathlib import Path

from cru import CruException

from ._base import AppBase, DATA_DIR_NAME, CommandDispatcher
from ._config import ConfigManager
from ._data import DataManager
from ._template import TemplateManager


class App(AppBase):
    def __init__(self, root: str):
        super().__init__("crupest-service", root)
        self.add_feature(DataManager())
        self.add_feature(ConfigManager())
        self.add_feature(TemplateManager())
        self.add_feature(CommandDispatcher())

    def setup(self):
        for feature in self.features:
            feature.setup()

    def run_command(self):
        command_dispatcher = self.get_feature(CommandDispatcher)
        command_dispatcher.run_command()


def _find_root() -> Path:
    cwd = Path.cwd()
    data_dir = cwd / DATA_DIR_NAME
    if data_dir.is_dir():
        return data_dir
    raise CruException(
        "No valid data directory found. Please run 'init' to create one."
    )


def create_app() -> App:
    root = _find_root()
    app = App(str(root))
    app.setup()
    return app


def main():
    app = create_app()
    app.run_command()


if __name__ == "__main__":
    main()