From 2d3549a200e6a1872d35b1b5d66c7700e84c86a8 Mon Sep 17 00:00:00 2001 From: nobody Date: Sat, 17 Aug 2024 21:52:54 +0200 Subject: [PATCH] more env config (VERBOSE/TRACE, TODAY) --- src/config_spec.rs | 4 ++- src/email.rs | 3 +- src/hedgedoc.rs | 1 + src/main.rs | 81 ++++++++++++++++++++++++++++++++++++++-------- src/mediawiki.rs | 14 ++++++-- 5 files changed, 86 insertions(+), 17 deletions(-) diff --git a/src/config_spec.rs b/src/config_spec.rs index 4f6b4a3..c694020 100644 --- a/src/config_spec.rs +++ b/src/config_spec.rs @@ -199,7 +199,9 @@ impl<'a> CfgField<'a> { match self { CfgField::Silent { default, .. } => Ok(Some(default.to_string())), CfgField::Default { default, .. } => Ok(Some(default.to_string())), - CfgField::Generated { key, generator, .. } => generator(key, config, is_dry_run()).map(Some), + CfgField::Generated { key, generator, .. } => { + generator(key, config, is_dry_run()).map(Some) + }, _ => Ok(None), } } diff --git a/src/email.rs b/src/email.rs index ff3da78..c6e732b 100644 --- a/src/email.rs +++ b/src/email.rs @@ -49,7 +49,7 @@ pub const CONFIG: CfgGroup<'static> = CfgGroup { ], }; -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct Email { server: String, credentials: Credentials, @@ -57,6 +57,7 @@ pub struct Email { is_dry_run: bool, } +#[derive(Debug)] pub struct SimpleEmail { base: Email, from: String, diff --git a/src/hedgedoc.rs b/src/hedgedoc.rs index 51e619b..017cc63 100644 --- a/src/hedgedoc.rs +++ b/src/hedgedoc.rs @@ -32,6 +32,7 @@ pub const CONFIG: CfgGroup<'static> = CfgGroup { ], }; +#[derive(Debug)] pub struct HedgeDoc { server_url: String, is_dry_run: bool, diff --git a/src/main.rs b/src/main.rs index 4d9cb08..6940d2d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -117,6 +117,44 @@ fn is_dry_run() -> bool { env::var("DRY_RUN").map(|v| !v.is_empty()).unwrap_or(false) } +/// Checks environment variable `VERBOSE` to see if status messages should be +/// printed. +/// +/// Use `verboseln!` to print stuff only if `VERBOSE` (or `TRACE`) is set. +fn is_verbose() -> bool { + env::var("VERBOSE").map(|v| !v.is_empty()).unwrap_or(false) +} +/// Like `println!`, but only if `is_verbose` (or `is_trace`) is true (due to +/// the environment variable `VERBOSE` being set.) +macro_rules! verboseln { + ($($arg:tt)*) => { + if is_verbose() || is_trace() { + println!($($arg)*); + } + }; +} + +/// (A. k. a. "*very* verbose".) Checks environment variable `TRACE` to see if +/// detailed small-step status messages should be printed. +/// +/// Use `traceln!` to print stuff only if `TRACE` is set. +fn is_trace() -> bool { + env::var("TRACE").map(|v| !v.is_empty()).unwrap_or(false) +} +macro_rules! traceln { + ($($arg:tt)*) => { + if is_trace() { + println!($($arg)*); + } + }; +} + +fn today() -> NaiveDate { + env::var("TODAY") + .map(|v| NaiveDate::parse_from_str(&v, "%F").expect("'TODAY' hat nicht format YYYY-MM-DD")) + .unwrap_or(Local::now().date_naive()) +} + struct Args { check_mode: bool, config_file: String, @@ -153,14 +191,16 @@ fn main() -> Result<(), Box> { // set up config file access let args = parse_args(); let config_file = args.config_file.as_str(); + verboseln!("Using config file {config_file}."); let config = KV::new(config_file).unwrap(); config_spec::populate_defaults(&CONFIG_SPEC, &config); if args.check_mode { - println!(include_str!("chaosknoten.txt"), VERSION=env!("CARGO_PKG_VERSION")); + println!(include_str!("chaosknoten.txt"), VERSION = env!("CARGO_PKG_VERSION")); return config_spec::interactive_check(&CONFIG_SPEC, config); } // get config let hedgedoc = HedgeDoc::new(&config["hedgedoc-server-url"], is_dry_run()); + traceln!("Hedgedoc: {:?}", hedgedoc); let email_ = Email::new( &config["email-server"], &config["email-user"], @@ -173,14 +213,16 @@ fn main() -> Result<(), Box> { &config["email-to"], config.get("email-in-reply-to").ok(), ); + traceln!("Email: {:?}", email); let wiki = Mediawiki::new( &config["wiki-server-url"], &config["wiki-http-user"], &config["wiki-http-password"], is_dry_run(), ); + traceln!("Wiki: {:?}", wiki); // get next plenum days - let today = Local::now().date_naive(); + let today = today(); let plenum_spec = date::parse_spec(&config["date-spec"])?; let nearest_plenum_days = date::get_matching_dates_around(today, plenum_spec); // figure out where we are @@ -301,6 +343,8 @@ fn main() -> Result<(), Box> { println!("---AUTH RESULT:---\n{}\n-----------", auth_result); // TEMPORÄR ENDE + // AAAAAAAAAA + let pad_content = hedgedoc.download(¤t_pad_id).expect("Fehler beim Download des Pads!"); let pad_content_without_top_instructions = try_to_remove_top_instructions(pad_content); println!("Pad-content geladen!"); @@ -517,12 +561,12 @@ fn try_to_remove_top_instructions(pad_content: String) -> String { /* ***** formatting helpers ***** */ -fn relative_date( ttp: i64 ) -> String { +fn relative_date(ttp: i64) -> String { if ttp.abs() > 2 { if ttp.is_negative() { - format!( "vor {} Tagen", -ttp ) + format!("vor {} Tagen", -ttp) } else { - format!( "in {} Tagen", ttp ) + format!("in {} Tagen", ttp) } } else { match ttp { @@ -532,7 +576,8 @@ fn relative_date( ttp: i64 ) -> String { -1 => "gestern", -2 => "vorgestern", _ => unreachable!(), - }.to_string() + } + .to_string() } } @@ -546,22 +591,27 @@ fn upper_first(s: &str) -> String { /* ***** transition actions ***** */ +// BBBBBBBBBB + fn do_announcement( - ttp: i64, plenum_day: &NaiveDate, config: &KV, hedgedoc: &HedgeDoc, email: &SimpleEmail, wiki: &Mediawiki, + ttp: i64, plenum_day: &NaiveDate, config: &KV, hedgedoc: &HedgeDoc, email: &SimpleEmail, + wiki: &Mediawiki, ) -> Result<(), Box> { // use TTP to adjust text if needed (in {ttp} days) todo!() } fn do_reminder( - ttp: i64, plenum_day: &NaiveDate, config: &KV, hedgedoc: &HedgeDoc, email: &SimpleEmail, wiki: &Mediawiki, + ttp: i64, plenum_day: &NaiveDate, config: &KV, hedgedoc: &HedgeDoc, email: &SimpleEmail, + wiki: &Mediawiki, ) -> Result<(), Box> { // use TTP to adjust text if needed (tomorrow or today / in {ttp} days) todo!() } fn do_protocol( - ttp: i64, plenum_day: &NaiveDate, config: &KV, hedgedoc: &HedgeDoc, email: &SimpleEmail, wiki: &Mediawiki, + ttp: i64, plenum_day: &NaiveDate, config: &KV, hedgedoc: &HedgeDoc, email: &SimpleEmail, + wiki: &Mediawiki, ) -> Result<(), Box> { // use TTP to adjust text if needed ({-ttp} days ago) todo!() @@ -570,7 +620,8 @@ fn do_protocol( /// General cleanup function. Call as `(999, today, …)` for a complete reset /// based on today as the base date. fn do_cleanup( - ttp: i64, plenum_day: &NaiveDate, config: &KV, hedgedoc: &HedgeDoc, email: &SimpleEmail, wiki: &Mediawiki, + ttp: i64, plenum_day: &NaiveDate, config: &KV, hedgedoc: &HedgeDoc, email: &SimpleEmail, + wiki: &Mediawiki, ) -> Result<(), Box> { todo!() } @@ -596,19 +647,23 @@ const TRANSITION_LUT: [[TransitionFunction; 5]; 5] = [ /* LOGGED */ [do_cleanup, do_clean_announcement, do_clean_reminder, nop, do_cleanup], ]; -fn nop(_: i64, _: &NaiveDate, _: &KV, _: &HedgeDoc, _: &SimpleEmail, _: &Mediawiki) -> Result<(), Box> { +fn nop( + _: i64, _: &NaiveDate, _: &KV, _: &HedgeDoc, _: &SimpleEmail, _: &Mediawiki, +) -> Result<(), Box> { Ok(()) } fn do_clean_announcement( - ttp: i64, plenum_day: &NaiveDate, config: &KV, hedgedoc: &HedgeDoc, email: &SimpleEmail, wiki: &Mediawiki, + ttp: i64, plenum_day: &NaiveDate, config: &KV, hedgedoc: &HedgeDoc, email: &SimpleEmail, + wiki: &Mediawiki, ) -> Result<(), Box> { do_cleanup(ttp, plenum_day, config, hedgedoc, email, wiki)?; do_announcement(ttp, plenum_day, config, hedgedoc, email, wiki) } fn do_clean_reminder( - ttp: i64, plenum_day: &NaiveDate, config: &KV, hedgedoc: &HedgeDoc, email: &SimpleEmail, wiki: &Mediawiki, + ttp: i64, plenum_day: &NaiveDate, config: &KV, hedgedoc: &HedgeDoc, email: &SimpleEmail, + wiki: &Mediawiki, ) -> Result<(), Box> { do_cleanup(ttp, plenum_day, config, hedgedoc, email, wiki)?; do_reminder(ttp, plenum_day, config, hedgedoc, email, wiki) diff --git a/src/mediawiki.rs b/src/mediawiki.rs index a2599ea..a63e470 100644 --- a/src/mediawiki.rs +++ b/src/mediawiki.rs @@ -35,6 +35,7 @@ pub const CONFIG: CfgGroup<'static> = CfgGroup { ], }; +#[derive(Debug)] pub struct Mediawiki { server_url: String, http_user: String, @@ -103,7 +104,10 @@ pub fn pad_ins_wiki(old_pad_content: String) { } /// Converts one file type into another using pandoc and saves the result as a txt file -fn pandoc_convert(old_pad_content: String, output_filepath: &str, input_format: pandoc::InputFormat, output_format: pandoc::OutputFormat) -> Result { +fn pandoc_convert( + old_pad_content: String, output_filepath: &str, input_format: pandoc::InputFormat, + output_format: pandoc::OutputFormat, +) -> Result { //Convert Markdown into Mediawiki // Vanilla pandoc Befehl: pandoc --from markdown --to mediawiki --no-highlight let mut p = pandoc::new(); @@ -129,6 +133,12 @@ fn convert_md_to_mediawiki(old_pad_content: String) -> String { // TODO: use tempfile="3.3", make it a NamedTempFile::new()?; // or alternatively use piped stdout to avoid files entirely let output_filepath: &str = "./pandoc_mediawiki.txt"; - pandoc_convert(old_pad_content, output_filepath, pandoc::InputFormat::Markdown, pandoc::OutputFormat::MediaWiki).expect("Fehler beim Umwandeln des und speichern des Pads in eine mediawiki-Textdatei"); + pandoc_convert( + old_pad_content, + output_filepath, + pandoc::InputFormat::Markdown, + pandoc::OutputFormat::MediaWiki, + ) + .expect("Fehler beim Umwandeln des und speichern des Pads in eine mediawiki-Textdatei"); read_txt_file(output_filepath) }