| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | mod config; |
| 6 | mod forge; |
| 7 | mod install; |
| 8 | mod migrate; |
| 9 | mod setup; |
| 10 | |
| 11 | use crate::config::{App, Config}; |
| 12 | use crate::forge::Forge; |
| 13 | use std::env; |
| 14 | use std::path::{Path, PathBuf}; |
| 15 | use std::process::ExitCode; |
| 16 | |
| 17 | |
| 18 | pub type Res<T> = Result<T, String>; |
| 19 | |
| 20 | const USAGE: &str = "usage: kur [--config <path>] [command] |
| 21 | |
| 22 | commands: |
| 23 | (none) first run: set up if the config file is missing, else the same as status |
| 24 | run [app] install newer releases (all apps, or only <app>) |
| 25 | status show installed and newest releases; changes nothing |
| 26 | setup set up kur (idempotent): install the binary, write the config, install the timer |
| 27 | --version print the version |
| 28 | "; |
| 29 | |
| 30 | fn main() -> ExitCode { |
| 31 | let mut args = env::args().skip(1); |
| 32 | let mut cli_config: Option<PathBuf> = None; |
| 33 | let mut words = Vec::new(); |
| 34 | while let Some(arg) = args.next() { |
| 35 | match arg.as_str() { |
| 36 | "--config" => match args.next() { |
| 37 | Some(path) => cli_config = Some(path.into()), |
| 38 | None => return usage(), |
| 39 | }, |
| 40 | "--version" => { |
| 41 | println!("kur {}", env!("CARGO_PKG_VERSION")); |
| 42 | return ExitCode::SUCCESS; |
| 43 | } |
| 44 | _ => words.push(arg), |
| 45 | } |
| 46 | } |
| 47 | let words: Vec<&str> = words.iter().map(String::as_str).collect(); |
| 48 | let (only, status) = match words.as_slice() { |
| 49 | ["setup"] => return setup_command(cli_config.as_deref()), |
| 50 | ["run"] => (None, false), |
| 51 | ["run", name] => (Some(*name), false), |
| 52 | ["status"] => (None, true), |
| 53 | [] => match config::config_path(cli_config.clone()) { |
| 54 | Ok(path) => match path.try_exists() { |
| 55 | Ok(true) => (None, true), |
| 56 | Ok(false) => return setup_command(Some(&path)), |
| 57 | Err(e) => { |
| 58 | eprintln!("kur: cannot check {}: {e}", path.display()); |
| 59 | return ExitCode::FAILURE; |
| 60 | } |
| 61 | }, |
| 62 | Err(e) => { |
| 63 | eprintln!("kur: {e}"); |
| 64 | return ExitCode::FAILURE; |
| 65 | } |
| 66 | }, |
| 67 | _ => return usage(), |
| 68 | }; |
| 69 | |
| 70 | let config_path = match config::config_path(cli_config) { |
| 71 | Ok(path) => path, |
| 72 | Err(e) => { |
| 73 | eprintln!("kur: {e}"); |
| 74 | return ExitCode::FAILURE; |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | let cfg = match config::load(&config_path, !status) { |
| 79 | Ok(cfg) => cfg, |
| 80 | Err(e) => { |
| 81 | eprintln!("kur: {e}"); |
| 82 | return ExitCode::FAILURE; |
| 83 | } |
| 84 | }; |
| 85 | let note = if status { |
| 86 | " (not written, status changes nothing)" |
| 87 | } else { |
| 88 | "" |
| 89 | }; |
| 90 | for message in &cfg.migrated { |
| 91 | println!("kur: config: {message}{note}"); |
| 92 | } |
| 93 | for warning in &cfg.warnings { |
| 94 | eprintln!("kur: warning: {warning}"); |
| 95 | } |
| 96 | let apps: Vec<&App> = cfg |
| 97 | .apps |
| 98 | .iter() |
| 99 | .filter(|app| only.is_none_or(|name| app.name == name)) |
| 100 | .collect(); |
| 101 | if apps.is_empty() { |
| 102 | eprintln!("kur: no app named {}", only.unwrap_or_default()); |
| 103 | return ExitCode::FAILURE; |
| 104 | } |
| 105 | |
| 106 | let _lock = if status { |
| 107 | None |
| 108 | } else { |
| 109 | match install::ensure_state_dir(&cfg.state_dir).and_then(|()| install::lock(&cfg.state_dir)) |
| 110 | { |
| 111 | Ok(lock) => Some(lock), |
| 112 | Err(e) => { |
| 113 | eprintln!("kur: {e}"); |
| 114 | return ExitCode::FAILURE; |
| 115 | } |
| 116 | } |
| 117 | }; |
| 118 | process_apps(&cfg, &apps, status) |
| 119 | } |
| 120 | |
| 121 | |
| 122 | fn process_apps(cfg: &Config, apps: &[&App], status: bool) -> ExitCode { |
| 123 | let forge = Forge::new(cfg); |
| 124 | let mut failed = false; |
| 125 | for app in apps { |
| 126 | let result = if status { |
| 127 | install::status(&forge, cfg, app) |
| 128 | } else { |
| 129 | install::update(&forge, cfg, app) |
| 130 | }; |
| 131 | match result { |
| 132 | Ok(msg) => println!("{}: {msg}", app.name), |
| 133 | Err(e) => { |
| 134 | eprintln!("kur: {}: {e}", app.name); |
| 135 | failed = true; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | if failed { |
| 140 | ExitCode::FAILURE |
| 141 | } else { |
| 142 | ExitCode::SUCCESS |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | |
| 147 | fn setup_command(cli: Option<&Path>) -> ExitCode { |
| 148 | let result = config::config_path(cli.map(Path::to_path_buf)).and_then(|path| setup::run(&path)); |
| 149 | match result { |
| 150 | Ok(()) => ExitCode::SUCCESS, |
| 151 | Err(e) => { |
| 152 | eprintln!("kur: setup: {e}"); |
| 153 | ExitCode::FAILURE |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | |
| 159 | fn usage() -> ExitCode { |
| 160 | eprint!("{USAGE}"); |
| 161 | ExitCode::from(2) |
| 162 | } |