feat: add writing output to file
Diff
Cargo.toml | 3 ++-
src/data/formats.rs | 11 +++++++++++
src/main.rs | 27 +++++++++++++++++++++++++--
3 files changed, 38 insertions(+), 3 deletions(-)
@@ -12,4 +12,5 @@ serde_derive = "1.0.166"
serde_json = "1.0.100"
walkdir = "2.3.3"
read_pipe = "0.1.2"
minijinja = "1.0.3"
\ No newline at end of file
minijinja = "1.0.3"
gumdrop = "0.8.1"
\ No newline at end of file
@@ -1,5 +1,6 @@
use serde::Serialize;
use serde::Deserialize;
use gumdrop::Options;
#[derive(Debug, Serialize, Deserialize)]
pub struct RenderConfig {
@@ -24,4 +25,14 @@ impl ::std::default::Default for RenderConfig {
site_language: String::from("en")
}
}
}
#[derive(Debug, Options)]
pub struct LaunchArgs {
#[options(help = "print help message")]
pub help: bool,
#[options(help = "rendering configuration to use")]
pub config: String,
#[options(help = "directory to write generated files to", required)]
pub out_dir: String
}
\ No newline at end of file
@@ -1,3 +1,9 @@
use std::str::FromStr;
use gumdrop::Options;
use crate::data::formats::LaunchArgs;
use crate::util::journal::Journal;
mod data {
pub mod formats;
}
@@ -10,6 +16,9 @@ const APP_NAME: &str = "livejrnl-rs";
const ERR_JOURNAL_NOT_FOUND: &str = "ERROR: No journal data specified.\n\nUsage:\n\tjrnl --format json | livejrnl-rs";
fn main() {
let args: LaunchArgs = LaunchArgs::parse_args_default_or_exit();
println!("{:#?}", args);
let mut jrnl_json: String = String::new();
if let Some(pipe) = read_pipe::read_pipe() {
jrnl_json.push_str(&pipe);
@@ -19,7 +28,7 @@ fn main() {
return;
}
let journal: util::journal::Journal = util::journal::parse_journal_json(&jrnl_json);
let journal: Journal = util::journal::parse_journal_json(&jrnl_json);
println!("{:#?}", journal);
let config_data = load_config();
@@ -31,7 +40,21 @@ fn main() {
println!("{:?}", template_files);
let html = util::templating::render_html(config_data, journal, &template_files);
println!("{}", html);
let out_dir = std::path::PathBuf::from_str(&args.out_dir).unwrap_or_default();
let out_file = &out_dir.join("index.html");
let out_path = &out_file.as_path();
match std::fs::create_dir_all(&out_dir) {
Ok(()) => {
let write = std::fs::write(out_path, html);
match write {
Ok(()) => println!("All done!"),
Err(e) => println!("{:?}", e)
}
},
Err(e) => println!("{:?}", e)
}
},
Err(error) => println!("{:#?}", error)
};