From da7de8d05e7cce341cec2a9213f657e7cfbe05a0 Mon Sep 17 00:00:00 2001 From: Cielquan Date: Fri, 7 Feb 2020 18:29:53 +0100 Subject: [PATCH] simplified path check for dotenv & htpasswd files --- src/dothdns/config.py | 15 +++++---------- src/dothdns/helpers.py | 26 ++++++++------------------ src/dothdns/subcommands/config.py | 6 +++--- 3 files changed, 16 insertions(+), 31 deletions(-) diff --git a/src/dothdns/config.py b/src/dothdns/config.py index abfcda0..96dfb77 100644 --- a/src/dothdns/config.py +++ b/src/dothdns/config.py @@ -39,10 +39,12 @@ ABS_PATH_HOME = Path.home() #: DoTH-DNS dir ABS_PATH_HOME_REPO_DIR = Path(ABS_PATH_HOME, "DoTH-DNS") -#: htpasswd file dir -ABS_PATH_HOME_REPO_DIR_HTPASSWD = Path( - ABS_PATH_HOME_REPO_DIR, "traefik-docker", "shared" +#: '.htpasswd' file +ABS_PATH_HOME_REPO_DIR_HTPASSWD_FILE = Path( + ABS_PATH_HOME_REPO_DIR, "traefik-docker", "shared", ".htpasswd" ) +#: '.env' file for docker-compose +ABS_PATH_HOME_REPO_DIR_DOTENV_FILE = Path(ABS_PATH_HOME_REPO_DIR, ".env") #: === PACKAGE PATHS === @@ -52,15 +54,8 @@ REL_PATH_PACKAGE_CONTAINER_CONFIGS_DIR = Path(REL_PATH_PACKAGE_DIR, "container_configs") -DOTENV_PATHS = [ABS_PATH_HOME_REPO_DIR] -DOTENV_FILES = [".env"] - - INI_PATHS = ( ABS_PATH_HOME_REPO_DIR, ABS_PATH_HOME, ) INI_FILES = ("dothdns.ini", ".dothdns.ini") - -HTPASSWD_PATHS = [ABS_PATH_HOME_REPO_DIR_HTPASSWD] -HTPASSWD_FILES = [".htpasswd"] diff --git a/src/dothdns/helpers.py b/src/dothdns/helpers.py index a8cb003..12bf421 100644 --- a/src/dothdns/helpers.py +++ b/src/dothdns/helpers.py @@ -34,9 +34,8 @@ import click from .config import ( + ABS_PATH_HOME_REPO_DIR_DOTENV_FILE, CHOICES_ARCHITECTURE, - DOTENV_FILES, - DOTENV_PATHS, INI_FILES, INI_PATHS, ) @@ -154,12 +153,7 @@ def get_env_file_data(env_file: Union[str, Path]) -> Dict[str, str]: def add_to_dotenv( - var_dict: Dict[str, str], - *, - overwrite: bool = False, - create: bool = True, - dotenv_paths: Iterable[Path] = DOTENV_PATHS, - dotenv_files: Iterable[str] = DOTENV_FILES, + var_dict: Dict[str, str], *, overwrite: bool = False, create: bool = True, ) -> Optional[str]: """Write Env Vars to '.env' file @@ -170,16 +164,16 @@ def add_to_dotenv( :param dotenv_files: Possible file names :return: Path of env file """ - env_file = file_finder(dotenv_paths, dotenv_files) + env_file = ABS_PATH_HOME_REPO_DIR_DOTENV_FILE - if env_file is None and create is not True: - return None - - if env_file: + if env_file.is_file(): #: Get env vars from file file_dict = get_env_file_data(env_file) + elif create is not True: + #: Abort if no file and creation is not permitted + return None else: - #: Set first option for .env file if no file was found + #: No file found -> no vars to load file_dict = {} #: Merge env vars from dict and file; set write mode to 'append' or 'write' @@ -189,10 +183,6 @@ def add_to_dotenv( dict_to_add = {**var_dict, **file_dict} #: Write env vars to .env file - if not env_file: - path_idx = 0 - env_file = Path(DOTENV_PATHS[path_idx], DOTENV_FILES[0]) - with open(str(env_file), "w") as file: file.writelines(f"{key.upper()}={dict_to_add[key]}\n" for key in dict_to_add) diff --git a/src/dothdns/subcommands/config.py b/src/dothdns/subcommands/config.py index 0e77d60..6dd60fb 100644 --- a/src/dothdns/subcommands/config.py +++ b/src/dothdns/subcommands/config.py @@ -36,8 +36,8 @@ import click import tzlocal # type: ignore -from ..config import CHOICES_ARCHITECTURE, HTPASSWD_FILES, HTPASSWD_PATHS -from ..helpers import CommandWithConfigFile, add_to_dotenv, file_finder +from ..config import ABS_PATH_HOME_REPO_DIR_HTPASSWD_FILE, CHOICES_ARCHITECTURE +from ..helpers import CommandWithConfigFile, add_to_dotenv from .init import create_config_dir @@ -151,7 +151,7 @@ def config( #: pylint: disable=C0330,R0912,R0913 env_dict.update(TIMEZONE=timezone) #: TRAEFIK_AUTH_MODE - if file_finder(HTPASSWD_PATHS, HTPASSWD_FILES) and ( + if ABS_PATH_HOME_REPO_DIR_HTPASSWD_FILE.is_file() and ( traefik_auth is True or traefik_auth is None ): env_dict.update(TRAEFIK_AUTH_MODE="Auth")