feat: add user variables to input
All checks were successful
continuous-integration/drone/push Build is passing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2024-05-25 14:47:18 +02:00
parent 3e06479cda
commit 6e8e63e5ee
Signed by: kjuulh
GPG Key ID: 9AA7BC13CE474394
2 changed files with 35 additions and 3 deletions

View File

@ -19,6 +19,8 @@ pub async fn process() -> anyhow::Result<()> {
pub struct ProcessOpts { pub struct ProcessOpts {
pub path: PathBuf, pub path: PathBuf,
pub output: PathBuf, pub output: PathBuf,
pub variables: HashMap<String, String>,
} }
impl Default for ProcessOpts { impl Default for ProcessOpts {
@ -29,6 +31,7 @@ impl Default for ProcessOpts {
.expect("to be able to get current dir") .expect("to be able to get current dir")
.join("cuddle-clusters") .join("cuddle-clusters")
.join("k8s"), .join("k8s"),
variables: HashMap::default(),
} }
} }
} }
@ -64,7 +67,14 @@ pub async fn process_opts(
let _ = tokio::fs::remove_dir_all(&opts.output).await; let _ = tokio::fs::remove_dir_all(&opts.output).await;
tokio::fs::create_dir_all(&opts.output).await?; tokio::fs::create_dir_all(&opts.output).await?;
process_templates(&components, &clusters, &template_files, &opts.output).await?; process_templates(
&components,
&clusters,
&template_files,
&opts.output,
&opts.variables,
)
.await?;
Ok(()) Ok(())
} }
@ -184,6 +194,7 @@ async fn process_templates(
clusters: &CuddleClusters, clusters: &CuddleClusters,
template_files: &TemplateFiles, template_files: &TemplateFiles,
dest: &Path, dest: &Path,
variables: &HashMap<String, String>,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
for (environment, value) in clusters.iter() { for (environment, value) in clusters.iter() {
process_cluster( process_cluster(
@ -192,6 +203,7 @@ async fn process_templates(
environment, environment,
template_files, template_files,
&dest.join(environment), &dest.join(environment),
variables,
) )
.await?; .await?;
} }
@ -205,9 +217,18 @@ async fn process_cluster(
environment: &str, environment: &str,
template_files: &TemplateFiles, template_files: &TemplateFiles,
dest: &Path, dest: &Path,
variables: &HashMap<String, String>,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
for (_, template_file) in &template_files.templates { for (_, template_file) in &template_files.templates {
process_template_file(components, value, environment, template_file, dest).await?; process_template_file(
components,
value,
environment,
template_file,
dest,
variables,
)
.await?;
} }
for (template_file_name, template_content) in components for (template_file_name, template_content) in components
@ -228,6 +249,7 @@ async fn process_cluster(
&template_file_name, &template_file_name,
&template_content, &template_content,
dest, dest,
variables,
) )
.await?; .await?;
} }
@ -245,6 +267,7 @@ async fn process_template_file(
environment: &str, environment: &str,
template_file: &PathBuf, template_file: &PathBuf,
dest: &Path, dest: &Path,
variables: &HashMap<String, String>,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let file = tokio::fs::read_to_string(template_file) let file = tokio::fs::read_to_string(template_file)
.await .await
@ -260,6 +283,7 @@ async fn process_template_file(
&file_name.to_string_lossy(), &file_name.to_string_lossy(),
&file, &file,
dest, dest,
variables,
) )
.await?; .await?;
@ -273,6 +297,7 @@ async fn process_render_template(
file_name: &str, file_name: &str,
file_content: &str, file_content: &str,
dest: &Path, dest: &Path,
user_vars: &HashMap<String, String>,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
if !dest.exists() { if !dest.exists() {
tokio::fs::create_dir_all(dest).await?; tokio::fs::create_dir_all(dest).await?;
@ -286,6 +311,7 @@ async fn process_render_template(
env.add_global("environment", environment); env.add_global("environment", environment);
let mut variables = HashMap::new(); let mut variables = HashMap::new();
for component in components { for component in components {
let name = component.name(); let name = component.name();
@ -295,6 +321,10 @@ async fn process_render_template(
variables.insert(name.replace("/", "_"), value); variables.insert(name.replace("/", "_"), value);
} }
} }
variables.insert(
"user_vars".into(),
minijinja::Value::from_serialize(user_vars),
);
let tmpl = env.get_template(file_name)?; let tmpl = env.get_template(file_name)?;
let rendered = tmpl.render(context! { let rendered = tmpl.render(context! {

View File

@ -1,4 +1,4 @@
use std::{cmp::Ordering, path::Path}; use std::{cmp::Ordering, collections::HashMap, path::Path};
use cuddle_clusters::{process::ProcessOpts, ConcreteComponent, IntoComponent}; use cuddle_clusters::{process::ProcessOpts, ConcreteComponent, IntoComponent};
use walkdir::DirEntry; use walkdir::DirEntry;
@ -27,6 +27,7 @@ pub(crate) async fn run_test_with_components(
ProcessOpts { ProcessOpts {
path: test_folder.clone(), path: test_folder.clone(),
output: actual.clone(), output: actual.clone(),
variables: HashMap::default(),
}, },
) )
.await?; .await?;
@ -37,6 +38,7 @@ pub(crate) async fn run_test_with_components(
ProcessOpts { ProcessOpts {
path: test_folder, path: test_folder,
output: expected.clone(), output: expected.clone(),
variables: HashMap::default(),
}, },
) )
.await?; .await?;