From 3740996db3ac247b95f3c24004b524b218ca122a Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 8 Mar 2021 22:29:28 +0100 Subject: [PATCH] Bugfixes --- src/cpl_cli/cli.py | 8 ++++---- src/cpl_cli/command_handler.py | 7 +++++++ src/cpl_cli/command_model.py | 7 ++++++- src/cpl_cli/error.py | 2 +- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/cpl_cli/cli.py b/src/cpl_cli/cli.py index e8cc4c52..8ca039b3 100644 --- a/src/cpl_cli/cli.py +++ b/src/cpl_cli/cli.py @@ -20,10 +20,10 @@ class CLI(ApplicationABC): def configure(self): self._command_handler: CommandHandler = self._services.get_service(CommandHandler) - self._command_handler.add_command(CommandModel('build', ['h', 'B'], Build)) - self._command_handler.add_command(CommandModel('help', ['h', 'H'], Help)) - self._command_handler.add_command(CommandModel('publish', ['p', 'P'], Publish)) - self._command_handler.add_command(CommandModel('version', ['v', 'V'], Version)) + self._command_handler.add_command(CommandModel('build', ['h', 'B'], Build, True)) + self._command_handler.add_command(CommandModel('help', ['h', 'H'], Help, False)) + self._command_handler.add_command(CommandModel('publish', ['p', 'P'], Publish, True)) + self._command_handler.add_command(CommandModel('version', ['v', 'V'], Version, False)) def main(self): if len(self._configuration.additional_arguments) < 1: diff --git a/src/cpl_cli/command_handler.py b/src/cpl_cli/command_handler.py index a163eace..890a055b 100644 --- a/src/cpl_cli/command_handler.py +++ b/src/cpl_cli/command_handler.py @@ -1,6 +1,9 @@ +import os + from cpl.application.application_runtime_abc import ApplicationRuntimeABC from cpl.dependency_injection.service_abc import ServiceABC from cpl.dependency_injection.service_provider_abc import ServiceProviderABC +from cpl_cli.error import Error from cpl_cli.command_model import CommandModel @@ -23,4 +26,8 @@ class CommandHandler(ServiceABC): def handle(self, cmd: str, args: list[str]): for command in self._commands: if cmd == command.name or cmd in command.aliases: + if command.is_project_needed and not os.path.isfile(os.path.join(self._runtime.working_directory, 'cpl.json')): + Error.error('The command requires to be run in an CPL project, but a project could not be found.') + return + self._services.get_service(command.command).run(args) diff --git a/src/cpl_cli/command_model.py b/src/cpl_cli/command_model.py index 5d6f57fb..85afe007 100644 --- a/src/cpl_cli/command_model.py +++ b/src/cpl_cli/command_model.py @@ -5,10 +5,11 @@ from cpl_cli.command_abc import CommandABC class CommandModel: - def __init__(self, name: str, aliases: list[str], command: Callable[CommandABC]): + def __init__(self, name: str, aliases: list[str], command: Callable[CommandABC], is_project_needed: bool): self._name = name self._aliases = aliases self._command = command + self._is_project_needed = is_project_needed @property def name(self) -> str: @@ -21,3 +22,7 @@ class CommandModel: @property def command(self) -> Callable[CommandABC]: return self._command + + @property + def is_project_needed(self) -> bool: + return self._is_project_needed diff --git a/src/cpl_cli/error.py b/src/cpl_cli/error.py index 21f4cf50..eb2456ef 100644 --- a/src/cpl_cli/error.py +++ b/src/cpl_cli/error.py @@ -6,4 +6,4 @@ class Error: @staticmethod def error(message: str): Console.error(message) - Console.error('Run \'cpl help\'') + Console.error('Run \'cpl help\'\n')