Skip to main content

wowlab_node/
update.rs

1//! Self-update via GitHub releases.
2
3const REPO_OWNER: &str = "legacy3";
4const REPO_NAME: &str = "wowlab";
5
6/// Errors raised by the self-update flow.
7#[derive(Debug, thiserror::Error)]
8#[error("self-update failed: {source}")]
9pub struct UpdateError {
10    #[source]
11    source: self_update::errors::Error,
12}
13
14impl From<self_update::errors::Error> for UpdateError {
15    fn from(error: self_update::errors::Error) -> Self {
16        Self { source: error }
17    }
18}
19
20/// Check GitHub releases for a newer version and return the download URL if available.
21///
22/// # Errors
23///
24/// Returns an error when release metadata cannot be fetched or compared.
25pub fn check_for_update(current_version: &str) -> Result<Option<String>, UpdateError> {
26    let releases = self_update::backends::github::ReleaseList::configure()
27        .repo_owner(REPO_OWNER)
28        .repo_name(REPO_NAME)
29        .build()?
30        .fetch()?;
31
32    if let Some(latest) = releases.first() {
33        if self_update::version::bump_is_greater(current_version, &latest.version)? {
34            return Ok(Some(latest.version.clone()));
35        }
36    }
37
38    Ok(None)
39}
40
41/// Returns `true` if updated and `false` if already on the latest release.
42///
43/// # Errors
44///
45/// Returns an error when release metadata or the update archive cannot be downloaded, verified, or installed.
46pub fn install_update(bin_name: &str, current_version: &str) -> Result<bool, UpdateError> {
47    tracing::info!(current_version, "Checking for updates");
48
49    let status = self_update::backends::github::Update::configure()
50        .repo_owner(REPO_OWNER)
51        .repo_name(REPO_NAME)
52        .bin_name(bin_name)
53        .identifier(bin_name)
54        .show_download_progress(false)
55        .no_confirm(true)
56        .current_version(current_version)
57        .build()?
58        .update()?;
59
60    if status.updated() {
61        tracing::info!(version = status.version(), "Node updated");
62
63        Ok(true)
64    } else {
65        tracing::debug!("Already on latest version");
66
67        Ok(false)
68    }
69}