Skip to content

Commit

Permalink
simplified path check for dotenv & htpasswd files
Browse files Browse the repository at this point in the history
  • Loading branch information
Cielquan committed Feb 7, 2020
1 parent 7257a86 commit da7de8d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 31 deletions.
15 changes: 5 additions & 10 deletions src/dothdns/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ===
Expand All @@ -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"]
26 changes: 8 additions & 18 deletions src/dothdns/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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
Expand All @@ -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'
Expand All @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions src/dothdns/subcommands/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit da7de8d

Please sign in to comment.