Skip to content

Commit c58cf48

Browse files
committed
feat: support env variables in config.yml file
1 parent 63a7d0f commit c58cf48

3 files changed

Lines changed: 29 additions & 3 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ sentry:
137137
environment: "production"
138138
```
139139

140+
It is possible to use environment variables inside the configuration file, using the format `${VARIABLE_NAME}`, for example `user: ${DB_USER}`.
141+
140142
### debug (optional)
141143

142144
Enable debug mode, default is `false`, if you want to see more logs, you can set it to `true`.

meilisync/main.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from meilisync.schemas import Event
1212
from meilisync.settings import Settings
1313
from meilisync.version import __VERSION__
14+
from meilisync.yaml_parser import parse_yaml
1415

1516
app = typer.Typer()
1617

@@ -29,9 +30,7 @@ async def _():
2930
if context.invoked_subcommand == "version":
3031
return
3132
context.ensure_object(dict)
32-
with open(config_file) as f:
33-
config = f.read()
34-
settings = Settings.model_validate(yaml.safe_load(config))
33+
settings = Settings.model_validate(parse_yaml(config_file))
3534
if settings.debug:
3635
logger.debug(settings)
3736
if settings.sentry:

meilisync/yaml_parser.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os
2+
import re
3+
4+
import yaml
5+
6+
7+
class EnvVarLoader(yaml.SafeLoader):
8+
pass
9+
10+
11+
def path_constructor(loader, node):
12+
return os.path.expandvars(node.value)
13+
14+
15+
path_matcher = re.compile(r".*\$\{([^}^{]+)\}.*")
16+
# apply path_constructor to YAML nodes that match the ${...} expression pattern
17+
EnvVarLoader.add_implicit_resolver("!path", path_matcher, None)
18+
EnvVarLoader.add_constructor("!path", path_constructor)
19+
20+
21+
def parse_yaml(config_file: str) -> dict:
22+
with open(config_file) as f:
23+
config = yaml.load(f, Loader=EnvVarLoader)
24+
25+
return config

0 commit comments

Comments
 (0)