This commit is contained in:
Sven Heidemann 2021-03-08 22:29:28 +01:00
parent 82ea419088
commit 3740996db3
4 changed files with 18 additions and 6 deletions

View File

@ -20,10 +20,10 @@ class CLI(ApplicationABC):
def configure(self): def configure(self):
self._command_handler: CommandHandler = self._services.get_service(CommandHandler) 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('build', ['h', 'B'], Build, True))
self._command_handler.add_command(CommandModel('help', ['h', 'H'], Help)) self._command_handler.add_command(CommandModel('help', ['h', 'H'], Help, False))
self._command_handler.add_command(CommandModel('publish', ['p', 'P'], Publish)) self._command_handler.add_command(CommandModel('publish', ['p', 'P'], Publish, True))
self._command_handler.add_command(CommandModel('version', ['v', 'V'], Version)) self._command_handler.add_command(CommandModel('version', ['v', 'V'], Version, False))
def main(self): def main(self):
if len(self._configuration.additional_arguments) < 1: if len(self._configuration.additional_arguments) < 1:

View File

@ -1,6 +1,9 @@
import os
from cpl.application.application_runtime_abc import ApplicationRuntimeABC from cpl.application.application_runtime_abc import ApplicationRuntimeABC
from cpl.dependency_injection.service_abc import ServiceABC from cpl.dependency_injection.service_abc import ServiceABC
from cpl.dependency_injection.service_provider_abc import ServiceProviderABC from cpl.dependency_injection.service_provider_abc import ServiceProviderABC
from cpl_cli.error import Error
from cpl_cli.command_model import CommandModel from cpl_cli.command_model import CommandModel
@ -23,4 +26,8 @@ class CommandHandler(ServiceABC):
def handle(self, cmd: str, args: list[str]): def handle(self, cmd: str, args: list[str]):
for command in self._commands: for command in self._commands:
if cmd == command.name or cmd in command.aliases: 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) self._services.get_service(command.command).run(args)

View File

@ -5,10 +5,11 @@ from cpl_cli.command_abc import CommandABC
class CommandModel: 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._name = name
self._aliases = aliases self._aliases = aliases
self._command = command self._command = command
self._is_project_needed = is_project_needed
@property @property
def name(self) -> str: def name(self) -> str:
@ -21,3 +22,7 @@ class CommandModel:
@property @property
def command(self) -> Callable[CommandABC]: def command(self) -> Callable[CommandABC]:
return self._command return self._command
@property
def is_project_needed(self) -> bool:
return self._is_project_needed

View File

@ -6,4 +6,4 @@ class Error:
@staticmethod @staticmethod
def error(message: str): def error(message: str):
Console.error(message) Console.error(message)
Console.error('Run \'cpl help\'') Console.error('Run \'cpl help\'\n')