diff --git a/kiwi_scp/commands/cmd_new.py b/kiwi_scp/commands/cmd_new.py new file mode 100644 index 0000000..8fa2a35 --- /dev/null +++ b/kiwi_scp/commands/cmd_new.py @@ -0,0 +1,39 @@ +import os +import shutil + +import click + +from .cmd import KiwiCommandType, KiwiCommand +from .decorators import kiwi_command +from .._constants import DEFAULT_DOCKER_COMPOSE_NAME, COMPOSE_FILE_NAME +from ..config import ProjectConfig +from ..instance import Instance +from ..project import Project + + +@kiwi_command() +class NewCommand(KiwiCommand): + """Create new empty project(s) in this instance""" + + type = KiwiCommandType.PROJECTS + + @classmethod + def run_for_project(cls, instance: Instance, project: Project, **kwargs) -> None: + KiwiCommand.print_error(f"Project {project.name} already exists!") + + @classmethod + def run_for_new_project(cls, instance: Instance, project_name: str, **kwargs) -> None: + try: + os.mkdir(project_name) + instance.config.projects.append(ProjectConfig( + name=project_name, + enabled=False, + )) + shutil.copy( + DEFAULT_DOCKER_COMPOSE_NAME, + instance.directory.joinpath(project_name).joinpath(COMPOSE_FILE_NAME) + ) + instance.save_config(instance.config) + + except FileExistsError: + KiwiCommand.print_error(f"Project directory {project_name} already exists!") diff --git a/kiwi_scp/data/etc/docker-compose_default.yml b/kiwi_scp/data/etc/docker-compose_default.yml index d3cbd6d..cc6476a 100644 --- a/kiwi_scp/data/etc/docker-compose_default.yml +++ b/kiwi_scp/data/etc/docker-compose_default.yml @@ -10,6 +10,10 @@ networks: name: ${KIWI_HUB_NAME} services: + ###################### + # START EDITING HERE # + ###################### + # an example service something: # uses an image diff --git a/kiwi_scp/subcommands/new.py b/kiwi_scp/subcommands/new.py deleted file mode 100644 index 5fc5243..0000000 --- a/kiwi_scp/subcommands/new.py +++ /dev/null @@ -1,30 +0,0 @@ -# system -import logging -import os -import shutil - -# local -from .._constants import DEFAULT_DOCKER_COMPOSE_NAME -from ..subcommand import ProjectCommand - - -class NewCommand(ProjectCommand): - """kiwi new""" - - def __init__(self): - super().__init__( - 'new', num_projects='+', - action="Creating", - description="Create new empty project(s) in this instance" - ) - - def _run_project(self, runner, args, project): - if project.exists(): - logging.error(f"Project '{project.get_name()}' exists in this instance!") - return False - - else: - os.mkdir(project.disabled_dir_name()) - shutil.copy(DEFAULT_DOCKER_COMPOSE_NAME, project.compose_file_name()) - logging.debug(f"Project '{project.get_name()}' created") - return True