From 29bd3d3e645a3cdaa5d80fdac59645eed60f0026 Mon Sep 17 00:00:00 2001 From: berrysweet Date: Thu, 6 Jul 2023 21:31:17 +0200 Subject: [PATCH] feat: add writing output to file --- Cargo.toml | 3 ++- src/data/formats.rs | 11 +++++++++++ src/main.rs | 27 +++++++++++++++++++++++++-- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 499c751..b92992c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/src/data/formats.rs b/src/data/formats.rs index e37aa44..a047d33 100644 --- a/src/data/formats.rs +++ b/src/data/formats.rs @@ -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 diff --git a/src/main.rs b/src/main.rs index 1c5b39e..40cdecb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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) }; -- libgit2 1.7.2