refactor logger

This commit is contained in:
2026-02-22 05:43:26 -07:00
parent 8877c61aed
commit 9649961580
6 changed files with 121 additions and 121 deletions

View File

@@ -1,7 +1,8 @@
use crate::log::LogLevel;
use crate::spec::Specification;
use crate::tmpl;
use crate::{dbug, info, verb};
use crate::{crit, dbug, info, verb};
use std::error::Error;
use std::io::Write;
pub struct DeviceConfigBundle {
@@ -23,50 +24,49 @@ pub struct Configuration {
impl DeviceConfigBundle {
pub fn from_spec(spec: Specification) -> Result<Self, Box<dyn std::error::Error>> {
let mut context = Self::get_context(&spec);
let mut context = Self::merge_context(&spec);
let hostname = Self::get_hostname(&context, &spec);
info!(&spec.loglevel, "Rendering {}", hostname);
let base_dir = std::path::Path::new("./tmpl").join(spec.get_layer());
info!("Loading templates for '{hostname}':");
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))?;
.map_err(|e| format!("Failed to parse {}: {e}", structure_path.display()))?;
let mut renderer = tera::Tera::default();
renderer.add_raw_templates(structure.load_template_data(&base_dir, spec.loglevel))?;
dbug!(&spec.loglevel, "Processing templates for {}", hostname);
renderer.add_raw_templates(structure.load_template_data(base_dir))?;
dbug!("Processing templates for {hostname}");
let mut config_variants = Vec::new();
let mut failures = false;
let mut configs = Vec::new();
for variant in &structure.variations {
context.insert(variant, &true);
let mut configs = Vec::new();
let mut cfgs = Vec::new();
for template_name in &structure.files {
dbug!(&spec.loglevel, " | {}.{}", &template_name, variant);
if !renderer.get_template_names().any(|n| n == template_name) {
continue;
}
dbug!(" | {template_name}.{variant}");
match renderer.render(template_name, &context) {
Ok(data) => configs.push(Configuration {
name: template_name.clone(),
data,
}),
Err(e) => Self::log_tera_error(template_name, &e),
Ok(data) => cfgs.push(Configuration::new(template_name, data)),
Err(e) => {
crit!("[!] {}", Error::source(&e).unwrap_or_else(|| &e));
renderer.add_raw_template(template_name, "")?;
failures = true;
}
}
}
config_variants.push(ConfigVariant {
name: String::from(variant),
configs,
});
configs.push(ConfigVariant::new(variant, cfgs));
context.remove(variant);
}
Ok(Self {
hostname,
configs: config_variants,
spec,
platform: structure.platform,
})
match failures {
true => Err(Box::<dyn Error>::from("Rendering failed.")),
false => Ok(Self {
hostname,
configs,
spec,
platform: structure.platform,
}),
}
}
fn get_hostname(context: &tera::Context, spec: &Specification) -> String {
@@ -77,7 +77,7 @@ impl DeviceConfigBundle {
.unwrap_or_else(|| spec.components.get_hostname())
}
fn get_context(spec: &Specification) -> tera::Context {
fn merge_context(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 {
@@ -87,18 +87,8 @@ impl DeviceConfigBundle {
context
}
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);
}
pub fn output_artifacts(self: Self) {
info!(&self.spec.loglevel, "Writing Output:");
info!("Writing Output:");
let device_outpath = std::path::Path::new(&self.spec.outpath).join(self.hostname);
if device_outpath.exists() {
@@ -106,12 +96,12 @@ impl DeviceConfigBundle {
}
std::fs::create_dir_all(&device_outpath).ok();
for variant_configs in &self.configs {
for variant_configs in self.configs {
let out_path = device_outpath.join(&variant_configs.name);
std::fs::create_dir_all(&out_path).ok();
let merged_config_path =
device_outpath.join(format!("all.{}.{}", variant_configs.name, &self.platform));
device_outpath.join(format!("all.{}.{}", variant_configs.name, self.platform));
let mut all_file: std::fs::File = std::fs::OpenOptions::new()
.create(true)
.append(true)
@@ -119,17 +109,16 @@ impl DeviceConfigBundle {
.expect("unable to open");
for config in &variant_configs.configs {
let config_outpath = out_path.join(format!("{}.{}", &config.name, &self.platform));
std::fs::create_dir_all(&config_outpath).ok();
verb!(&self.spec.loglevel, " | {}", &config_outpath.display());
let config_outpath = out_path.join(format!("{}.{}", config.name, self.platform));
verb!(" | {}", &config_outpath.display());
std::fs::write(&config_outpath, &config.data).ok();
all_file.write_all(&config.data.as_bytes()).ok();
}
info!(&self.spec.loglevel, " | {} ", &merged_config_path.display());
info!(" | {} ", &merged_config_path.display());
}
let compiled_spec_path = device_outpath.join("context.yaml");
verb!(&self.spec.loglevel, " | {}", &compiled_spec_path.display());
verb!(" | {}", &compiled_spec_path.display());
std::fs::write(
&compiled_spec_path,
yaml_serde::to_string(&self.spec.compiled).unwrap(),
@@ -137,3 +126,21 @@ impl DeviceConfigBundle {
.ok();
}
}
impl ConfigVariant {
pub fn new(name: &str, configs: Vec<Configuration>) -> Self {
Self {
name: String::from(name),
configs,
}
}
}
impl Configuration {
pub fn new(name: &str, data: String) -> Self {
Self {
name: String::from(name),
data,
}
}
}