1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//! `rho update --check`: reports the latest release and how `rho update`
//! would install it, without installing anything or touching the daemon.
//! Desktop clients run it, locally or over ssh, to offer an update.
use anyhow::Context;
use serde::Serialize;
use super::{
detect_install_method, install_script_ref, latest_release, InstallMethod, Lookups, UpdateInfo,
Updater,
};
/// How `rho update --check` prints its report.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum CheckFormat {
/// A short summary for a person.
Text,
/// One JSON object, `UpdateCheck`, for a program.
Json,
}
/// The report `rho update --check --json` prints, which the daemon also
/// answers `rho/daemon/update/check` with
/// (`app::daemon_host::machine_update`). Clients such as rho-ui parse
/// these field names, so keep them stable.
#[derive(Debug, Serialize)]
pub(crate) struct UpdateCheck {
pub(crate) current: String,
/// The latest release's version, without the tag prefix.
pub(crate) latest: String,
pub(crate) update_available: bool,
pub(crate) method: InstallMethod,
/// The command `rho update` runs for `method`. On Windows, where
/// `rho update` replaces an install script's rho itself and prints the
/// command for the other methods, the command that updates by hand.
/// `null` with no update, as `rho update` then resolves none either.
pub(crate) command: Option<String>,
/// `rho update` cannot finish unattended, so a client should run it in
/// a terminal rather than in the background.
pub(crate) needs_terminal: bool,
}
impl UpdateCheck {
/// The report of `update` for a rho installed by `method`, whose
/// command names what `lookups` find. Fails when an update is
/// available but the command cannot be resolved, as `rho update`
/// would.
pub(crate) fn new(
update: UpdateInfo,
method: InstallMethod,
lookups: &Lookups,
) -> anyhow::Result<Self> {
let update_available = update.is_available();
let UpdateInfo {
latest_tag,
latest_version,
current_version,
} = update;
Ok(Self {
current: current_version,
latest: latest_version,
update_available,
method,
command: update_available
.then(|| method.update_command_in(&install_script_ref(latest_tag), lookups))
.transpose()?,
needs_terminal: method.needs_terminal(),
})
}
}
/// Runs `rho update --check`. Unlike `rho update`, which goes on without the
/// release, a failed lookup is an error: the check has nothing else to report.
pub(crate) async fn run_check(current_version: &str, format: CheckFormat) -> anyhow::Result<()> {
let update = latest_release(current_version)
.await
.context("could not check the latest rho release")?;
let check = UpdateCheck::new(update, detect_install_method(), &Lookups::OfThisProcess)?;
match format {
CheckFormat::Json => println!("{}", serde_json::to_string(&check)?),
CheckFormat::Text => print_summary(&check),
}
Ok(())
}
fn print_summary(check: &UpdateCheck) {
println!("install method: {}", check.method.label());
let Some(command) = &check.command else {
println!(
"rho is up to date (v{}, latest release v{}).",
check.current, check.latest
);
return;
};
println!(
"rho v{} is available (current v{}).",
check.latest, check.current
);
println!("update command: {command}");
match check.method.updater() {
Updater::PrintsCommand => println!("run the update command to install it."),
Updater::Command if check.needs_terminal => {
println!("run `rho update` in a terminal to install it.")
}
Updater::Command | Updater::InPlace => println!("run `rho update` to install it."),
}
}
#[cfg(test)]
#[path = "update_check_tests.rs"]
mod tests;