Skip to content

Commit

Permalink
Merge pull request #111 from ClarkSource/get-values
Browse files Browse the repository at this point in the history
feat: allow printing merged value set
  • Loading branch information
AFriemann authored Dec 2, 2021
2 parents b9a7afa + a151d83 commit 45a9b3f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
26 changes: 25 additions & 1 deletion k8t/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from k8t.engine import build
from k8t.templates import YamlValidationError, analyze, render, validate
from k8t.util import (MERGE_METHODS, deep_merge, envvalues, load_cli_value,
load_yaml)
load_yaml, to_json, to_yaml)


def requires_project_directory(func):
Expand Down Expand Up @@ -255,6 +255,30 @@ def get_templates(directory, cname, ename): # pylint: disable=redefined-outer-n
click.echo(template_path)


@get.command(name="values", help="Get final value set.")
@click.option("-m", "--method", type=click.Choice(MERGE_METHODS), default="ltr", show_default=True, help="Value file merge method.")
@click.option("--value-file", "value_files", multiple=True, type=click.Path(dir_okay=False, exists=True), help="Additional value file to include.")
@click.option("--value", "cli_values", type=(str, str), multiple=True, metavar="KEY VALUE", help="Additional value(s) to include.")
@click.option("--cluster", "-c", "cname", metavar="NAME", help="Cluster context to use.")
@click.option("--environment", "-e", "ename", metavar="NAME", help="Deployment environment to use.")
@click.option("--output-format", "-o", type=click.Choice(["json", "yaml"]), default="json", help="Specify output format for values.")
@click.argument("directory", type=click.Path(exists=True, file_okay=False), default=os.getcwd())
@requires_project_directory
def get_values(directory, method, value_files, cli_values, cname, ename, output_format): # pylint: disable=redefined-outer-name
vals = deep_merge( # pylint: disable=redefined-outer-name
values.load_all(directory, cname, ename, method),
*(load_yaml(p) for p in value_files),
dict(load_cli_value(k, v) for k, v in cli_values),
envvalues(),
method=method,
)

if output_format == "json":
print(to_json(vals))
else:
print(to_yaml(vals))


@root.group(help="Edit local project files.")
def edit():
pass
Expand Down
6 changes: 4 additions & 2 deletions k8t/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import logging
from typing import Set, Tuple

import yaml # pylint: disable=E0401
from ruamel.yaml import YAML # pylint: disable=E0401
from jinja2 import Environment, meta, nodes # pylint: disable=E0401

from k8t import config
Expand Down Expand Up @@ -109,8 +109,10 @@ def get_variables(ast, engine: Environment) -> Set[str]:
def render(template_path: str, values: dict, engine: Environment) -> str:
output = engine.get_template(template_path).render(values)

yaml = YAML(typ='safe', pure=True)

try:
yaml.safe_load_all(output)
yaml.load_all(output)
except (yaml.scanner.ScannerError, yaml.parser.ParserError) as err:
raise YamlValidationError(err) from err

Expand Down
15 changes: 13 additions & 2 deletions k8t/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from functools import reduce
from typing import Any, Dict, List, Tuple

import yaml # pylint: disable=E0401
from ruamel.yaml import YAML # pylint: disable=E0401
from click import secho # pylint: disable=E0401

from simple_tools.interaction import confirm # pylint: disable=E0401
Expand Down Expand Up @@ -107,7 +107,8 @@ def load_yaml(path: str) -> dict:
LOGGER.debug("loading values file: %s", path)

with open(path, "r") as stream:
return yaml.safe_load(stream) or dict()
yaml = YAML(typ='safe', pure=True)
return yaml.load(stream) or dict()


def load_cli_value(key: str, value: str) -> Tuple[str, Any]:
Expand All @@ -119,6 +120,16 @@ def load_cli_value(key: str, value: str) -> Tuple[str, Any]:
return key, value


def to_json(input: dict) -> str:
return json.dumps(input)


def to_yaml(input: dict) -> str:
yaml = YAML()
yaml.scalarstring.walk_tree(input)
return yaml.round_trip_dump(input, default_flow_style = False, allow_unicode = True, explicit_start=True)


def envvalues() -> Dict:
prefix: str = "K8T_VALUE_"
values: dict = dict()
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ packages = find:
include_package_data = True
install_requires =
Jinja2
PyYAML
ruamel.yaml
boto3
click
coloredlogs
Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ isolated_build = True

[testenv]
passenv = *
# including PyYAML since moto forgot it
deps = pytest
mock
boto3
moto
PyYAML
commands = py.test {posargs}
# py.test --doctest-module README.rst

Expand Down

0 comments on commit 45a9b3f

Please sign in to comment.