Skip to content

Commit

Permalink
feat: bloom.config add all attribute with _FILE suffix indirect affec…
Browse files Browse the repository at this point in the history
…tation by file content (usefull for Docker secrets)
  • Loading branch information
rv2931 committed Feb 28, 2024
1 parent 1f4a7a8 commit abfcde2
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions bloom/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
from pathlib import Path

from pydantic import BaseSettings
Expand All @@ -12,6 +13,15 @@ def extract_values_from_env(config:dict,allow_extend:bool=False):
Returns a dict contains key/value
"""
for k,v in os.environ.items():
# Processing of indirect affectation via [ATTR]_FILE=VALUE_PATH => ATTR=VALUE
if k in [f"{k}_FILE" for k in config.keys()]\
and ( k.removesuffix('_FILE') in config.keys() or allow_extend == True):
if(os.path.isfile(v)):
file = open(v, mode='r')
config[k.removesuffix('_FILE')]=file.readline().strip()
# Processing of direct affectation via ATTR=VALUE
# if extracted key already exist in config OR if allowed to add new keys to config
# Then adding/updating key/value
if k in config.keys() or allow_extend == True:
config[k]=v
return config
Expand All @@ -31,10 +41,18 @@ def extract_values_from_file(filename:str,config:dict,allow_extend:bool=False,en
split=l.strip().split('=',1)
# if extraction contains 2 items and strictly 2 items
if(len(split)==2):
k=split[0]
v=split[1]
# Processing of indirect affectation via [ATTR]_FILE=VALUE_PATH => ATTR=VALUE
if k in [f"{k}_FILE" for k in config.keys()]\
and ( k.removesuffix('_FILE') in config.keys() or allow_extend == True):
if(os.path.isfile(v)):
file = open(v, mode='r')
config[k.removesuffix('_FILE')]=file.readline().strip()
# if extracted key already exist in config OR if allowed to add new keys to config
# Then adding/updating key/value
if split[0] in config.keys() or allow_extend == True:
config[split[0]]=split[1]
if k in config.keys() or allow_extend == True:
config[k]=v
if env_priority: extract_values_from_env(config,allow_extend=False)
return config

Expand Down Expand Up @@ -76,7 +94,7 @@ def __init__(self,*arg, **args):
if os.path.isfile(file_to_process):
extract_values_from_file(file_to_process,config,allow_extend=False,env_priority=True)

if APP_ENV has been defined
# if APP_ENV has been defined
if 'APP_ENV' in config:
# Extract .env.${APP_ENV} and override existing values
file_to_process=Path(os.path.dirname(__file__)).joinpath(f"../.env.{config['APP_ENV']}")
Expand Down

0 comments on commit abfcde2

Please sign in to comment.