Skip to content

Commit

Permalink
feat: support env variables in config.yml file
Browse files Browse the repository at this point in the history
  • Loading branch information
matinone committed Jan 31, 2024
1 parent 63a7d0f commit c58cf48
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ sentry:
environment: "production"
```

It is possible to use environment variables inside the configuration file, using the format `${VARIABLE_NAME}`, for example `user: ${DB_USER}`.

### debug (optional)

Enable debug mode, default is `false`, if you want to see more logs, you can set it to `true`.
Expand Down
5 changes: 2 additions & 3 deletions meilisync/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from meilisync.schemas import Event
from meilisync.settings import Settings
from meilisync.version import __VERSION__
from meilisync.yaml_parser import parse_yaml

app = typer.Typer()

Expand All @@ -29,9 +30,7 @@ async def _():
if context.invoked_subcommand == "version":
return
context.ensure_object(dict)
with open(config_file) as f:
config = f.read()
settings = Settings.model_validate(yaml.safe_load(config))
settings = Settings.model_validate(parse_yaml(config_file))
if settings.debug:
logger.debug(settings)
if settings.sentry:
Expand Down
25 changes: 25 additions & 0 deletions meilisync/yaml_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os
import re

import yaml


class EnvVarLoader(yaml.SafeLoader):
pass


def path_constructor(loader, node):
return os.path.expandvars(node.value)


path_matcher = re.compile(r".*\$\{([^}^{]+)\}.*")
# apply path_constructor to YAML nodes that match the ${...} expression pattern
EnvVarLoader.add_implicit_resolver("!path", path_matcher, None)
EnvVarLoader.add_constructor("!path", path_constructor)


def parse_yaml(config_file: str) -> dict:
with open(config_file) as f:
config = yaml.load(f, Loader=EnvVarLoader)

return config

0 comments on commit c58cf48

Please sign in to comment.