diff options
author | crupest <crupest@outlook.com> | 2024-11-11 01:12:29 +0800 |
---|---|---|
committer | Yuqian Yang <crupest@crupest.life> | 2025-01-08 22:37:23 +0800 |
commit | 43892b892cfdc4e15f7ab191c42ccb32279fd7f6 (patch) | |
tree | 9161420d397b95d24a0ac90629e0eab27f1b337f /tools/cru-py/cru/service/__main__.py | |
parent | c3308421b665e5d8dcb70b78acf137541a026555 (diff) | |
download | crupest-43892b892cfdc4e15f7ab191c42ccb32279fd7f6.tar.gz crupest-43892b892cfdc4e15f7ab191c42ccb32279fd7f6.tar.bz2 crupest-43892b892cfdc4e15f7ab191c42ccb32279fd7f6.zip |
HALF WORK: 2024.1.8
Diffstat (limited to 'tools/cru-py/cru/service/__main__.py')
-rw-r--r-- | tools/cru-py/cru/service/__main__.py | 54 |
1 files changed, 47 insertions, 7 deletions
diff --git a/tools/cru-py/cru/service/__main__.py b/tools/cru-py/cru/service/__main__.py index a7add4d..923c25b 100644 --- a/tools/cru-py/cru/service/__main__.py +++ b/tools/cru-py/cru/service/__main__.py @@ -1,11 +1,51 @@ -import argparse +from pathlib import Path +from cru import CruException -arg_parser = argparse.ArgumentParser(description="Service management") -command_subparser = arg_parser.add_subparsers(dest="command") +from ._base import AppBase, DATA_DIR_NAME, CommandDispatcher +from ._config import ConfigManager +from ._data import DataManager +from ._template import TemplateManager -template_parser = command_subparser.add_parser("template", help="Template management") -template_subparser = template_parser.add_subparsers(dest="template_command") -template_subparser.add_parser('list', description="List templates") -template_subparser.add_parser('generate') +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() |