env hook for output dir

This commit is contained in:
Edward Shen 2019-05-11 11:48:47 -04:00
parent 3803747c64
commit 95d450bf5c
Signed by: edward
GPG key ID: F350507060ED6C90

View file

@ -26,6 +26,7 @@ fn main() -> Result<(), Box<Error>> {
let config: Value = from_str(&read_to_string("config.json")?)?;
let template_dir =
&env::var("PANRES_TEMPLATE_DIR").unwrap_or_else(|_| String::from(DEFAULT_TEMPLATE_DIR));
let output_dir = &env::var("PANRES_OUTPUT_DIR").unwrap_or_else(|_| String::from(DEFAULT_OUTPUT_DIR));
let matches = App::new("panres")
.version(crate_version!())
@ -61,7 +62,7 @@ fn main() -> Result<(), Box<Error>> {
| Ok(notify::DebouncedEvent::Rename(_, msg)) => {
println!("Reloading: {:?}", msg);
tera.full_reload()?;
output(tera, context, outputs.clone(), template_dir)?;
output(tera, context, output_dir, outputs.clone(), template_dir)?;
},
Ok(notify::DebouncedEvent::Rescan) => {
println!("notify emitted rescan event!");
@ -71,33 +72,34 @@ fn main() -> Result<(), Box<Error>> {
}
}
output(tera, context, outputs, template_dir)?;
output(tera, context, output_dir, outputs, template_dir)?;
Ok(())
}
fn output<'a>(
engine: &Tera,
context: &Context,
dir: &str,
outputs: Values,
template_dir: &'a str,
) -> Result<(), Box<Error>> {
if outputs.clone().any(|e| e == "all") {
for output in read_dir(template_dir)? {
generate_output(engine, context, &output?.file_name().to_str().unwrap())?;
generate_output(engine, context, dir, &output?.file_name().to_str().unwrap())?;
}
} else {
for output in outputs {
generate_output(engine, context, &output)?;
generate_output(engine, context, dir, &output)?;
}
}
Ok(())
}
fn generate_output(engine: &Tera, context: &Context, format: &str) -> Result<(), Box<Error>> {
create_dir_all(DEFAULT_OUTPUT_DIR).expect("Could not create output dir");
fn generate_output(engine: &Tera, context: &Context, dir: &str, format: &str) -> Result<(), Box<Error>> {
create_dir_all(dir).expect("Could not create output dir");
write(
format!("{}/{}", DEFAULT_OUTPUT_DIR, format),
format!("{}/{}", dir, format),
engine.render(format, context.clone())?,
)
.expect("to be able to write to output folder");