forge
Explore
Log in

Don't you dare go Hollow

kur

Clone
★ 0

0 watching0 forksRustAGPL-3.073.7 KiB6ae6238Tahinli21 days ago5 commits

main.rs162 lines, 5.0 KiBHistoryRawDownload
1//! kur: installs the newest forge release of each configured app on this host, with rollback.
2//! Runs from a systemd user timer as the unprivileged account that owns the apps (never root).
3//! Prints one line per app on stdout, errors on stderr.
4
5mod config;
6mod forge;
7mod install;
8mod migrate;
9mod setup;
10
11use crate::config::{App, Config};
12use crate::forge::Forge;
13use std::env;
14use std::path::{Path, PathBuf};
15use std::process::ExitCode;
16
17/// Crate-wide result type; errors are human-readable messages.
18pub type Res<T> = Result<T, String>;
19
20const USAGE: &str = "usage: kur [--config <path>] [command]
21
22commands:
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
30fn 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 // Status never writes, so a renamed key is only reported.
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 // Held until exit, so a manual `kur run` and a timer run never stage the same files at once.
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/// Runs status or update for each app. One app failing never stops the others.
122fn 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/// Runs first-run setup for the resolved config path (`cli` overrides it, as `--config` does).
147fn 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/// Prints usage and returns exit code 2 (bad invocation).
159fn usage() -> ExitCode {
160 eprint!("{USAGE}");
161 ExitCode::from(2)
162}