From 29bc413f8e69dae08b77a88c2f6a8f0c5accac81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Mon, 24 Jan 2022 16:53:19 +0100 Subject: [PATCH] RESERVED_PROJECT_NAMES --- kiwi_scp/_constants.py | 4 ++++ kiwi_scp/commands/cmd_new.py | 6 +++++- kiwi_scp/config.py | 12 +++++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/kiwi_scp/_constants.py b/kiwi_scp/_constants.py index 275fb52..e1e54cc 100644 --- a/kiwi_scp/_constants.py +++ b/kiwi_scp/_constants.py @@ -1,5 +1,9 @@ import os +RESERVED_PROJECT_NAMES = [ + 'config' +] + ############# # REGEX PARTS diff --git a/kiwi_scp/commands/cmd_new.py b/kiwi_scp/commands/cmd_new.py index 8fa2a35..f554661 100644 --- a/kiwi_scp/commands/cmd_new.py +++ b/kiwi_scp/commands/cmd_new.py @@ -5,7 +5,7 @@ import click from .cmd import KiwiCommandType, KiwiCommand from .decorators import kiwi_command -from .._constants import DEFAULT_DOCKER_COMPOSE_NAME, COMPOSE_FILE_NAME +from .._constants import DEFAULT_DOCKER_COMPOSE_NAME, COMPOSE_FILE_NAME, RESERVED_PROJECT_NAMES from ..config import ProjectConfig from ..instance import Instance from ..project import Project @@ -23,6 +23,10 @@ class NewCommand(KiwiCommand): @classmethod def run_for_new_project(cls, instance: Instance, project_name: str, **kwargs) -> None: + if project_name in RESERVED_PROJECT_NAMES: + KiwiCommand.print_error(f"Project name '{project_name}' is reserved!") + return + try: os.mkdir(project_name) instance.config.projects.append(ProjectConfig( diff --git a/kiwi_scp/config.py b/kiwi_scp/config.py index 20b01f3..3280f9a 100644 --- a/kiwi_scp/config.py +++ b/kiwi_scp/config.py @@ -5,7 +5,7 @@ from typing import Optional, Dict, List, Any, TextIO, Tuple from pydantic import BaseModel, constr, root_validator, validator -from ._constants import RE_SEMVER, RE_VARNAME, KIWI_CONF_NAME +from ._constants import RE_SEMVER, RE_VARNAME, KIWI_CONF_NAME, RESERVED_PROJECT_NAMES from .yaml import YAML @@ -53,6 +53,16 @@ class ProjectConfig(BaseModel): result["override_storage"] = self.override_storage.kiwi_dict return result + @validator("name") + @classmethod + def check_project(cls, value: str) -> str: + """check if project name is allowed""" + + if value in RESERVED_PROJECT_NAMES: + raise ValueError(f"Project name '{value}' is reserved!") + + return value + @validator("override_storage", pre=True) @classmethod def unify_storage(cls, value) -> Dict[str, Any]: