diff --git a/crates/cuddle-clusters/src/process.rs b/crates/cuddle-clusters/src/process.rs index b32d927..4203e1a 100644 --- a/crates/cuddle-clusters/src/process.rs +++ b/crates/cuddle-clusters/src/process.rs @@ -19,6 +19,8 @@ pub async fn process() -> anyhow::Result<()> { pub struct ProcessOpts { pub path: PathBuf, pub output: PathBuf, + + pub variables: HashMap, } impl Default for ProcessOpts { @@ -29,6 +31,7 @@ impl Default for ProcessOpts { .expect("to be able to get current dir") .join("cuddle-clusters") .join("k8s"), + variables: HashMap::default(), } } } @@ -64,7 +67,14 @@ pub async fn process_opts( let _ = tokio::fs::remove_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(()) } @@ -184,6 +194,7 @@ async fn process_templates( clusters: &CuddleClusters, template_files: &TemplateFiles, dest: &Path, + variables: &HashMap, ) -> anyhow::Result<()> { for (environment, value) in clusters.iter() { process_cluster( @@ -192,6 +203,7 @@ async fn process_templates( environment, template_files, &dest.join(environment), + variables, ) .await?; } @@ -205,9 +217,18 @@ async fn process_cluster( environment: &str, template_files: &TemplateFiles, dest: &Path, + variables: &HashMap, ) -> anyhow::Result<()> { 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 @@ -228,6 +249,7 @@ async fn process_cluster( &template_file_name, &template_content, dest, + variables, ) .await?; } @@ -245,6 +267,7 @@ async fn process_template_file( environment: &str, template_file: &PathBuf, dest: &Path, + variables: &HashMap, ) -> anyhow::Result<()> { let file = tokio::fs::read_to_string(template_file) .await @@ -260,6 +283,7 @@ async fn process_template_file( &file_name.to_string_lossy(), &file, dest, + variables, ) .await?; @@ -273,6 +297,7 @@ async fn process_render_template( file_name: &str, file_content: &str, dest: &Path, + user_vars: &HashMap, ) -> anyhow::Result<()> { if !dest.exists() { tokio::fs::create_dir_all(dest).await?; @@ -286,6 +311,7 @@ async fn process_render_template( env.add_global("environment", environment); let mut variables = HashMap::new(); + for component in components { let name = component.name(); @@ -295,6 +321,10 @@ async fn process_render_template( variables.insert(name.replace("/", "_"), value); } } + variables.insert( + "user_vars".into(), + minijinja::Value::from_serialize(user_vars), + ); let tmpl = env.get_template(file_name)?; let rendered = tmpl.render(context! { diff --git a/crates/cuddle-clusters/tests/common.rs b/crates/cuddle-clusters/tests/common.rs index 946a8c6..9dedb4b 100644 --- a/crates/cuddle-clusters/tests/common.rs +++ b/crates/cuddle-clusters/tests/common.rs @@ -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 walkdir::DirEntry; @@ -27,6 +27,7 @@ pub(crate) async fn run_test_with_components( ProcessOpts { path: test_folder.clone(), output: actual.clone(), + variables: HashMap::default(), }, ) .await?; @@ -37,6 +38,7 @@ pub(crate) async fn run_test_with_components( ProcessOpts { path: test_folder, output: expected.clone(), + variables: HashMap::default(), }, ) .await?;