Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/monetize

crates/monetize-box-watcher/src/main.rs

//! `monetize-box-watcher [--config <watcher.toml>] [--dry-run]` — one run (a systemd timer
//! runs it every few minutes). `--dry-run` starts nothing whatever the config says.

use std::path::PathBuf;

fn main() {
    let mut args = std::env::args().skip(1);
    let mut config = std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".config/monetize-box-watcher/watcher.toml")).unwrap_or_else(|| PathBuf::from("watcher.toml"));
    let mut dry = false;
    while let Some(a) = args.next() {
        match a.as_str() {
            "--config" => config = args.next().map(PathBuf::from).unwrap_or(config),
            "--dry-run" => dry = true,
            "-h" | "--help" => {
                println!("monetize-box-watcher [--config <watcher.toml>] [--dry-run]");
                return;
            }
            other => {
                eprintln!("monetize-box-watcher: unknown argument {other:?}");
                std::process::exit(2);
            }
        }
    }
    let mut cfg = match monetize_box_watcher::load(&config) {
        Ok(c) => c,
        Err(e) => {
            eprintln!("monetize-box-watcher: {e}");
            std::process::exit(2);
        }
    };
    if dry {
        cfg.apply = false;
    }
    if cfg.hold_dir.is_none() {
        cfg.hold_dir = monetize_box_watcher::default_hold_dir();
    }
    println!("monetize-box-watcher: {} ONE box(es), {} account(s), {}", cfg.boxes.len(), cfg.accounts.len(), if cfg.apply { "APPLY: a stopped ONE box is started" } else { "DRY-RUN: nothing is started" });
    let (apis, refused) = monetize_box_watcher::open_accounts(&cfg);
    for (name, why) in &refused {
        println!("ALERT box-watcher.account {name}: {why}");
    }
    for (name, verdict) in monetize_box_watcher::run(&cfg, &apis, &refused, &monetize_box_watcher::resolve) {
        println!("{}", verdict.line(&name));
    }
}