Skip to content

Commit

Permalink
Envsubst (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
kraftp authored Jul 31, 2024
1 parent 29ab60b commit fc11d16
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
18 changes: 17 additions & 1 deletion dbos_transact/dbos_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json
import os
import re
from importlib import resources
from typing import Any, Dict, List, Optional, TypedDict

Expand Down Expand Up @@ -47,10 +49,24 @@ class ConfigFile(TypedDict):
env: Dict[str, str]


def substitute_env_vars(content: str) -> str:
regex = r"\$\{([^}]+)\}" # Regex to match ${VAR_NAME} style placeholders

def replace_func(match: re.Match[str]) -> str:
var_name = match.group(1)
return os.environ.get(
var_name, '""'
) # If the env variable is not set, return an empty string

return re.sub(regex, replace_func, content)


def load_config(configFilePath: str = "dbos-config.yaml") -> ConfigFile:
# Load the YAML file
with open(configFilePath, "r") as file:
data = yaml.safe_load(file)
content = file.read()
substituted_content = substitute_env_vars(content)
data = yaml.safe_load(substituted_content)

# Load the JSON schema relative to the package root
with resources.open_text("dbos_transact", "dbos-config.schema.json") as schema_file:
Expand Down
2 changes: 1 addition & 1 deletion templates/hello/dbos-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ database:
hostname: localhost
port: 5432
username: postgres
password: dbos
password: ${PGPASSWORD}
app_db_name: hello
telemetry:
logs:
Expand Down
9 changes: 7 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# type: ignore

import os
from unittest.mock import mock_open

import pytest
Expand Down Expand Up @@ -27,10 +28,13 @@ def test_valid_config(mocker):
hostname: 'some host'
port: 1234
username: 'some user'
password: abc123
password: ${PGPASSWORD}
app_db_name: 'some db'
connectionTimeoutMillis: 3000
env:
foo: ${BARBAR}
"""
os.environ["BARBAR"] = "FOOFOO"
mocker.patch(
"builtins.open", side_effect=generate_mock_open(mock_filename, mock_config)
)
Expand All @@ -39,9 +43,10 @@ def test_valid_config(mocker):
assert configFile["database"]["hostname"] == "some host"
assert configFile["database"]["port"] == 1234
assert configFile["database"]["username"] == "some user"
assert configFile["database"]["password"] == "abc123"
assert configFile["database"]["password"] == os.environ["PGPASSWORD"]
assert configFile["database"]["app_db_name"] == "some db"
assert configFile["database"]["connectionTimeoutMillis"] == 3000
assert configFile["env"]["foo"] == "FOOFOO"


def test_config_missing_params(mocker):
Expand Down

0 comments on commit fc11d16

Please sign in to comment.