use pathbuf in spec; move output to render

This commit is contained in:
2026-02-17 03:59:16 -07:00
parent a813c4cb53
commit ae5bf37df1
3 changed files with 141 additions and 134 deletions

View File

@@ -6,10 +6,6 @@ mod tmpl;
use log::LogLevel;
use render::RenderedConfig;
use std::fs::{self, File, OpenOptions, create_dir_all, write};
use std::io::Write;
use std::path::Path;
use yaml_serde;
fn main() {
let args: cli::Args = cli::parse_args();
@@ -21,38 +17,11 @@ fn main() {
spec::compile(&args.devices, &args.env.spec_path, dbg);
for spec in specifications {
let result = RenderedConfig::from_spec(&spec, dbg).unwrap_or_else(|e| {
eprintln!("{}", e);
std::process::exit(1);
});
output_rendered_configs(result, &args.env.out_path, dbg)
RenderedConfig::from_spec(&spec, dbg)
.unwrap_or_else(|e| {
eprintln!("{}", e);
std::process::exit(1);
})
.output(&args.env.out_path, dbg);
}
}
fn output_rendered_configs(rendered_config: RenderedConfig, outdir: &str, dbg: LogLevel) {
info!(dbg, "Writing Output");
let out_path = Path::new(outdir).join(rendered_config.hostname);
if out_path.exists() {
fs::remove_dir_all(&out_path).ok();
}
create_dir_all(&out_path).ok();
let merged_config_path = out_path.join("all.conf");
let mut all_file: File = OpenOptions::new()
.create(true)
.append(true)
.open(&merged_config_path)
.expect("unable to open");
for config in rendered_config.configs {
let template_path = out_path.join(config.name + ".tera");
verb!(dbg, " | {}", &template_path.display());
write(&template_path, &config.data).ok();
all_file.write_all(&config.data.as_bytes()).ok();
}
let spec: String = yaml_serde::to_string(&rendered_config.spec).unwrap();
let compiled_spec_path = out_path.join("compiled-spec.yaml");
verb!(dbg, " | {}", &compiled_spec_path.display());
write(&compiled_spec_path, spec).ok();
info!(dbg, " | {} ", &merged_config_path.display());
}