Skip to content

Commit

Permalink
fix: fix the jinja2 template in the config file
Browse files Browse the repository at this point in the history
  • Loading branch information
xmnlab committed Sep 27, 2024
1 parent e583e70 commit 2ef9c65
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .sugar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ version: 1.0
compose-app: docker-compose
env-file: .env
defaults:
group: {{ env.KXGR_GROUP }}
project-name: sugar-{{ env.KXGR_PROJECT_NAME }}
group: ${{ env.KXGR_GROUP }}
project-name: sugar-${{ env.KXGR_PROJECT_NAME }}
groups:
group1:
project-name: project1 # optional
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ of your project. This is an example of a configuration file:
version: 1.0
compose-app: docker compose
default:
group: {{ env.ENV }}
group: ${{ env.ENV }}
groups:
group1:
project-name: project1
Expand Down Expand Up @@ -124,6 +124,6 @@ Some examples of how to use it:
`sugar ext restart --group group1 --services service1,service2`


**NOTE**: If you use: ```default: group: {{ env.ENV }}```, you don't need to
**NOTE**: If you use: ```default: group: ${{ env.ENV }}```, you don't need to
give `--group <GROUP_NAME>`, except if you want a different group than the
default one.
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ of your project. This is an example of a configuration file:
version: 1.0
compose-app: docker compose
default:
group: {{ "{{ env.ENV }}" }}
group: ${{ "${{ env.ENV }}" }}
groups:
group1:
project-name: project1
Expand Down Expand Up @@ -128,6 +128,6 @@ Some examples of how to use it:
`sugar ext restart --group group1 --services service1,service2`


**NOTE**: If you use: ```default: group: {{ "{{ env.ENV }}" }}```, you don't need to
**NOTE**: If you use: ```default: group: ${{ env.ENV }}```, you don't need to
give `--group <GROUP_NAME>`, except if you want a different group than the
default one.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ quote-style = "single"
[tool.bandit]
exclude_dirs = ["tests"]
targets = "src/sugar/"
skips = ["B102", "B701"]

[tool.vulture]
exclude = ["tests", "src/sugar/cli.py"]
Expand Down
24 changes: 9 additions & 15 deletions src/sugar/plugins/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,15 @@
import sh
import yaml # type: ignore

from jinja2 import Template
from jinja2 import Environment

from sugar.logs import KxgrErrorType, KxgrLogs


def escape_template_tag(v: str) -> str:
"""Escape template tags for template rendering."""
return v.replace('{{', r'\{\{').replace('}}', r'\}\}')


def unescape_template_tag(v: str) -> str:
"""Unescape template tags for template rendering."""
return v.replace(r'\{\{', '{{').replace(r'\}\}', '}}')
TEMPLATE = Environment(
autoescape=False,
variable_start_string='${{',
variable_end_string='}}',
)


class SugarBase:
Expand Down Expand Up @@ -212,7 +208,7 @@ def _filter_service_group(self):
def _load_config(self):
with open(self.config_file, 'r') as f:
# escape template tags
content = escape_template_tag(f.read())
content = f.read()
f_content = io.StringIO(content)
self.config = yaml.safe_load(f_content)

Expand Down Expand Up @@ -277,12 +273,10 @@ def _load_defaults(self):
_defaults = self.config.get('defaults', {})

for k, v in _defaults.items():
unescaped_value = (
unescape_template_tag(v) if isinstance(v, str) else str(v)
)
unescaped_value = v if isinstance(v, str) else str(v)

_defaults[k] = yaml.safe_load(
Template(unescaped_value).render(env=self.env)
TEMPLATE.from_string(unescaped_value).render(env=self.env)
)

self.defaults = _defaults
Expand Down

0 comments on commit 2ef9c65

Please sign in to comment.