shift fn locations for consistency
This commit is contained in:
@@ -20,9 +20,9 @@ impl RenderedConfig {
|
|||||||
spec: &Specification,
|
spec: &Specification,
|
||||||
dbg: LogLevel,
|
dbg: LogLevel,
|
||||||
) -> Result<Self, Box<dyn std::error::Error>> {
|
) -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
let context = get_context_from_specification(&spec);
|
let context = Self::get_context(&spec);
|
||||||
|
|
||||||
let hostname = get_hostname(&context, &spec);
|
let hostname = Self::get_hostname(&context, &spec);
|
||||||
info!(dbg, "Rendering {}", hostname);
|
info!(dbg, "Rendering {}", hostname);
|
||||||
|
|
||||||
let base_dir = std::path::Path::new("./tmpl").join(spec.get_layer());
|
let base_dir = std::path::Path::new("./tmpl").join(spec.get_layer());
|
||||||
@@ -31,12 +31,9 @@ impl RenderedConfig {
|
|||||||
.map_err(|e| format!("Failed to parse {}: {}", structure_path.display(), e))?;
|
.map_err(|e| format!("Failed to parse {}: {}", structure_path.display(), e))?;
|
||||||
|
|
||||||
let mut renderer = tera::Tera::default();
|
let mut renderer = tera::Tera::default();
|
||||||
|
renderer.add_raw_templates(structure.load_template_data(&base_dir))?;
|
||||||
|
|
||||||
let mut configs = Vec::new();
|
let mut configs = Vec::new();
|
||||||
|
|
||||||
let template_data = get_template_data_from_structure(&structure, &base_dir);
|
|
||||||
|
|
||||||
renderer.add_raw_templates(template_data)?;
|
|
||||||
|
|
||||||
for template_name in &structure.files {
|
for template_name in &structure.files {
|
||||||
verb!(dbg, " | {}", &template_name);
|
verb!(dbg, " | {}", &template_name);
|
||||||
if !renderer.get_template_names().any(|n| n == template_name) {
|
if !renderer.get_template_names().any(|n| n == template_name) {
|
||||||
@@ -59,7 +56,30 @@ impl RenderedConfig {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// walk the error chain
|
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()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_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 {
|
||||||
|
context.insert(key, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
context
|
||||||
|
}
|
||||||
|
|
||||||
fn log_tera_error(name: &str, e: &tera::Error) {
|
fn log_tera_error(name: &str, e: &tera::Error) {
|
||||||
let mut chain = e.to_string();
|
let mut chain = e.to_string();
|
||||||
let mut source = std::error::Error::source(e);
|
let mut source = std::error::Error::source(e);
|
||||||
@@ -100,47 +120,3 @@ impl RenderedConfig {
|
|||||||
info!(dbg, " | {} ", &merged_config_path.display());
|
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()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|||||||
16
src/tmpl.rs
16
src/tmpl.rs
@@ -16,4 +16,20 @@ impl Structure {
|
|||||||
std::fs::read_to_string(path).map_err(|e| format!("{}: {}", path.display(), e))?;
|
std::fs::read_to_string(path).map_err(|e| format!("{}: {}", path.display(), e))?;
|
||||||
Ok(yaml_serde::from_str(&data)?)
|
Ok(yaml_serde::from_str(&data)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn load_template_data(&self, base_dir: &std::path::Path) -> Vec<(String, String)> {
|
||||||
|
self.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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user