1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-25 05:53:00 +00:00
kiwi-scp/kiwi_scp/commands/cmd_new.py

42 lines
1.4 KiB
Python
Raw Normal View History

2022-01-20 09:21:05 +00:00
import os
import shutil
from .cmd import KiwiCommandType, KiwiCommand
from .decorators import kiwi_command
2022-01-24 15:53:19 +00:00
from .._constants import DEFAULT_DOCKER_COMPOSE_NAME, COMPOSE_FILE_NAME, RESERVED_PROJECT_NAMES
2022-01-20 09:21:05 +00:00
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:
2022-01-24 15:53:19 +00:00
if project_name in RESERVED_PROJECT_NAMES:
KiwiCommand.print_error(f"Project name '{project_name}' is reserved!")
return
2022-01-20 09:21:05 +00:00
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!")