additional error handling; use path buffers; improve logic
This commit is contained in:
56
src/main.rs
56
src/main.rs
@@ -4,57 +4,51 @@ mod specs;
|
||||
mod tmpls;
|
||||
|
||||
use log::LogLevel;
|
||||
use serde_yml;
|
||||
use std::fs::{self, create_dir_all, write, OpenOptions};
|
||||
use std::fs::{self, File, OpenOptions, create_dir_all, write};
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
use tmpls::RenderedConfig;
|
||||
use yaml_serde;
|
||||
|
||||
fn main() {
|
||||
let args: cli::Args = cli::parse_args();
|
||||
let dbg: LogLevel = args.loglevel;
|
||||
|
||||
dbug!(dbg, "{}", &args);
|
||||
dbug!(dbg, "{:#?}", &args);
|
||||
|
||||
let specifications: Vec<specs::Specification> =
|
||||
specs::compile(&args.devices, &args.env.spec_path, dbg);
|
||||
|
||||
for spec in specifications {
|
||||
let result = tmpls::process_templates(&spec, dbg).ok().unwrap();
|
||||
output_rendered_configs(result, dbg)
|
||||
output_rendered_configs(result, &args.env.out_path, dbg)
|
||||
}
|
||||
}
|
||||
|
||||
fn output_rendered_configs(rendered_config: RenderedConfig, dbg: LogLevel) {
|
||||
fn output_rendered_configs(rendered_config: RenderedConfig, outdir: &str, dbg: LogLevel) {
|
||||
info!(dbg, "Writing Output");
|
||||
let path: String = format!("out/{}", rendered_config.hostname);
|
||||
if Path::new(&path).exists() {
|
||||
fs::remove_dir_all(&path).ok();
|
||||
let out_path = Path::new(outdir).join(rendered_config.hostname);
|
||||
if out_path.exists() {
|
||||
fs::remove_dir_all(&out_path).ok();
|
||||
}
|
||||
create_dir_all(&path).ok();
|
||||
let rendered_config_path = format!("{}/all.conf", path);
|
||||
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 rendered_template in rendered_config.configs {
|
||||
let outpath = format!("{}/{}.tmpl", path, rendered_template.0);
|
||||
verb!(dbg, " | {}", &outpath);
|
||||
write(&outpath, &rendered_template.1).ok();
|
||||
append_to_file(&rendered_config_path, rendered_template.1).ok();
|
||||
let template_path = out_path.join(rendered_template.0 + ".tera");
|
||||
verb!(dbg, " | {}", &template_path.display());
|
||||
write(&template_path, &rendered_template.1).ok();
|
||||
all_file.write_all(&rendered_template.1.as_bytes()).ok();
|
||||
}
|
||||
let outpath = format!("{}/compiled.spec", path);
|
||||
let spec: String = match serde_yml::to_string(&rendered_config.spec) {
|
||||
Ok(yaml) => yaml,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to convert to YAML: {}", e);
|
||||
String::new()
|
||||
}
|
||||
};
|
||||
verb!(dbg, " | {}", &outpath);
|
||||
write(&outpath, spec).ok();
|
||||
info!(dbg, " | {} ", &rendered_config_path);
|
||||
}
|
||||
|
||||
fn append_to_file(path: &str, content: String) -> std::io::Result<()> {
|
||||
let mut file = OpenOptions::new().create(true).append(true).open(path)?;
|
||||
|
||||
write!(file, "{}", content)?;
|
||||
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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user