//! kur: installs the newest forge release of each configured app on this host, with rollback. //! Runs from a systemd user timer as the unprivileged account that owns the apps (never root). //! Prints one line per app on stdout, errors on stderr. mod config; mod forge; mod install; mod migrate; mod setup; use crate::config::{App, Config}; use crate::forge::Forge; use std::env; use std::path::{Path, PathBuf}; use std::process::ExitCode; /// Crate-wide result type; errors are human-readable messages. pub type Res = Result; const USAGE: &str = "usage: kur [--config ] [command] commands: (none) first run: set up if the config file is missing, else the same as status run [app] install newer releases (all apps, or only ) status show installed and newest releases; changes nothing setup set up kur (idempotent): install the binary, write the config, install the timer --version print the version "; fn main() -> ExitCode { let mut args = env::args().skip(1); let mut cli_config: Option = None; let mut words = Vec::new(); while let Some(arg) = args.next() { match arg.as_str() { "--config" => match args.next() { Some(path) => cli_config = Some(path.into()), None => return usage(), }, "--version" => { println!("kur {}", env!("CARGO_PKG_VERSION")); return ExitCode::SUCCESS; } _ => words.push(arg), } } let words: Vec<&str> = words.iter().map(String::as_str).collect(); let (only, status) = match words.as_slice() { ["setup"] => return setup_command(cli_config.as_deref()), ["run"] => (None, false), ["run", name] => (Some(*name), false), ["status"] => (None, true), [] => match config::config_path(cli_config.clone()) { Ok(path) => match path.try_exists() { Ok(true) => (None, true), Ok(false) => return setup_command(Some(&path)), Err(e) => { eprintln!("kur: cannot check {}: {e}", path.display()); return ExitCode::FAILURE; } }, Err(e) => { eprintln!("kur: {e}"); return ExitCode::FAILURE; } }, _ => return usage(), }; let config_path = match config::config_path(cli_config) { Ok(path) => path, Err(e) => { eprintln!("kur: {e}"); return ExitCode::FAILURE; } }; // Status never writes, so a renamed key is only reported. let cfg = match config::load(&config_path, !status) { Ok(cfg) => cfg, Err(e) => { eprintln!("kur: {e}"); return ExitCode::FAILURE; } }; let note = if status { " (not written, status changes nothing)" } else { "" }; for message in &cfg.migrated { println!("kur: config: {message}{note}"); } for warning in &cfg.warnings { eprintln!("kur: warning: {warning}"); } let apps: Vec<&App> = cfg .apps .iter() .filter(|app| only.is_none_or(|name| app.name == name)) .collect(); if apps.is_empty() { eprintln!("kur: no app named {}", only.unwrap_or_default()); return ExitCode::FAILURE; } // Held until exit, so a manual `kur run` and a timer run never stage the same files at once. let _lock = if status { None } else { match install::ensure_state_dir(&cfg.state_dir).and_then(|()| install::lock(&cfg.state_dir)) { Ok(lock) => Some(lock), Err(e) => { eprintln!("kur: {e}"); return ExitCode::FAILURE; } } }; process_apps(&cfg, &apps, status) } /// Runs status or update for each app. One app failing never stops the others. fn process_apps(cfg: &Config, apps: &[&App], status: bool) -> ExitCode { let forge = Forge::new(cfg); let mut failed = false; for app in apps { let result = if status { install::status(&forge, cfg, app) } else { install::update(&forge, cfg, app) }; match result { Ok(msg) => println!("{}: {msg}", app.name), Err(e) => { eprintln!("kur: {}: {e}", app.name); failed = true; } } } if failed { ExitCode::FAILURE } else { ExitCode::SUCCESS } } /// Runs first-run setup for the resolved config path (`cli` overrides it, as `--config` does). fn setup_command(cli: Option<&Path>) -> ExitCode { let result = config::config_path(cli.map(Path::to_path_buf)).and_then(|path| setup::run(&path)); match result { Ok(()) => ExitCode::SUCCESS, Err(e) => { eprintln!("kur: setup: {e}"); ExitCode::FAILURE } } } /// Prints usage and returns exit code 2 (bad invocation). fn usage() -> ExitCode { eprint!("{USAGE}"); ExitCode::from(2) }