use pathbuf in spec; move output to render
This commit is contained in:
119
src/render.rs
119
src/render.rs
@@ -2,8 +2,7 @@ use crate::spec::Specification;
|
||||
use crate::tmpl;
|
||||
use crate::{LogLevel, info, verb};
|
||||
use serde_json::Value;
|
||||
use std::fs;
|
||||
use tera::{Context, Tera};
|
||||
use std::io::Write;
|
||||
|
||||
pub struct RenderedConfig {
|
||||
pub hostname: String,
|
||||
@@ -21,56 +20,30 @@ impl RenderedConfig {
|
||||
spec: &Specification,
|
||||
dbg: LogLevel,
|
||||
) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let mut context = Context::new();
|
||||
let context = get_context_from_specification(&spec);
|
||||
|
||||
// Flatten "data" map into Tera context
|
||||
if let Some(data_map) = spec.compiled.get("data").and_then(|v| v.as_object()) {
|
||||
for (key, val) in data_map {
|
||||
context.insert(key, val);
|
||||
}
|
||||
}
|
||||
|
||||
let hostname = context
|
||||
.get("hostname")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("default_hostname")
|
||||
.to_string();
|
||||
let hostname = get_hostname(&context, &spec);
|
||||
info!(dbg, "Rendering {}", hostname);
|
||||
|
||||
let base_dir = std::path::Path::new("./tmpl").join(spec.get_layer());
|
||||
|
||||
let structure_path = base_dir.join("structure.yaml");
|
||||
let structure = tmpl::Structure::from_file(&structure_path)
|
||||
.map_err(|e| format!("Failed to parse {}: {}", structure_path.display(), e))?;
|
||||
|
||||
let mut tera = Tera::default();
|
||||
let mut renderer = tera::Tera::default();
|
||||
let mut configs = Vec::new();
|
||||
|
||||
info!(dbg, "Rendering {}", hostname);
|
||||
let template_data = get_template_data_from_structure(&structure, &base_dir);
|
||||
|
||||
let template_data: Vec<(String, String)> = structure
|
||||
.files
|
||||
.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) => {
|
||||
eprintln!("Skipping {}: {}", path.display(), e);
|
||||
None
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
tera.add_raw_templates(template_data)?;
|
||||
renderer.add_raw_templates(template_data)?;
|
||||
|
||||
for template_name in &structure.files {
|
||||
verb!(dbg, " | {}", &template_name);
|
||||
if !tera.get_template_names().any(|n| n == template_name) {
|
||||
if !renderer.get_template_names().any(|n| n == template_name) {
|
||||
continue;
|
||||
}
|
||||
|
||||
match tera.render(template_name, &context) {
|
||||
match renderer.render(template_name, &context) {
|
||||
Ok(rendered) => configs.push(Configuration {
|
||||
name: template_name.clone(),
|
||||
data: rendered,
|
||||
@@ -96,4 +69,78 @@ impl RenderedConfig {
|
||||
}
|
||||
eprintln!("[tera] {}: {}", name, chain);
|
||||
}
|
||||
|
||||
pub fn output(self: Self, outdir: &str, dbg: LogLevel) {
|
||||
info!(dbg, "Writing Output:");
|
||||
|
||||
let out_path = std::path::Path::new(outdir).join(self.hostname);
|
||||
if out_path.exists() {
|
||||
std::fs::remove_dir_all(&out_path).ok();
|
||||
}
|
||||
std::fs::create_dir_all(&out_path).ok();
|
||||
|
||||
let merged_config_path = out_path.join("all.conf");
|
||||
let mut all_file: std::fs::File = std::fs::OpenOptions::new()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(&merged_config_path)
|
||||
.expect("unable to open");
|
||||
|
||||
for config in &self.configs {
|
||||
let template_path = out_path.join(format!("{}.tera", &config.name));
|
||||
verb!(dbg, " | {}", &template_path.display());
|
||||
std::fs::write(&template_path, &config.data).ok();
|
||||
all_file.write_all(&config.data.as_bytes()).ok();
|
||||
}
|
||||
|
||||
let spec: String = yaml_serde::to_string(&self.spec).unwrap();
|
||||
let compiled_spec_path = out_path.join("compiled-spec.yaml");
|
||||
verb!(dbg, " | {}", &compiled_spec_path.display());
|
||||
std::fs::write(&compiled_spec_path, spec).ok();
|
||||
info!(dbg, " | {} ", &merged_config_path.display());
|
||||
}
|
||||
}
|
||||
|
||||
fn get_template_data_from_structure(
|
||||
structure: &tmpl::Structure,
|
||||
base_dir: &std::path::Path,
|
||||
) -> Vec<(String, String)> {
|
||||
structure
|
||||
.files
|
||||
.iter()
|
||||
.filter_map(|name| {
|
||||
let path = base_dir.join(format!("{}.tera", name));
|
||||
match std::fs::read_to_string(&path) {
|
||||
Ok(content) => Some((name.clone(), content)),
|
||||
Err(e) => {
|
||||
eprintln!("Skipping {}: {}", path.display(), e);
|
||||
None
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn get_context_from_specification(spec: &Specification) -> tera::Context {
|
||||
let mut context = tera::Context::new();
|
||||
if let Some(data_map) = spec.compiled.get("data").and_then(|v| v.as_object()) {
|
||||
for (key, val) in data_map {
|
||||
context.insert(key, val);
|
||||
}
|
||||
}
|
||||
context
|
||||
}
|
||||
|
||||
fn get_hostname(context: &tera::Context, spec: &Specification) -> String {
|
||||
context
|
||||
.get("hostname")
|
||||
.and_then(|v| v.as_str())
|
||||
.map(|s| s.to_string())
|
||||
.unwrap_or_else(|| {
|
||||
spec.device
|
||||
.file_stem()
|
||||
.and_then(|n| n.to_str())
|
||||
.unwrap_or("unknown")
|
||||
.to_string()
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user