diff --git a/frigate/cli.py b/frigate/cli.py index 274d814..c1aac59 100644 --- a/frigate/cli.py +++ b/frigate/cli.py @@ -24,7 +24,10 @@ def cli(): @click.option( "--no-deps", is_flag=True, default=True, help="Do not render dependency values", ) -def gen(filename, output_format, no_credits, no_deps): +@click.option( + "--no-update", is_flag=True, default=True, help="Do not update the charts", +) +def gen(filename, output_format, no_credits, no_deps, no_update): click.echo( - frigate.gen.gen(filename, output_format, credits=no_credits, deps=no_deps) + frigate.gen.gen(filename, output_format, credits=no_credits, deps=no_deps, update=no_update) ) diff --git a/frigate/gen.py b/frigate/gen.py index 8621645..4221b0a 100644 --- a/frigate/gen.py +++ b/frigate/gen.py @@ -3,7 +3,8 @@ import tempfile import shutil import subprocess - +import datetime +from json import JSONEncoder from jinja2 import Environment, FileSystemLoader from ruamel.yaml import YAML from ruamel.yaml.comments import CommentedMap @@ -36,6 +37,36 @@ def load_chart(chartdir, root=None): return chart, list(traverse(values, root=root)) +def load_prepacked_chart_with_dependencies(chartdir, root=None): + root = [] if root is None else root + chart, values = load_chart(chartdir, root=root) + if "dependencies" in chart: + for dependency in chart["dependencies"]: + dependency_name = dependency["name"] + + with tempfile.TemporaryDirectory() as tmpdirname: + tar_file_path = os.path.join( + chartdir, "charts", f"{dependency_name}-{dependency['version']}.tgz", + ) + if os.path.isfile(tar_file_path): + dependency_path = tar_file_path + shutil.unpack_archive(dependency_path, tmpdirname) + dependency_dir = os.path.join(tmpdirname, dependency_name) + else: + dependency_path = os.path.join( + chartdir, "charts", f"{dependency_name}", + ) + dependency_dir = os.path.join(chartdir, dependency_path) + # chart namespace eg nginx.controller.foo + namespace = root + [dependency_name] + + _, dependency_values = load_prepacked_chart_with_dependencies( + dependency_dir, namespace + ) + values = squash_duplicate_values(values + dependency_values) + return chart, values + + def load_chart_with_dependencies(chartdir, root=None): """Load the yaml information from a Helm chart directory and its dependencies. @@ -184,6 +215,12 @@ def clean_comment(comment): return comment.strip("# ") +class DateTimeEncoder(JSONEncoder): + def default(self, obj): + if isinstance(obj, (datetime.date, datetime.datetime)): + return obj.isoformat() + + def traverse(tree, root=None): """Iterate over a tree of configuration and extract all information. @@ -230,10 +267,10 @@ def traverse(tree, root=None): if key in tree.ca.items: comment = get_comment(tree, key) param = ".".join(root + [key]) - yield [param, comment, json.dumps(default)] + yield [param, comment, json.dumps(default, indent=4, sort_keys=True, cls=DateTimeEncoder)] -def gen(chartdir, output_format, credits=True, deps=True): +def gen(chartdir, output_format, credits=True, deps=True, update=True): """Generate documentation for a Helm chart. Generate documentation for a Helm chart given the path to a chart and a @@ -244,14 +281,19 @@ def gen(chartdir, output_format, credits=True, deps=True): output_format (str): Output format (maps to jinja templates in frigate) credits (bool): Show Frigate credits in documentation deps (bool): Read values from chart dependencies and include them in the config table + update (bool): If false, frigate won't update the charts at runtime Returns: str: Rendered documentation for the Helm chart """ - chart, values = ( - load_chart_with_dependencies(chartdir) if deps else load_chart(chartdir) - ) + if deps: + if update: + chart, values = load_chart_with_dependencies(chartdir) + else: + chart, values = load_prepacked_chart_with_dependencies(chartdir) + else: + chart, values = load_chart(chartdir) templates = Environment(loader=FileSystemLoader([chartdir, TEMPLATES_PATH])) if os.path.isfile(os.path.join(chartdir, DOTFILE_NAME)): diff --git a/frigate/templates/html.jinja2 b/frigate/templates/html.jinja2 index 2a20588..15c11e1 100644 --- a/frigate/templates/html.jinja2 +++ b/frigate/templates/html.jinja2 @@ -44,7 +44,7 @@
{{ param }}{{ default }}{{ default }}| Parameter | +Description | +Default | +
|---|---|---|
| {{ param }} | +{{ comment }} | ++{% if "\n" in default %} + +```json + +{{ default }} + +``` + +{% else %} +{{ default }} +{% endif %} + | +