Skip to content

Commit

Permalink
Merge pull request #435 from datacoves/config-env-vars-defaults
Browse files Browse the repository at this point in the history
fix: Fixes to Config's env_var usage
  • Loading branch information
mprunell authored Feb 6, 2024
2 parents baf1d76 + 11b1941 commit b17fc2f
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions dbt_coves/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,18 +280,16 @@ def load_and_validate_config_yaml(self) -> None:
self._config = ConfigModel(**yaml_dict)

def replace_env_vars(self, yaml_dict: Dict) -> Dict:
# Define a regular expression pattern to find placeholders
pattern = r"\{\{\s*env_var\(['\"]([^'\"]+)['\"]\)\s*\}\}"
# regex -> {{ env_var('ENV_VAR_NAME', 'DEFAULT_VALUE') }}
env_var_pattern = re.compile(
r"\{\{\s*env_var\s*\(\s*['\"]?\s*([^'\"]+)['\"]?\s*(?:,\s*['\"]?\s*([^'\"]+)['\"]?\s*)?\)\s*\}\}"
)

# Iterate through the YAML dictionary and replace placeholders
def replace(match):
def replace_env_var(match):
env_var_name = match.group(1)
try:
return os.environ[env_var_name]
except KeyError:
raise ValueError(
f"Configuration environment variable [red]{env_var_name}[/red] not found"
)
default_value = match.group(2) or ""
return os.environ.get(env_var_name, default_value)

def replace_recursively(obj):
if isinstance(obj, dict):
Expand All @@ -301,7 +299,7 @@ def replace_recursively(obj):
for i, item in enumerate(obj):
obj[i] = replace_recursively(item)
elif isinstance(obj, str):
obj = re.sub(pattern, replace, obj)
obj = env_var_pattern.sub(replace_env_var, obj)
return obj

return replace_recursively(yaml_dict)
Expand Down

0 comments on commit b17fc2f

Please sign in to comment.