Skip to content

Commit 2dbab1c

Browse files
lorenzofellettiLorenzo Felletti
authored andcommitted
feat: enhance Cuelang input type with configurable input and output cue-paths
1 parent 2dfdceb commit 2dbab1c

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

kapitan/inputs/cuelang.py

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import shutil
44
import subprocess
55
import tempfile
6-
from typing import Any, Dict, List, Optional, Tuple
76

87
import yaml
98

@@ -39,35 +38,28 @@ def compile_file(self, config: KapitanInputTypeCuelangConfig, input_path: str, c
3938
temp_input_dir = os.path.join(temp_dir, input_dir_name)
4039
shutil.copytree(abs_input_path, temp_input_dir)
4140

42-
cwd = os.getcwd()
43-
44-
# os.chdir(temp_input_dir)
45-
41+
# Write the input data to file
4642
input_file_path = os.path.join(temp_input_dir, "input.yaml")
4743
with open(input_file_path, "w") as f:
4844
yaml.dump(config.input, f)
4945

46+
#  Prepare the command to run CUE export
5047
cmd = [
5148
self.cue_path,
5249
"export",
53-
".", # can't get it to work compiling from an absolute path
54-
# temp_input_dir,
55-
"-l",
56-
"input:",
57-
"input.yaml",
58-
"--out",
59-
"yaml",
50+
".",
6051
]
61-
62-
with open("output.yaml", "w") as f:
52+
# if specified where to inject the input, add it to the command
53+
if config.input_fill_path:
54+
cmd += ["-l", config.input_fill_path]
55+
cmd += ["input.yaml", "--out", "yaml"]
56+
# if specified where the output is yielded, add it to the command
57+
if config.output_yield_path:
58+
cmd += ["--expression", config.output_yield_path]
59+
60+
output_file = os.path.join(compile_path, "output.yaml") # TODO: make this configurable
61+
with open(output_file, "w") as f:
6362
result = subprocess.run(cmd, stdout=f, stderr=subprocess.PIPE, text=True, cwd=temp_input_dir)
6463
if result.returncode != 0:
6564
err = f"Failed to run CUE export: {result.stderr}"
6665
raise KustomizeTemplateError(err)
67-
68-
# os.chdir(cwd)
69-
output_file = os.path.join(compile_path, "output.yaml")
70-
71-
with open(output_file, "w") as out_f:
72-
with open("output.yaml", "r") as in_f:
73-
out_f.write(in_f.read())

kapitan/inventory/model/input_types.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,14 @@ class KapitanInputTypeKustomizeConfig(KapitanInputTypeBaseConfig):
110110
class KapitanInputTypeCuelangConfig(KapitanInputTypeBaseConfig):
111111
input_type: Literal[InputTypes.CUELANG] = InputTypes.CUELANG
112112
output_type: OutputType = OutputType.YAML
113-
yield_field: Optional[str] = None
113+
# optional value to pass to the CUE input
114114
input: Optional[Dict[str, Any]] = None
115+
# optional CUE path in which the input is injected. By default, the input
116+
# is injected at the root.
117+
input_fill_path: Optional[str] = None
118+
# optional CUE path (e.g. metadata.name) that we want to yield in the output.
119+
# By default, the whole output is yielded
120+
output_yield_path: Optional[str] = None
115121

116122

117123
CompileInputTypeConfig = Annotated[

0 commit comments

Comments
 (0)