optimize template loading

This commit is contained in:
2026-02-16 02:54:15 -07:00
parent 00b067f89a
commit bd91fe5b15
2 changed files with 71 additions and 72 deletions

View File

@@ -20,7 +20,7 @@ fn main() {
specs::compile(&args.devices, &args.env.spec_path, dbg); specs::compile(&args.devices, &args.env.spec_path, dbg);
for spec in specifications { for spec in specifications {
let result = tmpls::process_templates(&spec, dbg).ok().unwrap(); let result = RenderedConfig::from_spec(&spec, dbg).ok().unwrap();
output_rendered_configs(result, &args.env.out_path, dbg) output_rendered_configs(result, &args.env.out_path, dbg)
} }
} }

View File

@@ -21,88 +21,87 @@ pub struct Configuration {
pub data: String, pub data: String,
} }
pub fn process_templates( impl RenderedConfig {
pub fn from_spec(
spec: &Specification, spec: &Specification,
dbg: LogLevel, dbg: LogLevel,
) -> Result<RenderedConfig, Box<dyn std::error::Error>> { ) -> Result<Self, Box<dyn std::error::Error>> {
// Create Tera context from spec.compiled
let mut context = Context::new(); let mut context = Context::new();
if let Value::Object(map) = &spec.compiled {
if let Some(Value::Object(data_map)) = map.get("data") { // Flatten "data" map into Tera context
for (key, value) in data_map { if let Some(data_map) = spec.compiled.get("data").and_then(|v| v.as_object()) {
context.insert(key, value); for (key, val) in data_map {
} context.insert(key, val);
} }
} }
let device_name = match context.get("hostname") { let hostname = context
Some(value) => value.as_str().unwrap_or("default_hostname"), .get("hostname")
None => "default_hostname", .and_then(|v| v.as_str())
}; .unwrap_or("default_hostname")
.to_string();
// Get the base directory path let base_dir = std::path::Path::new("./tmpl").join(spec.get_layer());
let base_dir = format!("./tmpl/{}", spec.get_layer());
// Read structure.yaml // Read structure.yaml
let structure_path = format!("{}/structure.yaml", base_dir); let structure_path = base_dir.join("structure.yaml");
let structure_content: String = match fs::read_to_string(&structure_path) { let structure_data = fs::read_to_string(&structure_path)
Ok(content) => content, .map_err(|e| format!("Failed to read {}: {}", structure_path.display(), e))?;
Err(e) => {
eprintln!( let config_meta: TemplateConfig = yaml_serde::from_str(&structure_data)?;
"while attempting to read {}: {}, skipping",
&structure_path, e
);
String::new()
}
};
let config: TemplateConfig = yaml_serde::from_str(&structure_content)?;
// Initialize Tera with the specific directory
let mut tera = Tera::default(); let mut tera = Tera::default();
// Process each template
info!(dbg, "Rendering {}", &device_name);
let mut configs = Vec::new(); let mut configs = Vec::new();
for template_name in config.files {
// Read the template file directly info!(dbg, "Rendering {}", hostname);
let template_path = format!("{}/{}.tera", base_dir, template_name);
verb!(dbg, " | {}", &template_path); let template_data: Vec<(String, String)> = config_meta
let template_content = match fs::read_to_string(&template_path) { .files
Ok(content) => content, .iter()
.filter_map(|name| {
let path = base_dir.join(format!("{}.tera", name));
match fs::read_to_string(&path) {
Ok(content) => Some((name.clone(), content)),
Err(e) => { Err(e) => {
eprintln!( eprintln!("Skipping {}: {}", path.display(), e);
"while attempting to read {}: {}, skipping", None
&template_path, e
);
String::new()
} }
};
// Add this specific template to Tera
tera.add_raw_template(&template_name, &template_content)?;
// Render the template
let rendered = tera
.render(&template_name, &context)
.inspect_err(|e| {
let mut chain = format!("{}", e);
let mut next_source = std::error::Error::source(e);
while let Some(source) = next_source {
chain.push_str(&format!(" -> {}", source));
next_source = source.source();
} }
eprintln!("[tera] {}", chain);
}) })
.unwrap_or_default(); .collect();
configs.push(Configuration {
name: String::from(&template_name), tera.add_raw_templates(template_data)?;
data: rendered,
}); for template_name in &config_meta.files {
verb!(dbg, " | {}", &template_name);
if !tera.get_template_names().any(|n| n == template_name) {
continue;
} }
Ok(RenderedConfig { match tera.render(template_name, &context) {
hostname: String::from(device_name), Ok(rendered) => configs.push(Configuration {
name: template_name.clone(),
data: rendered,
}),
Err(e) => Self::log_tera_error(template_name, &e),
}
}
Ok(Self {
hostname,
configs, configs,
spec: spec.compiled.clone(), spec: spec.compiled.clone(),
}) })
} }
/// walk the error chain
fn log_tera_error(name: &str, e: &tera::Error) {
let mut chain = e.to_string();
let mut source = std::error::Error::source(e);
while let Some(s) = source {
chain.push_str(&format!(" -> {}", s));
source = s.source();
}
eprintln!("[tera] {}: {}", name, chain);
}
}