Skip to main content

kimun_notes/cli/commands/
update.rs

1//! `kimun update [--check]` — check for a newer release and, where the install
2//! channel permits, self-update in place.
3
4use color_eyre::eyre::Result;
5
6use crate::update;
7
8/// Run the update command. `check_only` reports without downloading.
9pub async fn run(check_only: bool) -> Result<()> {
10    let config_dir = crate::settings::config_dir()?;
11
12    // One GitHub round-trip: fetch the release once, derive the status from it,
13    // and (if applying) reuse the very same release — no second fetch.
14    let latest = update::latest_release().await?;
15    let status = update::status_for(&config_dir, &latest);
16
17    println!("Current version: {}", status.current);
18    println!("Latest version:  {}", status.latest);
19
20    if !status.update_available {
21        println!("kimün is up to date.");
22        return Ok(());
23    }
24
25    println!("Update available: {} → {}", status.current, status.latest);
26
27    if check_only {
28        return Ok(());
29    }
30
31    if !status.channel.self_update_eligible() {
32        match status.channel.upgrade_hint() {
33            Some(cmd) => println!("To upgrade, run: {cmd}"),
34            None => println!(
35                "This install cannot self-update. Download the latest release from {}",
36                update::releases_url()
37            ),
38        }
39        return Ok(());
40    }
41
42    println!("Downloading and installing {}...", status.latest);
43    update::install(latest).await?;
44    println!(
45        "Updated to {}. Restart kimün to use the new version.",
46        status.latest
47    );
48    Ok(())
49}