rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! `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;