Skip to content

Commit 45a9b3f

Browse files
authored
Merge pull request #111 from ClarkSource/get-values
feat: allow printing merged value set
2 parents b9a7afa + a151d83 commit 45a9b3f

File tree

5 files changed

+45
-6
lines changed

5 files changed

+45
-6
lines changed

k8t/cli.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from k8t.engine import build
2222
from k8t.templates import YamlValidationError, analyze, render, validate
2323
from k8t.util import (MERGE_METHODS, deep_merge, envvalues, load_cli_value,
24-
load_yaml)
24+
load_yaml, to_json, to_yaml)
2525

2626

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

257257

258+
@get.command(name="values", help="Get final value set.")
259+
@click.option("-m", "--method", type=click.Choice(MERGE_METHODS), default="ltr", show_default=True, help="Value file merge method.")
260+
@click.option("--value-file", "value_files", multiple=True, type=click.Path(dir_okay=False, exists=True), help="Additional value file to include.")
261+
@click.option("--value", "cli_values", type=(str, str), multiple=True, metavar="KEY VALUE", help="Additional value(s) to include.")
262+
@click.option("--cluster", "-c", "cname", metavar="NAME", help="Cluster context to use.")
263+
@click.option("--environment", "-e", "ename", metavar="NAME", help="Deployment environment to use.")
264+
@click.option("--output-format", "-o", type=click.Choice(["json", "yaml"]), default="json", help="Specify output format for values.")
265+
@click.argument("directory", type=click.Path(exists=True, file_okay=False), default=os.getcwd())
266+
@requires_project_directory
267+
def get_values(directory, method, value_files, cli_values, cname, ename, output_format): # pylint: disable=redefined-outer-name
268+
vals = deep_merge( # pylint: disable=redefined-outer-name
269+
values.load_all(directory, cname, ename, method),
270+
*(load_yaml(p) for p in value_files),
271+
dict(load_cli_value(k, v) for k, v in cli_values),
272+
envvalues(),
273+
method=method,
274+
)
275+
276+
if output_format == "json":
277+
print(to_json(vals))
278+
else:
279+
print(to_yaml(vals))
280+
281+
258282
@root.group(help="Edit local project files.")
259283
def edit():
260284
pass

k8t/templates.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import logging
1111
from typing import Set, Tuple
1212

13-
import yaml # pylint: disable=E0401
13+
from ruamel.yaml import YAML # pylint: disable=E0401
1414
from jinja2 import Environment, meta, nodes # pylint: disable=E0401
1515

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

112+
yaml = YAML(typ='safe', pure=True)
113+
112114
try:
113-
yaml.safe_load_all(output)
115+
yaml.load_all(output)
114116
except (yaml.scanner.ScannerError, yaml.parser.ParserError) as err:
115117
raise YamlValidationError(err) from err
116118

k8t/util.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from functools import reduce
1616
from typing import Any, Dict, List, Tuple
1717

18-
import yaml # pylint: disable=E0401
18+
from ruamel.yaml import YAML # pylint: disable=E0401
1919
from click import secho # pylint: disable=E0401
2020

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

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

112113

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

121122

123+
def to_json(input: dict) -> str:
124+
return json.dumps(input)
125+
126+
127+
def to_yaml(input: dict) -> str:
128+
yaml = YAML()
129+
yaml.scalarstring.walk_tree(input)
130+
return yaml.round_trip_dump(input, default_flow_style = False, allow_unicode = True, explicit_start=True)
131+
132+
122133
def envvalues() -> Dict:
123134
prefix: str = "K8T_VALUE_"
124135
values: dict = dict()

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ packages = find:
1717
include_package_data = True
1818
install_requires =
1919
Jinja2
20-
PyYAML
20+
ruamel.yaml
2121
boto3
2222
click
2323
coloredlogs

tox.ini

+2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ isolated_build = True
44

55
[testenv]
66
passenv = *
7+
# including PyYAML since moto forgot it
78
deps = pytest
89
mock
910
boto3
1011
moto
12+
PyYAML
1113
commands = py.test {posargs}
1214
# py.test --doctest-module README.rst
1315

0 commit comments

Comments
 (0)