Cotton code
73 lines · 3.4 KB · RustRaw
//! The Mac's meetings job, as launchd runs it//! (`cotton_mac::meetings_job` says what a wake does).//!//!   meetings-job wake --config <file> [--now]        one wake (launchd's; --now skips the back-off and the build's wait)//!   meetings-job status --config <file> [--loaded yes|no]   the one status line; exit 0 when the job may run//!   meetings-job dry-run --config <file> --file <audio>     the engine over one local file, nothing sent//!//! Installed, removed and asked through `pnpm -s meetings:job:install`,//! `meetings:job:remove`, `meetings:job:status` and `meetings:job:run`//! (the meetings job script). Nothing here prints a credential, and the log//! holds no meeting's words: the job's own lines go to its log, and launchd//! sends this process's output nowhere.use std::path::PathBuf;use std::process::ExitCode;use cotton_mac::meetings_job::{self as job, Config, Log, State};const USAGE: &str = "meetings-job (wake [--now] | status [--loaded yes|no] | dry-run --file <audio>) --config <file>";fn main() -> ExitCode {    let argv: Vec<String> = std::env::args().skip(1).collect();    let Some(verb) = argv.first().cloned() else {        eprintln!("{USAGE}");        return ExitCode::from(2);    };    let (mut config, mut forced, mut loaded, mut file) = (None::<PathBuf>, false, true, None::<PathBuf>);    let mut at = 1;    while at < argv.len() {        let value = argv.get(at + 1).cloned().unwrap_or_default();        match argv[at].as_str() {            "--config" => { config = Some(PathBuf::from(&value)); at += 1; }            "--now" => forced = true,            "--loaded" => { loaded = value == "yes"; at += 1; }            "--file" => { file = Some(PathBuf::from(&value)); at += 1; }            "-h" | "--help" => { println!("{USAGE}"); return ExitCode::SUCCESS; }            other => { eprintln!("unknown flag {other}\n{USAGE}"); return ExitCode::from(2); }        }        at += 1;    }    let Some(config) = config else {        eprintln!("--config is needed\n{USAGE}");        return ExitCode::from(2);    };    let cfg = match Config::read(&config) {        Ok(cfg) => cfg,        Err(why) => { eprintln!("meetings job: no config: {why}"); return ExitCode::from(2); }    };    cfg.export();    // A fault says where it was in the job's log, never what it held.    let log_path = cfg.log.clone();    std::panic::set_hook(Box::new(move |info| {        let place = info.location().map(|l| format!("{}:{}", l.file(), l.line())).unwrap_or_default();        Log::new(&log_path).say(&format!("fault: the job stopped at {place}"));    }));    match verb.as_str() {        "wake" => ExitCode::from(job::wake(&cfg, forced).exit()),        "status" => {            let state = State::load(&cfg.state_path());            let (line, ok) = job::status_line(&cfg, &state, loaded, job::signed_in(&cfg), job::models_held(&cfg), job::now());            println!("{line}");            if ok { ExitCode::SUCCESS } else { ExitCode::from(1) }        }        "dry-run" => {            let Some(audio) = file else { eprintln!("dry-run needs --file <audio>"); return ExitCode::from(2); };            match job::dry_run(&cfg, &audio) {                Ok(line) => { println!("{line}"); ExitCode::SUCCESS }                Err(why) => { eprintln!("meetings job: {}", job::one_line(&why)); ExitCode::FAILURE }            }        }        other => { eprintln!("unknown verb {other}\n{USAGE}"); ExitCode::from(2) }    }}