add config output
This commit is contained in:
48
src/main.rs
48
src/main.rs
@@ -4,6 +4,11 @@ mod specs;
|
||||
mod tmpls;
|
||||
|
||||
use log::LogLevel;
|
||||
use serde_yml;
|
||||
use std::fs::{self, create_dir_all, write, OpenOptions};
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
use tmpls::RenderedConfig;
|
||||
|
||||
fn main() {
|
||||
let args: cli::Args = cli::parse_args();
|
||||
@@ -19,13 +24,42 @@ fn main() {
|
||||
verb!(
|
||||
dbg,
|
||||
"Compiled Spec:\n{}",
|
||||
serde_json::to_string_pretty(&spec.compiled).unwrap()
|
||||
serde_json::to_string_pretty(&spec.compiled["data"]).unwrap()
|
||||
);
|
||||
info!(dbg, "Rendered Config:");
|
||||
for line in result {
|
||||
if line != "\n" {
|
||||
print!("{}", line)
|
||||
}
|
||||
}
|
||||
output_rendered_configs(result, dbg)
|
||||
}
|
||||
}
|
||||
|
||||
fn output_rendered_configs(rendered_config: RenderedConfig, 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();
|
||||
}
|
||||
create_dir_all(&path).ok();
|
||||
let rendered_config_path = format!("{}/all.conf", path);
|
||||
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 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(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user