new state() function

- now, it is possible to use e.g. STATE_OVERRIDE=Waiting to override the state
- this is useful for development in order to ignore parts of the config.sqlite
This commit is contained in:
murmeldin 2024-10-14 16:00:26 +02:00
parent b04d35ee6b
commit aca180d6ba
3 changed files with 19 additions and 9 deletions

3
architecture.md Normal file
View file

@ -0,0 +1,3 @@
# Architecture
This document is intended to explain how the Plenum Bot works.

View file

@ -2,6 +2,9 @@
use std::env; use std::env;
use std::error::Error; use std::error::Error;
use std::fmt::Display;
use std::io::IsTerminal;
use std::os::linux::raw::stat;
use std::time::Instant; use std::time::Instant;
use chrono::{Local, NaiveDate}; use chrono::{Local, NaiveDate};
@ -9,18 +12,14 @@ use clap::{Arg, Command};
use colored::Colorize; use colored::Colorize;
use regex::Regex; use regex::Regex;
use std::fmt::Display; use cccron_lib::{trace_var, trace_var_, verboseln};
use std::io::IsTerminal;
use cccron_lib::config_spec::{self, CfgField, CfgGroup, CfgSpec}; use cccron_lib::config_spec::{self, CfgField, CfgGroup, CfgSpec};
use cccron_lib::key_value::KeyValueStore as KV;
use cccron_lib::date; use cccron_lib::date;
use cccron_lib::email::{self, Email, SimpleEmail}; use cccron_lib::email::{self, Email, SimpleEmail};
use cccron_lib::hedgedoc::{self, HedgeDoc}; use cccron_lib::hedgedoc::{self, HedgeDoc};
use cccron_lib::is_dry_run; use cccron_lib::is_dry_run;
use cccron_lib::key_value::{KeyValueStore as KV, KeyValueStore};
use cccron_lib::mediawiki::{self, Mediawiki}; use cccron_lib::mediawiki::{self, Mediawiki};
use cccron_lib::{verboseln,trace_var,trace_var_};
use cccron_lib::NYI; use cccron_lib::NYI;
/* ***** Config Spec ***** */ /* ***** Config Spec ***** */
@ -93,6 +92,14 @@ fn today() -> NaiveDate {
.map(|v| NaiveDate::parse_from_str(&v, "%F").expect("'TODAY' hat nicht format YYYY-MM-DD")) .map(|v| NaiveDate::parse_from_str(&v, "%F").expect("'TODAY' hat nicht format YYYY-MM-DD"))
.unwrap_or(Local::now().date_naive()) .unwrap_or(Local::now().date_naive())
} }
/// Gets either the state from the config or overrides it with the state from
/// the environment variable `STATE_OVERRIDE` (for testing purposes.)
fn state(config: &KeyValueStore) -> ProgramState {
match env::var("STATE_OVERRIDE") {
Ok(val) => ProgramState::parse(&val),
Err(_e) => ProgramState::parse(&config["state-name"])
}
}
#[derive(Debug)] #[derive(Debug)]
struct Args { struct Args {
@ -182,7 +189,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let nearest_plenum_days = date::get_matching_dates_around(today, plenum_spec); let nearest_plenum_days = date::get_matching_dates_around(today, plenum_spec);
trace_var!(nearest_plenum_days); trace_var!(nearest_plenum_days);
// figure out where we are // figure out where we are
let mut last_state = ProgramState::parse(&config["state-name"]); let mut last_state = state(&config);
let last_run = config.get("state-last-run").unwrap_or_default(); let last_run = config.get("state-last-run").unwrap_or_default();
let last_run = NaiveDate::parse_from_str(&last_run, "%Y-%m-%d").unwrap_or_default(); let last_run = NaiveDate::parse_from_str(&last_run, "%Y-%m-%d").unwrap_or_default();
trace_var!(last_run); trace_var!(last_run);

View file

@ -6,8 +6,8 @@ use reqwest::blocking::Client;
use serde::Deserialize; use serde::Deserialize;
use serde_json::json; use serde_json::json;
use crate::config_spec::{CfgField, CfgGroup};
use crate::{trace_var, verboseln}; use crate::{trace_var, verboseln};
use crate::config_spec::{CfgField, CfgGroup};
pub const CONFIG: CfgGroup<'static> = CfgGroup { pub const CONFIG: CfgGroup<'static> = CfgGroup {
name: "wiki", name: "wiki",
@ -284,7 +284,7 @@ pub fn pad_ins_wiki(old_pad_content: String, wiki: &Mediawiki) -> Result<(), Box
fn pandoc_convert(markdown: String) -> Result<String, Box<dyn Error>> { fn pandoc_convert(markdown: String) -> Result<String, Box<dyn Error>> {
let (output, errors, status) = crate::pipe( let (output, errors, status) = crate::pipe(
"pandoc", "pandoc",
&mut ["--from", "markdown", "--to", "mediawiki", "--no-highlight"], &mut ["--from", "markdown-auto_identifiers", "--to", "mediawiki", "--no-highlight"],
markdown, markdown,
)?; )?;
if status.success() { if status.success() {