This repository has been archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from tumido/fix-dereference-jsonschema
fix: dereference jsonschema from a template
- Loading branch information
Showing
8 changed files
with
4,662 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import json | ||
from typing import List, Dict, Any | ||
from pathlib import Path | ||
|
||
import jsonref | ||
import yaml | ||
try: | ||
from yaml import CLoader as Loader, CDumper as Dumper | ||
except ImportError: | ||
from yaml import Loader, Dumper | ||
from jinja2 import Template | ||
|
||
JSONSCHEMA_TEMPLATE_NAME = "values.schema.tmpl.json" | ||
JSONSCHEMA_NAME = "values.schema.json" | ||
CHART_LOCK = "Chart.lock" | ||
|
||
def parse_chart_lock(chart_dir: Path): | ||
"""Open and load Chart.yaml file.""" | ||
with open(chart_dir / CHART_LOCK, "r") as f: | ||
return yaml.load(f, Loader=Loader) | ||
|
||
def template_schema(chart_dir: Path, lock: Dict[str, Any]): | ||
"""Load values.schema.tmpl.json and template it via Jinja2.""" | ||
with open(chart_dir / JSONSCHEMA_TEMPLATE_NAME, "r") as f: | ||
schema_template = Template(f.read()) | ||
|
||
return json.loads(schema_template.render(lock)) | ||
|
||
def dereference_and_save(chart_dir: Path, schema_template: Any): | ||
"""Take schema containing $refs and dereference them.""" | ||
schema = jsonref.replace_refs(schema_template) | ||
with open(chart_dir / JSONSCHEMA_NAME, "w") as f: | ||
json.dump(schema, f, indent=4, sort_keys=True) | ||
|
||
if __name__ == '__main__': | ||
charts = [p.parent for p in Path(".").rglob(CHART_LOCK)] | ||
|
||
errors: List[BaseException] = [] | ||
for chart in charts: | ||
try: | ||
lock = parse_chart_lock(chart) | ||
schema_template = template_schema(chart, lock) | ||
|
||
dereference_and_save(chart, schema_template) | ||
except BaseException as e: | ||
print(f"Could not process schema for '{chart}': {e}") | ||
errors.append(e) | ||
if errors: | ||
exit(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,6 @@ | |
.idea/ | ||
*.tmproj | ||
.vscode/ | ||
# Templates files | ||
README.md.gotmpl | ||
values.schema.tmpll.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.