optimize template loading
This commit is contained in:
@@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
141
src/tmpls.rs
141
src/tmpls.rs
@@ -21,88 +21,87 @@ pub struct Configuration {
|
|||||||
pub data: String,
|
pub data: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn process_templates(
|
impl RenderedConfig {
|
||||||
spec: &Specification,
|
pub fn from_spec(
|
||||||
dbg: LogLevel,
|
spec: &Specification,
|
||||||
) -> Result<RenderedConfig, Box<dyn std::error::Error>> {
|
dbg: LogLevel,
|
||||||
// Create Tera context from spec.compiled
|
) -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
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!(
|
|
||||||
"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 config_meta: TemplateConfig = yaml_serde::from_str(&structure_data)?;
|
||||||
let mut tera = Tera::default();
|
|
||||||
|
|
||||||
// Process each template
|
let mut tera = Tera::default();
|
||||||
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
|
|
||||||
let template_path = format!("{}/{}.tera", base_dir, template_name);
|
|
||||||
verb!(dbg, " | {}", &template_path);
|
|
||||||
let template_content = match fs::read_to_string(&template_path) {
|
|
||||||
Ok(content) => content,
|
|
||||||
Err(e) => {
|
|
||||||
eprintln!(
|
|
||||||
"while attempting to read {}: {}, skipping",
|
|
||||||
&template_path, e
|
|
||||||
);
|
|
||||||
String::new()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Add this specific template to Tera
|
info!(dbg, "Rendering {}", hostname);
|
||||||
tera.add_raw_template(&template_name, &template_content)?;
|
|
||||||
|
|
||||||
// Render the template
|
let template_data: Vec<(String, String)> = config_meta
|
||||||
let rendered = tera
|
.files
|
||||||
.render(&template_name, &context)
|
.iter()
|
||||||
.inspect_err(|e| {
|
.filter_map(|name| {
|
||||||
let mut chain = format!("{}", e);
|
let path = base_dir.join(format!("{}.tera", name));
|
||||||
let mut next_source = std::error::Error::source(e);
|
match fs::read_to_string(&path) {
|
||||||
while let Some(source) = next_source {
|
Ok(content) => Some((name.clone(), content)),
|
||||||
chain.push_str(&format!(" -> {}", source));
|
Err(e) => {
|
||||||
next_source = source.source();
|
eprintln!("Skipping {}: {}", path.display(), e);
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
match tera.render(template_name, &context) {
|
||||||
|
Ok(rendered) => configs.push(Configuration {
|
||||||
|
name: template_name.clone(),
|
||||||
|
data: rendered,
|
||||||
|
}),
|
||||||
|
Err(e) => Self::log_tera_error(template_name, &e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
hostname,
|
||||||
|
configs,
|
||||||
|
spec: spec.compiled.clone(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(RenderedConfig {
|
/// walk the error chain
|
||||||
hostname: String::from(device_name),
|
fn log_tera_error(name: &str, e: &tera::Error) {
|
||||||
configs,
|
let mut chain = e.to_string();
|
||||||
spec: spec.compiled.clone(),
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user