From f8e5f06eb71e2d8487e16c1d37d40d71aa9b3b33 Mon Sep 17 00:00:00 2001 From: 01Joseph-Hwang10 Date: Thu, 16 May 2024 04:44:08 +0900 Subject: [PATCH] fix: environment variable loading --- ldm/commands/_install.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/ldm/commands/_install.py b/ldm/commands/_install.py index c636686..0fc1c2c 100644 --- a/ldm/commands/_install.py +++ b/ldm/commands/_install.py @@ -1,8 +1,7 @@ import os from os import getcwd -from os.path import join +from os.path import join, exists from yaml_replace import YAMLTemplate -from os.path import exists from ._command import Command from ..api.installer import Installer from ..api.config import CONFIG_FILE_NAME, DependencyConfig @@ -17,9 +16,9 @@ def run(self, targets: list[str] | None = None): self.logger.error("No configuration file found") exit(1) - # Load environment variables - for env_file in config.config.environment.env_files: - load_dotenv(dotenv_path=join(getcwd(), env_file)) + self.logger.debug("Read configuration file: ") + for line in config.model_dump_json(indent=2).split("\n"): + self.logger.debug(line) Installer(logger=self.logger).install(config, targets) exit(0) @@ -28,6 +27,14 @@ def run(self, targets: list[str] | None = None): def read_config() -> DependencyConfig | None: if exists(CONFIG_FILE_NAME): with open(CONFIG_FILE_NAME, "r") as f: - config = YAMLTemplate(f.read()).render(dict(os.environ)) - return DependencyConfig(**config) + # Read configuration file + template = YAMLTemplate(f.read()) + config = DependencyConfig(**template.render({})) + + # Load environment variables + for env_file in config.config.environment.env_files: + load_dotenv(dotenv_path=join(getcwd(), env_file)) + + # Rerender configuration file with environment variables + return DependencyConfig(**template.render(os.environ)) return None